C# 学习之 串口小工具

最近由于工作原因,需要做一些小小的上位机,就来学习写一个简单的上位机玩玩,刚开始的时候,曾经一度犹豫,是该用C++ 呢,还是C# 呢,还是用Qt 来做?

我并没有跨平台的需求,也没有太多的时间去捣腾MFC, 最后就选择了C#,必经拖控件顺手,美不美观,测试用用的,凑活就够了。

OK,话不多说,下面开始介绍我的小工具的搭建过程:
1.画一个像下面的小界面,看着的确是不怎么好看,一个丁点大的地儿,放了那么多的控件,想想都揪心
这里写图片描述

2.绑定事件 其实也没啥,系统都会帮我们全部建好,不用我们再去一行一行的去添加代码,so easy!!

this.Serial_Rate.SelectedIndexChanged += new System.EventHandler(this.Serial_Param_Changed);// 下拉列表绑定事件
this.Serial_OpenPort.Click += new System.EventHandler(this.Button_OpenPort_Click);//打开串口绑定事件
this.Serial_Send_Data.Click += new System.EventHandler(this.Serial_Send_Data_Click);//发送数据绑定事件
this.Serial_SendText.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Serial_SendText_PressKey);//发送消息框绑定事件  过滤按键
this.Serial_RecvText.TextChanged += new System.EventHandler(this.Serial_RecvText_TextChanged);//接收数据框绑定事件  自动下拉到最下面
this.Serial_Display_Hex.Click += new System.EventHandler(this.Serial_Display_Hex_CheckedChanged);//已hex 显示 选择框绑定事件
this.Serial_Empty.Click += new System.EventHandler(this.Serial_Empty_Click);//清空按钮绑定事件

3.串口的打开与关闭 这个也挺简单的,主要是判断当前串口是否打开,如果没有的话,就打开,并且刷新界面上的指示灯和按键内容(代码没有放上来)

private void Button_OpenPort_Click(object sender, EventArgs e)
{
    if (Serial1.IsOpen)
    {
        Serial1.Close();//关闭串口
    }
    else
    {
        Serial_Open();//读取串口设置,并打开串口
    }
    Serial_Status();//根据串口状态,修改状态指示灯和按钮内容
}

4.数据的发送(需要注意的地方就是 在发送之间,先将发送的内容转换编码形式,发送的时候用字节形式发送)

private void Serial_Send_Data_Click(object sender, EventArgs e)
{
if (Serial_SendText.Text.Length > 0)
   {
       if (Serial1.IsOpen == false)
       {
           Serial_Open();
           Serial_Status();
       }

       if (Serial_SendWithHex.Checked == false)
       {
           string TxBuff = Serial_SendText.Text;
           byte[] Tx = Encoding.GetEncoding("gb2312").GetBytes(TxBuff);
           Comm_Send_Bytes += Tx.Length;
           Serial_SendNum.Text = "发送:" + Comm_Send_Bytes.ToString();
           Serial1.Write(Tx, 0, Tx.Length);
       }
       else
       {
           string TxTemp = Serial_SendText.Text;
           byte[] Serial_SendBuff = Str2Hex(TxTemp);
           Comm_Send_Bytes += Serial_SendBuff.Length;
           Serial_SendNum.Text = "发送:" + Comm_Send_Bytes.ToString();
           Serial1.Write(Serial_SendBuff, 0, Serial_SendBuff.Length);
       }
   }
   else
   {
       MessageBox.Show("发送框不能为空!");
   }
}

5.数据接收(C# 的SerialPort 控件,只能在子进程里面运行,所以需要使用委托事件来刷新接收框中的内容,这里我是以字节形式接收,方便计算正确的长度)

private void Com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    if (Serial1.IsOpen)
    {
        try
        {
            //开辟接收缓冲区
            byte[] ReDatas = new byte[Serial1.BytesToRead];
            //从串口读取数据
            Comm_Received_Bytes += Serial1.Read(ReDatas, 0, Serial1.BytesToRead);
            //实现数据的解码与显示
            this.Invoke(updateText, ReDatas);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

6.清空接收发送区(数据太多了,看的累,有什么难的,点一点不就清空了—哈(-_-))

//清空输入输出TextBox 发送接收数据长度及label
private void Serial_Empty_Click(object sender, EventArgs e)
  {
      Comm_Received_Bytes = 0;
      Comm_Send_Bytes = 0;
      Serial_SendNum.Text = "发送:0";
      Serial_RecvNum.Text = "接收:0";
      Serial_RecvText.Text = "";
      Serial_SendText.Text = "";
      Serial_Rx_Buff_Hex = "";
      Serial_Rx_Buff_Ascii = "";
  }

7.注意事项
a.关于串口发送中文乱码的解决方法(主要就是编码的问题)
1). 使用System.Text.Encoding 类 的编码转换方法(练习时 可以试试)

//接收区
    string ReDatas = Serial1.ReadExisting();
    Comm_Received_Bytes += ReDatas.Length;
    byte[] RecvBuff = System.Text.Encoding.GetEncoding("gb2312").GetBytes(ReDatas);
    ReDatas = System.Text.Encoding.GetEncoding("gb2312").GetString(RecvBuff );
    //实现数据的解码与显示
    this.Invoke(updateText, ReDatas);

//发送区
string TxBuff = Serial_SendText.Text;
byte[] Tx = GB2312.GetBytes(TxBuff);
Comm_Send_Bytes += Tx.Length;
Serial_SendNum.Text = "发送:" + Comm_Send_Bytes.ToString();
Serial1.Write(Tx, 0, Tx.Length);

2).无脑的办法

Serial1.Encoding = System.Text.Encoding.GetEncoding("gb2312");
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值