资源简介

用C#些的串口与欧姆龙PLC之间的通信,自动计算校验码,使用hostlink协议进行通信.用计算机做串口通信的朋友也可以借鉴其中关于串口操作的代码

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ascii码串口调试
{
    public partial class Form1 : Form
    {
        int intFcs;
        string received;

        public Form1()
        {
            InitializeComponent();
        }


        //SEND
        private void button1_Click(object sender EventArgs e)
        {
            string str = textBox1.Text;

            int n;
            int l = str.Length;
            char m = ‘\0‘;
            for (int i = 0; i < l; i++)
            {
                n = str[i] ^ m;
                m = (char)(n);
            }
            n = (int)(m);
            textBox3.Text = ““;
            textBox3.Text = textBox1.Text + String.Format(“{0:x}“ n) + “*\r“;


            try
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                    serialPort1.Open(); //打开串口
                }
                else
                {
                    serialPort1.Open();//打开串口
                }
            }
            catch
            {
                MessageBox.Show(“串口不存在或被占用“);
            }
            received = ““;
            string strSend = textBox3.Text;
            serialPort1.Write(strSend); //接收串口数据方法:事件serialport_datareceived、readExisting、read(byte[]intint)、readbyte()
            
        }

        private void serialPort1_DataReceived(object sender System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            received = received+serialPort1.ReadExisting();

        }


        //RECEIVED
        private void button2_Click(object sender EventArgs e)
        {
            textBox2.Text = received;
            received = ““;

        }


        //CLose
        private void button3_Click(object sender EventArgs e)
        {
            serialPort1.Close();
            this.Close();
        }



        //FCS
        private void button4_Click(object sender EventArgs e)
        {
            string str = textBox1.Text;

            int n;
            int l = str.Length;
            char m = ‘\0‘;
            for (int i = 0; i < l; i++)
            {
                n = str[i] ^ m;
                m = (char)(n);
            }
            n = (int)(m);
            textBox3.Text = ““;
            textBox3.Text = textBox1.Text+ String.Format(“{0:x}“n)+“*\r“;

        }

        private void Form1_Load(object sender EventArgs e)
        {
            this.ControlBox = false;
        }

    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5612  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\Properties\Resources.resx

     文件        249  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\Properties\Settings.settings

     文件       1312  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\Properties\AssemblyInfo.cs

     文件       2867  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\Properties\Resources.Designer.cs

     文件       1103  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\Properties\Settings.Designer.cs

     文件       7834  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\Form1.Designer.cs

     文件       4710  2009-04-09 10:39  ascii码串口调试\ascii码串口调试\04.ico

     文件       6013  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\Form1.resx

     文件       3359  2009-04-09 10:39  ascii码串口调试\ascii码串口调试\ascii码串口调试.csproj

     文件       5632  2005-09-23 06:56  ascii码串口调试\ascii码串口调试\bin\Debug\ascii码串口调试.vshost.exe

     文件      28672  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\bin\Debug\ascii码串口调试.exe

     文件      28160  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\bin\Debug\ascii码串口调试.pdb

     文件        180  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\obj\Debug\ascii码串口调试.Form1.resources

     文件        180  2009-03-21 10:31  ascii码串口调试\ascii码串口调试\obj\Debug\ascii码串口调试.Properties.Resources.resources

     文件      28672  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\obj\Debug\ascii码串口调试.exe

     文件      28160  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\obj\Debug\ascii码串口调试.pdb

     文件        842  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\obj\Debug\ascii码串口调试.csproj.GenerateResource.Cache

     文件        359  2009-04-13 21:12  ascii码串口调试\ascii码串口调试\obj\ascii码串口调试.csproj.FileList.txt

     文件       2771  2009-04-10 16:55  ascii码串口调试\ascii码串口调试\Form1.cs

     文件        490  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\Program.cs

     文件        949  2009-03-21 10:25  ascii码串口调试\ascii码串口调试.sln

    ..A..H.     14336  2009-04-13 21:58  ascii码串口调试\ascii码串口调试.suo

     目录          0  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\obj\Debug\TempPE

     目录          0  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\bin\Debug

     目录          0  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\obj\Debug

     目录          0  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\Properties

     目录          0  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\bin

     目录          0  2009-03-21 10:25  ascii码串口调试\ascii码串口调试\obj

     目录          0  2009-03-21 10:25  ascii码串口调试\ascii码串口调试

     目录          0  2009-03-21 10:25  ascii码串口调试

............此处省略3个文件信息

评论

共有 条评论