C#如何实现串口通信
这篇文章主要讲解了“C#如何实现串口通信”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#如何实现串口通信”吧!
1.基本概念
2.前端winForm布局如下(仅仅为了实现功能,布局略丑)
3.代码实现如下
namespaceSerialPortTest{publicpartialclassForm1:Form{SerialPortsp1=newSerialPort();publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){ //分别对应前端的波特率、数字位、校验位、停止位cbBaudRate.SelectedIndex=0;cbDataBits.SelectedIndex=0;cbCheck.SelectedIndex=0;cbStop.SelectedIndex=0;string[]strCom=SerialPort.GetPortNames();if(strCom==null){MessageBox.Show("本机没有串口!","Error");return;}//GetPortNames()方法:获取当前计算机的串行端口名的数组foreach(stringcominSystem.IO.Ports.SerialPort.GetPortNames()){cbCom.Items.Add(com);}cbCom.SelectedIndex=0;sp1.BaudRate=9600;Control.CheckForIllegalCrossThreadCalls=false;sp1.DataReceived+=Sp1_DataReceived;sp1.DtrEnable=true;//获取或设置一个值,该值在串行通信过程中启用数据终端就绪(DTR)信号。sp1.RtsEnable=true;//获取或设置一个值,该值指示在串行通信中是否启用请求发送(RTS)信号//设置数据读取超时为1秒sp1.ReadTimeout=1000;sp1.Close();}privatevoidSp1_DataReceived(objectsender,SerialDataReceivedEventArgse){if(sp1.IsOpen)//判断是否打开串口{//输出当前时间DateTimedt=DateTime.Now;txtReceived.Text+=dt.GetDateTimeFormats('f')[0].ToString()+"\r\n";try{Byte[]receivedData=newByte[sp1.BytesToRead];//创建接收字节数组sp1.Read(receivedData,0,receivedData.Length);//读取数据AddContent(newUTF8Encoding().GetString(receivedData));//用万能的UTF8可以传输中文不会乱码}catch(System.Exceptionex){MessageBox.Show(ex.Message,"出错提示!!!!!");txtSendStr.Text="";}}else{MessageBox.Show("请打开某个串口","错误提示");}}//将接受到的内容显示出来privatevoidAddContent(stringcontent){this.BeginInvoke(newMethodInvoker(delegate{txtReceived.AppendText(content);txtReceived.AppendText("\r\n");//记录收到的字符个数lblRevCount.Text=(int.Parse(lblRevCount.Text)+content.Length).ToString();}));}privatevoidbtnOpen_Click(objectsender,EventArgse){//serialPort1.IsOpenif(!sp1.IsOpen){try{//设置串口号stringserialName=cbCom.SelectedItem.ToString();sp1.PortName=serialName;//设置各“串口设置”stringstrBaudRate=cbBaudRate.Text;stringstrDateBits=cbDataBits.Text;stringstrStopBits=cbStop.Text;Int32iBaudRate=Convert.ToInt32(strBaudRate);Int32iDateBits=Convert.ToInt32(strDateBits);sp1.BaudRate=iBaudRate;//波特率sp1.DataBits=iDateBits;//数据位switch(cbStop.Text)//停止位{case"1":sp1.StopBits=StopBits.One;break;case"1.5":sp1.StopBits=StopBits.OnePointFive;break;case"2":sp1.StopBits=StopBits.Two;break;default:MessageBox.Show("Error:参数不正确!","Error");break;}switch(cbCheck.Text)//校验位{case"无":sp1.Parity=Parity.None;break;case"奇校验":sp1.Parity=Parity.Odd;break;case"偶校验":sp1.Parity=Parity.Even;break;default:MessageBox.Show("Error:参数不正确!","Error");break;}if(sp1.IsOpen==true)//如果打开状态,则先关闭一下{sp1.Close();}//设置必要控件不可用cbCom.Enabled=false;cbBaudRate.Enabled=false;cbDataBits.Enabled=false;cbStop.Enabled=false;cbCheck.Enabled=false;sp1.Open();//打开串口btnOpen.Text="关闭串口";}catch(System.Exceptionex){MessageBox.Show("Error:"+ex.Message,"Error");return;}}else{//恢复控件功能//设置必要控件不可用cbCom.Enabled=true;cbBaudRate.Enabled=true;cbDataBits.Enabled=true;cbStop.Enabled=true;cbCheck.Enabled=true;sp1.Close();//关闭串口btnOpen.Text="打开串口";}}privatevoidbtnSend_Click(objectsender,EventArgse){byte[]sendData=null;if(!sp1.IsOpen)//如果没打开{MessageBox.Show("请先打开串口!","Error");return;}StringstrSend=txtSendStr.Text;try{sendData=Encoding.UTF8.GetBytes(txtSendStr.Text.Trim());//sp1.WriteLine(txtSendStr.Text);//写入数据sp1.Write(sendData,0,sendData.Length);}catch(Exceptionex){MessageBox.Show("Error:"+ex.Message,"Error");}}}}
4.测试运行结果如下
在自己同一台电脑上测试,需要先用Configure Virtual Serial Port Driver建立两个虚拟串口,如下
串口运行结果如下:
上述两窗体通信时要选择同一波特率,不然收发数据会失败
关于C# serialport的一些说明:
SerialPort() :如果未指定,则此构造函数使用默认属性值。 例如, DataBits 属性默认值为 8, Parity 属性默认为 None 枚举值,
StopBits 属性默认值为 1,默认端口名为 COM1。
public static string[] GetPortNames() :获取当前计算机的串行端口名的数组
SerialPort.Read 方法 (Byte[], Int32, Int32) :从 SerialPort 输入缓冲区读取一些字节并将那些字节写入字节数组中指定的偏移量处
SerialPort.ReadLine 方法 () :一直读取到输入缓冲区中的 NewLine 值
SerialPort.Write 方法 (Byte[], Int32, Int32) : 使用缓冲区中的数据将指定数量的字节写入串行端口
SerialPort.WriteLine 方法 (String) : 将指定的字符串和 NewLine 值写入输出缓冲区。
感谢各位的阅读,以上就是“C#如何实现串口通信”的内容了,经过本文的学习后,相信大家对C#如何实现串口通信这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。