Technology Sharing

[Source code open source] C# desktop application development: serial port debugging assistant

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

C# Desktop Application Development

1. Environment construction and project creation: refer to the Pomodoro Timer project

Project creation reference

2. Interface layout design

insert image description here

3. Specific functions

(1) Port scanning:
    private void btn_com_scan_Click(object sender, EventArgs e)
    {
        //端口号扫描
        ReflashPortToComboBox(serialPort1, cb_select_com);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
(2) Initialize interface data settings
    private void Form1_Load(object sender, EventArgs e)
    {
        //第一次加载的时候,进行端口号扫描
        ReflashPortToComboBox(serialPort1, cb_select_com);
        cb_select_com.SelectedItem = "COM1";
        cb_select_baudrate.SelectedItem = "115200";
        cb_select_data_bits.SelectedItem = "8";
        cb_select_check_bit.SelectedItem = "None";
        cb_select_stop_bit.SelectedItem = "1";

        btn_open_com.Enabled = true;
        btn_close_com.Enabled = false;
        //默认使用 ascii模式
        rbtn_recv_ascii.Checked = true;
        rbtn_send_ascii.Checked = true;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
(3) Serial port receiving:
 private void ReceiveData(object sender, SerialDataReceivedEventArgs e)
 {
     string content = serialPort1.ReadExisting();        //从串口中读取输入流,返回string
     
     ShowData(content);
 }
 private void ShowData(string text)
 {
     string receiveText = text;
     
     //更新接收数据计数
     recv_cntr += (UInt32)receiveText.Length;
     lab_recv_cntr.Text = recv_cntr.ToString();

     textBox_debug.AppendText("接收到了"+ receiveText.Length.ToString()+ "个数据: "+ text+"rn");

     if (rbtn_recv_hex.Checked)
     {//按HEX模式 展示接收到的内容 
         byte[] recData = System.Text.Encoding.Default.GetBytes(receiveText);// 将接受到的字符串据转化成数组;  

         foreach (byte str in recData)
         {
             textBox_recv.AppendText(string.Format("{0:X2} ", str));
         }
     }
     else
     {//按ASCII模式 展示接收到的内容
         
         textBox_recv.AppendText(text);                   //将收到的字符串追加展示出来
     }

 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
(4) Button event:
private void btn_open_com_Click(object sender, EventArgs e)
{
    Int32 ibaudrate = Convert.ToInt32(cb_select_baudrate.SelectedItem.ToString());
    serialPort1.PortName = cb_select_com.SelectedItem.ToString();
    serialPort1.BaudRate = ibaudrate;
    serialPort1.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), cb_select_check_bit.Text);
    serialPort1.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), cb_select_stop_bit.Text);
    serialPort1.DataBits = Convert.ToInt16(cb_select_data_bits.Text);

    //添加串口事件处理
    serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(ReceiveData);


    try
    {
        serialPort1.Open();
        btn_open_com.Enabled = false;
        btn_close_com.Enabled = true;
    }
    catch (Exception ex)
    {
        MessageBox.Show("串口打开失败"+ex, "Error");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

4. Complete engineering code open source

The source code is packaged and uploaded on CSDN. The original intention is that you don't need to use points to download it for free. If you encounter points when downloading, just contact me and send it privately.