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ū)中同步讀取一個(gè)字節(jié)
- ReadChar(): 從SerialPort輸入緩沖區(qū)中同步讀取一個(gè)字符
- 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#設(shè)計(jì)模式之單例模式的實(shí)現(xiàn)方式
這篇文章主要給大家介紹了關(guān)于c#設(shè)計(jì)模式之單例模式的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
C#使用xsd文件驗(yàn)證XML格式是否正確的實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用xsd文件驗(yàn)證XML格式是否正確的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了C#針對xml文件的創(chuàng)建、驗(yàn)證相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
c#中的浮點(diǎn)型轉(zhuǎn)整形的舍取 四舍五入和銀行家舍入實(shí)現(xiàn)代碼
c#中的浮點(diǎn)型轉(zhuǎn)整形的舍取 四舍五入和銀行家舍入實(shí)現(xiàn)代碼,學(xué)習(xí)c#的朋友可以參考下2012-03-03
Unity UGUI的RectMask2D遮罩組件的介紹使用
這篇文章主要為大家介紹了Unity UGUI的RectMask2D遮罩組件的介紹使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C# ODP.NET 調(diào)用Oracle函數(shù)返回值時(shí)報(bào)錯(cuò)的一個(gè)解決方案
這篇文章主要介紹了C# ODP.NET 調(diào)用Oracle函數(shù)返回值時(shí)報(bào)錯(cuò)的一個(gè)解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

