最简单的串口通信上位机(VS2019 C#)

最简单的串口通信上位机(VS2019 C#)

      • 最基本的页面:
      • 部分代码介绍:
    • VS2019程序压缩包

最基本的页面:

这里用了两个groupBox,这样就可以在两个页面分别用radioButton了。
最简单的串口通信上位机(VS2019 C#)_第1张图片

部分代码介绍:

首先是 打开串口 按钮的代码:

private void Button_com_Click(object sender, EventArgs e)
  {
      try
      {           
      if (!s.IsOpen )
      {
              button_send.Enabled = true;  //打开串口的同时让send按钮可用
              //SerialPort s = new SerialPort(); 这是在之前定义的
              s.PortName = comboBox_com_selsct.SelectedItem.ToString();  
              s.BaudRate = Convert.ToInt32(comboBox_baud_select.SelectedItem.ToString());   //     获取或设置串行波特率。
              s.Open();
              s.DataReceived += s_DataReceived;
           
               
              button_com.Text = "关闭串口";
          }
      else
      {
          button_com.Text = "打开串口";
              s.Close();
      }
      }
      catch (Exception  ee)
      {
          MessageBox.Show(ee.ToString());
      }
  }

其中,最重要的就是 s_DataReceived 的过程了:

 void s_DataReceived(object sender,SerialDataReceivedEventArgs e)  
    
 {
     int count = s.BytesToRead;  //     接收缓冲区中数据的字节数。
     //String str = null;
     
     byte[] buff = new byte[count];            
     received_count += count;
     s.Read(buff, 0, count);
     builder.Clear();
     this .Invoke ((EventHandler )(delegate
     {   
         if(radioButton_gettext.Checked == true )
         {
             //Regex regChina = new Regex("^[^\x00-\xFF]");
             //if(regChina.IsMatch(buff))
             builder.Append(Encoding.GetEncoding("GB18030").GetString(buff));  //中文GBK编码
            
         }
        
         if (radioButton_getHex .Checked == true)
         {
             foreach (byte b in buff )
             {
                 builder.Append(b.ToString("X2") + " ");
             }
         }
         
         this.textBox_get.AppendText(builder.ToString());
         label_getcount.Text = "Get:" + received_count.ToString();
     }));
 }

发送 按钮介绍:

private void Button_send_Click(object sender, EventArgs e)
{
    int n=0;
    if (radioButton_sendHex .Checked ==true )
    {
       
        String[] mc = textBox_send.Text.Split(' ');      //分离为数组
        
        List<byte> buff = new List<byte>();

        foreach (string m in mc)
        {
            try
            {
                buff.Add(byte.Parse(m, System.Globalization.NumberStyles.HexNumber));
            }catch(Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
            
        }
		//发送新行 发送回车
        if(checkBox_newLine.Checked == true)
        {
            buff.Add(0x0d);
            buff.Add(0x0a);
        }
        

        s.Write(buff.ToArray(), 0, buff.Count);
        n = buff.Count;
        
    }
    
    if (radioButton_sendtext.Checked == true)
    {
        byte[] sent_bytes;
        if (checkBox_newLine.Checked == false)
        {
            sent_bytes = Encoding.GetEncoding("GB18030").GetBytes(textBox_send.Text);
            n = textBox_send.TextLength;
        }
            
        else
        {
            sent_bytes = Encoding.GetEncoding("GB18030").GetBytes(textBox_send.Text + "\r\n");
            n = textBox_send.TextLength + 2;
        }
            
        
        s.Write(sent_bytes,0,sent_bytes.Length);
        //s.Write(sent_bytes, 0, sent_bytes.Length);
        
    }
    
    send_count += n;
    label_sendcount.Text = "Send:" + send_count.ToString();
}

VS2019程序压缩包

链接: link.

你可能感兴趣的:(C#)