C#串口編程System.IO.Ports.SerialPort類
從Microsoft .Net 2.0版本以后,就默認(rèn)提供了System.IO.Ports.SerialPort類,用戶可以非常簡單地編寫少量代碼就完成串口的信息收發(fā)程序。
1. 串口硬件信號定義
DB9 Connector 信號定義。串口測試將2、3針腳短接即可。
2、串口端口號搜索
string[] portList = System.IO.Ports.SerialPort.GetPortNames(); for (int i = 0; i < portList.Length; i++) { string name = portList[i]; comboBox.Items.Add(name); }
還有一種通過調(diào)用API的方法來獲取實(shí)現(xiàn),可以獲取詳細(xì)的完整串口名稱,對于USB-to-COM虛擬串口來說特別適用。
3、串口屬性參數(shù)設(shè)置
SerialPort mySerialPort = new SerialPort("COM2");//端口 mySerialPort.BaudRate = 9600;//波特率 mySerialPort.Parity = Parity.None;//校驗(yàn)位 mySerialPort.StopBits = StopBits.One;//停止位 mySerialPort.DataBits = 8;//數(shù)據(jù)位 mySerialPort.Handshake = Handshake.Non; mySerialPort.ReadTimeout = 1500; mySerialPort.DtrEnable = true;//啟用數(shù)據(jù)終端就緒信息 mySerialPort.Encoding = Encoding.UTF8; mySerialPort.ReceivedBytesThreshold = 1;//DataReceived觸發(fā)前內(nèi)部輸入緩沖器的字節(jié)數(shù) mySerialPort.DataReceived += new SerialDataReceivedEvenHandler(DataReceive_Method); mySerialPort.Open();
4、串口發(fā)送信息
- Write(Byte[], Int32, Int32) :將指定數(shù)量的字節(jié)寫入串行端口
- Write(Char[], Int32, Int32) :將指定數(shù)量的字符寫入串行端口
- Write(String) :將指定的字符串寫入串行端口
- WriteLine(String) :將指定的字符串和NewLine值寫入輸出緩沖區(qū)
// Write a string port.Write("Hello World"); // Write a set of bytes port.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3); // Close the port port.Close();
5. 串口接收信息
- Read(Byte[], Int32, Int32):從SerialPort輸入緩沖區(qū)讀取一些字節(jié),并將那些字節(jié)寫入字節(jié)數(shù)組中指定的偏移量處
- ReadByte():從SerialPort輸入緩沖區(qū)中同步讀取一個字節(jié)
- ReadChar(): 從SerialPort輸入緩沖區(qū)中同步讀取一個字符
- ReadExisting() :在編碼的基礎(chǔ)上,讀取SerialPort對象的流和輸入緩沖區(qū)中所有立即可用的字節(jié)
- ReadLine() :一直讀取到輸入緩沖區(qū)中的NewLine值
- ReadTo(String) :一直讀取到輸入緩沖區(qū)中的指定value的字符串
string serialReadString; private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { serialReadString = port.ReadExisting()); this.txt1.Invoke( new MethodInvoker(delegate { this.txt1.AppendText(serialReadString); })); }
6、循環(huán)接收數(shù)據(jù)
void com_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Use either the binary OR the string technique (but not both) // Buffer and process binary data while (com.BytesToRead > 0) bBuffer.Add((byte)com.ReadByte()); ProcessBuffer(bBuffer); // Buffer string data sBuffer += com.ReadExisting(); ProcessBuffer(sBuffer); } private void ProcessBuffer(string sBuffer) { // Look in the string for useful information // then remove the useful data from the buffer } private void ProcessBuffer(List<byte> bBuffer) { // Look in the byte array for useful information // then remove the useful data from the buffer }
到此這篇關(guān)于C#串口編程System.IO.Ports.SerialPort類的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
探討C#中Dispose方法與Close方法的區(qū)別詳解
本篇文章是對C#中Dispose方法與Close方法的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C#運(yùn)用FileInfo類實(shí)現(xiàn)拷貝文件的方法
這篇文章主要介紹了C#運(yùn)用FileInfo類實(shí)現(xiàn)拷貝文件的方法,需要的朋友可以參考下2014-07-07Unity ScrollRect實(shí)現(xiàn)軌跡滑動效果
這篇文章主要為大家詳細(xì)介紹了Unity ScrollRect實(shí)現(xiàn)軌跡滑動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09.NET/C# 使用Stopwatch測量運(yùn)行時間
這篇文章主要介紹了.NET/C# 使用Stopwatch測量運(yùn)行時間,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01