在.NET work 2.0中提供了SerialPort类,该类主要实现串口数据通信等。本文章将本人在学习过程中从网络上搜集到的相关信息写出来供大家参考。 下面主要介绍该类的主要属性(表1)和方法(表.2)。 如果需要了解更多的信息请登录http://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport(VS.80).aspx查看。 相关文章 《使用System.IO.Ports读取COM口数据》 http://www.devasp.net/net/articles/display/727.html 使用SerialPort类的方法: 方法一: 首先要添加 using System.IO; 1...在类的内部定义SerialPort com; 2...打开串口 com = new SerialPort(); 3...发送数据 Byte[] TxData ={1,2,3,4,5,6,7,8 }; 4...接收数据 4.1使用事件接收 this.com.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.OnDataReceived); private void OnDataReceived(object sender, SerialDataReceivedEventArgs e) 4.2使用线程接收 接收数据启动一个线程,使其接收。 在类的内部定义 Thread _readThread; 打开串口后启动线程 _keepReading = true; 线程函数 view plaincopy to clipboardprint? 方法二:使用C#自带的SerialPor控件。 1...在“工具箱”的“组件”中选择SerialPor控件添加。 2...设置串口并打开 serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.Open(); 3...写入数据可以使用Write或者下面的函数 serialPort1.WriteLine(str); 4...添加数据接收的事件 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 使用中的一些常见问题 C#中SerialPort类中DataReceived事件GUI实时处理方法(来自wanglei_wan@yahoo.com.cn 的看法) 如何知道当前电脑有哪个串口 在窗体上添加一个comboBox控件。 然后使用comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames()); 或者 string[] portList = System.IO.Ports.SerialPort.GetPortNames(); |
在C#中使用SerialPort类实现串口通信 遇到多线程问题
在C#中使用SerialPort类实现串口通信。
2009年11月01日 星期日 10:03