C# 串口通讯

本人做的一个  

C# 串口通讯

一,软件概述

本上位机采用 上位机每次点击“发送”按钮后将发送 包头:0xAA;命令 :0x01;帧长:0x0D;帧数据 下位机接收到数据后,将发送 二,具体实现核心代码

1 //             string[] ports = SerialPort.GetPortNames();

            Array.Sort(ports);

            comboBoxPortSelect.Items.AddRange(ports);

//             comboBoxPortSelect.SelectedIndex = 0;

//             if (mycomm.IsOpen) {

                mycomm.Close();

            }

mycomm.PortName = comboBoxPortSelect.Text;

             mycomm.ReadTimeout = 32;

            try  {

                mycomm.Open();

                btnOpen.Text = “                 lblToolStripStatus.Text = “             }catch{

                btnOpen.Text = “                 MessageBox.Show(“                 lblToolStripStatus.Text = “             }

            //             mycomm.DataReceived += comm_DataReceived;          

2 //             byte[] package = new byte[18]; int j = 0; int k = 3;

            package[0] = 0xAA;//             package[1] = 0x01;//             package[2] = 0x0D;//             byte parity = 0x01+0x0d;//

            //             byte[] realData = new byte[13];

            string data = getMode().Append(getTxFre(txtTxFre.Text).Append(getDuoPuLe(txtDuoFre.Text)).Append(    getBand()).Append(getTxPower()).Append(getRxFre(txtRxFre.Text)).Append(getRxBandAndDelay()).Append(getAGC())).ToString();//             for (int i = 0; i < data.Length; i += 8)

            {

                //                 realData[j++] = (byte)Convert.ToInt32(data.Substring(i, 8), 2);               

            }            

            foreach (byte b in realData) {           

                 parity += b;//                  package[k++] = b; }

            parity &= 0x7F; //             package[16] = parity;//             package[17] = 0xa5;//             if (!mycomm.IsOpen)

            {

                MessageBox.Show(“                 return;

            }

            mycomm.Write(package, 0, 18);// 3 private void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)

        {           

            int n = mycomm.BytesToRead;

            byte[] buf = new byte[n];//             mycomm.Read(buf, 0, n);//             builder.Clear();//             //             this.Invoke((EventHandler)(delegate

            {    //                 foreach (byte b in buf)

                {

                    builder.Append(b.ToString(“X2″) + ” “);                   

                }

                //                 //builder.Append(Encoding.ASCII.GetString(buf));   

                txtGet.Text = “”;

                txtGet.AppendText(builder.ToString());

            }));           

            //             if (buf[3] == 0x00){

                lblToolStripRxStatus.Text = “             }                             

            else if(buf[3] == 0x01){

                lblToolStripRxStatus.Text = “             } 

            //             byte rxParity;//             rxParity = 0x01+0x01;

            rxParity += buf[3];

            rxParity &= 0x7F;

            if (rxParity != buf[4]) {

                lblToolStripRxStatus.Text += “             }  

        }

三,总结

  总体上说,    byte[] package = new byte[18]{0};

   mycomm.Write(package, 0, 18);

接收数据在串口的

 private void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)

  { 

int n = mycomm.BytesToRead; 

byte[] buf = new byte[n]; 

comm.Read(buf, 0, n);

 }   需要注意的地方是每次接收到的数据未必是一个完整的包(尤其是包较长时),例如刚开始本人本机调试(本地收发)时收到的18字节经常是先收到9字节,再收到9字节,后者先2,再8,再8字节。导致接手区的数据不对(没有18字节,只有最开始的92字节),此时可以用一个缓冲数组保存接收到的数据,直到接收的长度正确或到包尾了再进行处理。

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2015年4月5日
下一篇 2015年4月6日

相关推荐