網站首頁 編程語言 正文
從Microsoft .Net 2.0版本以后,就默認提供了System.IO.Ports.SerialPort類,用戶可以非常簡單地編寫少量代碼就完成串口的信息收發程序。
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);
}
還有一種通過調用API的方法來獲取實現,可以獲取詳細的完整串口名稱,對于USB-to-COM虛擬串口來說特別適用。
3、串口屬性參數設置
SerialPort mySerialPort = new SerialPort("COM2");//端口
mySerialPort.BaudRate = 9600;//波特率
mySerialPort.Parity = Parity.None;//校驗位
mySerialPort.StopBits = StopBits.One;//停止位
mySerialPort.DataBits = 8;//數據位
mySerialPort.Handshake = Handshake.Non;
mySerialPort.ReadTimeout = 1500;
mySerialPort.DtrEnable = true;//啟用數據終端就緒信息
mySerialPort.Encoding = Encoding.UTF8;
mySerialPort.ReceivedBytesThreshold = 1;//DataReceived觸發前內部輸入緩沖器的字節數
mySerialPort.DataReceived += new SerialDataReceivedEvenHandler(DataReceive_Method);
mySerialPort.Open();
4、串口發送信息
- Write(Byte[], Int32, Int32)?:將指定數量的字節寫入串行端口
- Write(Char[], Int32, Int32)?:將指定數量的字符寫入串行端口
- Write(String)?:將指定的字符串寫入串行端口
- WriteLine(String)?:將指定的字符串和NewLine值寫入輸出緩沖區
// 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輸入緩沖區讀取一些字節,并將那些字節寫入字節數組中指定的偏移量處
- ReadByte():從SerialPort輸入緩沖區中同步讀取一個字節
- ReadChar(): 從SerialPort輸入緩沖區中同步讀取一個字符
- ReadExisting()?:在編碼的基礎上,讀取SerialPort對象的流和輸入緩沖區中所有立即可用的字節
- ReadLine()?:一直讀取到輸入緩沖區中的NewLine值
- ReadTo(String)?:一直讀取到輸入緩沖區中的指定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、循環接收數據
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
}
原文鏈接:https://www.cnblogs.com/springsnow/p/12975487.html
相關推薦
- 2022-12-05 Spark中的數據讀取保存和累加器實例詳解_相關技巧
- 2022-05-31 python字典中get()函數的基本用法實例_python
- 2022-08-14 使用Composing?builds提升Android編譯速度_Android
- 2023-01-10 C#固定大小緩沖區及使用指針復制數據詳解_C#教程
- 2022-11-13 Python?fileinput模塊應用詳解_python
- 2022-07-17 一起詳細聊聊C#中的Visitor模式_C#教程
- 2022-09-03 Go實現替換(覆蓋)文件某一行內容的示例代碼_Golang
- 2024-03-18 JedisDataException: READONLY You can‘t write again
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支