串口通信

 [STAThread]
             static void Main()
             {
                   Application.Run(new Form1());
             }

             public int iPort = 1; //1,2,3,4
             public int iRate = 9600; //1200,2400,4800,9600
             public byte bSize = 8; //8 bits
             public byte bParity = 0; // 0-4=no,odd,even,mark,space
             public byte bStopBits = 1; // 0,1,2 = 1, 1.5, 2
             public int iTimeout = 1000;
             public mycom mycom1 = new mycom();
             public byte[] recb;

             //程序开启,串口初始化
             private void Form1_Load(object sender, System.EventArgs e)
             {
                   mycom1.PortNum = iPort;
                   mycom1.BaudRate = iRate;
                   mycom1.ByteSize = bSize;
                   mycom1.Parity = bParity;
                   mycom1.StopBits = bStopBits;
                   mycom1.ReadTimeout = iTimeout;
                   if (this.OpenCom())
                         msg.AppendText("串口初始化成功……\r\n");
                   else
                         msg.AppendText("串口初始化失败!\r\n");
             }
             //显示包信息
             public string dis_package(byte[] reb)
             {
                   string temp = "";
                   foreach (byte b in reb)
                         temp += b.ToString("X2") + " ";
                   return temp;
             }
             //开串口
             public bool OpenCom()
             {
                   try
                   {
                         if (mycom1.Opened)
                         {
                               mycom1.Close();
                               mycom1.Open(); //打开串口
                         }
                         else
                         {
                               mycom1.Open();//打开串口
                         }
                         return true;
                   }
                   catch (Exception e)
                   {
                         MessageBox.Show("错误:" + e.Message);
                         return false;
                   }

             }
             //发送按钮
             private void button1_Click(object sender, System.EventArgs e)
             {
                   if (t_send.Text == "")
                   { MessageBox.Show("发送数据为空!"); return; }
                   byte[] temp1 = mysendb();
                   int sendnumb = 0;
                   try
                   {
                         sendnumb = mycom1.Write(temp1);
                         msg.AppendText("\r\n发送数据(" + sendnumb + "):" + dis_package(temp1));
                         recb = mycom1.Read(50);
                         //if(recb.Length!=0)
                         msg.AppendText("\r\n接收到数据包:" + dis_package(recb));
                   }
                   catch
                   { msg.AppendText("\r\n发送失败!"); return; }

                   //OpenCom();
             }
             //去掉发送数组中的空格
             public string delspace(string putin)
             {
                   string putout = "";
                   for (int i = 0; i < putin.Length; i++)
                   {
                         if (putin[i] != ' ')
                               putout += putin[i];
                   }
                   return putout;
             }
             //提取数据包
             public byte[] mysendb()
             {
                   string temps = delspace(t_send.Text);
                   byte[] tempb = new byte[50];
                   int j = 0;
                   for (int i = 0; i < temps.Length; i = i + 2, j++)
                         tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
                   byte[] send = new byte[j];
                   Array.Copy(tempb, send, j);
                   return send;
             }
             //清空按钮
             private void button3_Click(object sender, System.EventArgs e)
             {
                   t_send.Text = string.Empty;
                   msg.Text = string.Empty;
             }

             //参数设置
             private void button2_Click(object sender, System.EventArgs e)
             {
                   iPort = Convert.ToInt16(t_port.Text); //1,2,3,4
                   iRate = Convert.ToInt16(t_rate.Text); //1200,2400,4800,9600
                   bSize = Convert.ToByte(t_bytesize.Text, 10); //8 bits
                   bParity = Convert.ToByte(t_parity.Text, 10); // 0-4=no,odd,even,mark,space
                   bStopBits = Convert.ToByte(t_stopbyte.Text, 10); // 0,1,2 = 1, 1.5, 2
                   //iTimeout=3;
                   if (this.OpenCom())
                         msg.AppendText("串口初始化成功……\r\n");
                   else
                         msg.AppendText("串口初始化失败!\r\n");
             }
             //程序关闭,结束串口
             private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
             {
                   mycom1.Close();
             }

             private void button5_Click(object sender, System.EventArgs e)
             {
                   if (mycom1.Opened)
                   {
                         mycom1.Close();
                         button5.Text = "开启串口";
                         msg.AppendText("\r\n串口被关闭……");
                   }
                   else
                   {
                         mycom1.Open();
                         button5.Text = "关闭串口";
                         msg.AppendText("\r\n串口成功开启……");
                   }
             }
       }
}

原文地址:https://www.cnblogs.com/jcomet/p/1251093.html