概述
转载连接:
http://blog.csdn.net/CoderJYF/article/details/70255251?locationNum=4&fps=1
C#串口介绍以及简单串口通信程序设计实现
周末,没事干,写个简单的串口通信工具,也算是本周末曾来过,废话不多,直接到主题
串口介绍
串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。(至于再详细,自己百度)
串口应用:
工业领域使用较多,比如:数据采集,设备控制等等,好多都是用串口通信来实现!你要是细心的话,你会发现,目前家用国网智能电能表就具备RS485通信总线(串行总线的一种)与RS232可以相互转化(当然一般,非专业的谁也不会闲的蛋疼,趴电表上瞎看,最多也就看看走了多少度电)
RS232 DB9介绍:
1.示意图
![](https://file2.kaopuke.com:8081/files_image/20230513/202305131915276501723.png)
2.针脚介绍:
- 载波检测(DCD)
- 接受数据(RXD)
- 发出数据(TXD)
- 数据终端准备好(DTR)
- 信号地线(SG)
- 数据准备好(DSR)
- 请求发送(RTS)
- 清除发送(CTS)
- 振铃指示(RI)
3.实物图:
以下是我购买XX公司的一个usb转串口线:这个头就是一个公头,另一端是一个usb口
![](https://file2.kaopuke.com:8081/files_image/20230513/202305131915275760602.png)
笨小孩串口工具运行图:
1.开启程序
![](https://file2.kaopuke.com:8081/files_image/20230513/202305131915278717377.png)
2.发送一行字符串HelloBenXH,直接将针脚的发送和接收链接起来就可以测试了(针脚2 接受数据(RXD) 和3 发出数据(TXD))直接链接,
![](https://file2.kaopuke.com:8081/files_image/20230513/202305131915283847350.png)
C#代码实现:采用SerialPort
1.实例化一个SerialPort
- private SerialPort ComDevice = new SerialPort();
2.初始化参数绑定接收数据事件
- public void init()
- {
- btnSend.Enabled = false;
- cbbComList.Items.AddRange(SerialPort.GetPortNames());
- if (cbbComList.Items.Count > 0)
- {
- cbbComList.SelectedIndex = 0;
- }
- cbbBaudRate.SelectedIndex = 5;
- cbbDataBits.SelectedIndex = 0;
- cbbParity.SelectedIndex = 0;
- cbbStopBits.SelectedIndex = 0;
- pictureBox1.BackgroundImage = Properties.Resources.red;
-
- ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);
-
- }
3.打开串口button事件
-
-
-
-
-
- private void btnOpen_Click(object sender, EventArgs e)
- {
- if (cbbComList.Items.Count <= 0)
- {
- MessageBox.Show("没有发现串口,请检查线路!");
- return;
- }
-
- if (ComDevice.IsOpen == false)
- {
- ComDevice.PortName = cbbComList.SelectedItem.ToString();
- ComDevice.BaudRate = Convert.ToInt32(cbbBaudRate.SelectedItem.ToString());
- ComDevice.Parity = (Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString());
- ComDevice.DataBits = Convert.ToInt32(cbbDataBits.SelectedItem.ToString());
- ComDevice.StopBits = (StopBits)Convert.ToInt32(cbbStopBits.SelectedItem.ToString());
- try
- {
- ComDevice.Open();
- btnSend.Enabled = true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- btnOpen.Text = "关闭串口";
- pictureBox1.BackgroundImage = Properties.Resources.green;
- }
- else
- {
- try
- {
- ComDevice.Close();
- btnSend.Enabled = false;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- btnOpen.Text = "打开串口";
- pictureBox1.BackgroundImage = Properties.Resources.red;
- }
-
- cbbComList.Enabled = !ComDevice.IsOpen;
- cbbBaudRate.Enabled = !ComDevice.IsOpen;
- cbbParity.Enabled = !ComDevice.IsOpen;
- cbbDataBits.Enabled = !ComDevice.IsOpen;
- cbbStopBits.Enabled = !ComDevice.IsOpen;
- }
4.发送数据
-
-
-
-
-
- public bool SendData(byte[] data)
- {
- if (ComDevice.IsOpen)
- {
- try
- {
- ComDevice.Write(data, 0, data.Length);
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- else
- {
- MessageBox.Show("串口未打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- return false;
- }
-
-
-
-
-
-
- private void btnSend_Click(object sender, EventArgs e)
- {
- byte[] sendData = null;
-
- if (rbtnSendHex.Checked)
- {
- sendData = strToHexByte(txtSendData.Text.Trim());
- }
- else if (rbtnSendASCII.Checked)
- {
- sendData = Encoding.ASCII.GetBytes(txtSendData.Text.Trim());
- }
- else if (rbtnSendUTF8.Checked)
- {
- sendData = Encoding.UTF8.GetBytes(txtSendData.Text.Trim());
- }
- else if (rbtnSendUnicode.Checked)
- {
- sendData = Encoding.Unicode.GetBytes(txtSendData.Text.Trim());
- }
- else
- {
- sendData = Encoding.ASCII.GetBytes(txtSendData.Text.Trim());
- }
-
- if (this.SendData(sendData))
- {
- lblSendCount.Invoke(new MethodInvoker(delegate
- {
- lblSendCount.Text = (int.Parse(lblSendCount.Text) + txtSendData.Text.Length).ToString();
- }));
- }
- else
- {
-
- }
-
- }
-
-
-
-
-
-
- private byte[] strToHexByte(string hexString)
- {
- hexString = hexString.Replace(" ", "");
- if ((hexString.Length % 2) != 0)
- hexString += " ";
- byte[] returnBytes = new byte[hexString.Length / 2];
- for (int i = 0; i < returnBytes.Length; i++)
- returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ",""), 16);
- return returnBytes;
- }
5.接收和数据输出
-
-
-
-
-
- private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- byte[] ReDatas = new byte[ComDevice.BytesToRead];
- ComDevice.Read(ReDatas, 0, ReDatas.Length);
- this.AddData(ReDatas);
- }
-
-
-
-
-
- public void AddData(byte[] data)
- {
- if (rbtnHex.Checked)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < data.Length; i++)
- {
- sb.AppendFormat("{0:x2}" + " ", data[i]);
- }
- AddContent(sb.ToString().ToUpper());
- }
- else if (rbtnASCII.Checked)
- {
- AddContent(new ASCIIEncoding().GetString(data));
- }
- else if (rbtnUTF8.Checked)
- {
- AddContent(new UTF8Encoding().GetString(data));
- }
- else if (rbtnUnicode.Checked)
- {
- AddContent(new UnicodeEncoding().GetString(data));
- }
- else
- {}
-
- lblRevCount.Invoke(new MethodInvoker(delegate
- {
- lblRevCount.Text = (int.Parse(lblRevCount.Text) + data.Length).ToString();
- }));
- }
-
-
-
-
-
-
- private void AddContent(string content)
- {
- this.BeginInvoke(new MethodInvoker(delegate
- {
- if(chkAutoLine.Checked && txtShowData.Text.Length>0)
- {
- txtShowData.AppendText("rn");
- }
- txtShowData.AppendText(content);
- }));
- }
6.清空数据区域事件
-
-
-
-
-
- private void btnClearRev_Click(object sender, EventArgs e)
- {
- txtShowData.Clear();
- }
-
-
-
-
-
-
- private void btnClearSend_Click(object sender, EventArgs e)
- {
- txtSendData.Clear();
- }
最后
以上就是发嗲御姐为你收集整理的C# 串口介绍以及简单串口通信程序设计实现 C#串口介绍以及简单串口通信程序设计实现的全部内容,希望文章能够帮你解决C# 串口介绍以及简单串口通信程序设计实现 C#串口介绍以及简单串口通信程序设计实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复