这其实就是一个用C#写一个串口调试的工具,下面看怎么实现
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);
上面列出了主要的代码,其实就是一个串口助手。
如果觉得《读取RS485串口寄存器状态 – java – 前端》对你有帮助,请点赞、收藏,并留下你的观点哦!