網站首頁 編程語言 正文
串口通訊是一種計算機常用的數據傳輸方式。
程序運行如下:
首先,檢查計算機的串口,并獲取所有串口信息。
private void CheckPort()//檢查串口是否可用 ? ? ? ? { ? ? ? ? ? ? myLog(2, "檢測串口開始!"); ?//log記錄函數 ? ? ? ? ? ? comboBox1.Items.Clear();//清除控件中的當前值 ? ? ? ?? ? ? ? ? ? ? string[] a = SerialPort.GetPortNames(); ? ? ? ? ? ? if (a.Length != 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? for (int i = 0; i < a.Length; i++) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? comboBox1.Items.Add(a[i]); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? comboBox1.SelectedIndex = 0;//?? ? ? ? ? ? ? ? ? myLog(2, "檢測串口完成"); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? myLog(2, "無可用串口...", true); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void SetPort()//設置串口 ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? serialPort1.PortName = comboBox1.Text.Trim();//串口名給了串口類 ? ? ? ? ? ? ? ? //波特率 ? 虛擬串口的波特率好像不能修改,但得將其賦為空,不然報錯 ? ? ? ?? ? ? ? ? ? ? ? ? // serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text.ToString().Trim()); ? ? ? ? ? ? ? ? ? ? ? ? // 9600; ? ? ? ? ? ? ? ? serialPort1.BaudRate = Convert.ToInt32("".Trim()); ? ? ? ? ? ? ? ? //奇偶效驗 ? ? ? ? ? ? ? ? if (comboBox5.Text.Trim() == "奇校驗") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? serialPort1.Parity = Parity.Odd;//將奇校驗位給了sp的協議 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (comboBox5.Text.Trim() == "偶校驗") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? serialPort1.Parity = Parity.Even; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? serialPort1.Parity = Parity.None; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //停止位 ? ? ? ? ? ? ? ? if (comboBox4.Text.Trim() == "1.5") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? serialPort1.StopBits = StopBits.OnePointFive;//設置停止位有幾位 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (comboBox4.Text.Trim() == "2") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? serialPort1.StopBits = StopBits.Two; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? serialPort1.StopBits = StopBits.One; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? serialPort1.DataBits = Convert.ToInt16(comboBox3.Text.ToString().Trim());//數據位 ? ? ? ? ? ? ? ? serialPort1.Encoding = Encoding.UTF8;//串口通信的編碼格式 ? ? ? ? ? ? ? ? serialPort1.Open(); ? ? ? ? ? ? } ? ? ? ? ? ? catch { } ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// Log記錄 ? ? ? ? /// </summary> ? ? ? ? /// <param name="aa">aa= ?0為發送 ?1為接受 2為日常記錄</param> ? ? ? ? /// <param name="mystr">記錄內容</param> ? ? ? ? /// <param name="IsHint">true 顯示彈框,false 不顯示彈框 默認false</param> ? ? ? ? private void myLog(int aa, string mystr, bool IsHint = false) ? ? ? ? { ? ? ? ? ? ? //log存放路徑 ? ? ? ? ? ? string myypath = myPath + DateTime.Now.ToString("yyyyMMdd") + ".txt"; ? ? ? ? ? ? StreamWriter sw = new StreamWriter(myypath, true); ? ? ? ? ? ? string tempSendstr; ? ? ? ? ? ? if (aa == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? tempSendstr = DateTime.Now.ToString("HH:mm:ss.fff") + " ?" + serialPort1.PortName + " 發送--->>> ?" + mystr; ? ? ? ? ? ? } ? ? ? ? ? ? else if (aa == 1) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? tempSendstr = DateTime.Now.ToString("HH:mm:ss.fff") + " ?" + serialPort1.PortName + " 接收<<<--- ?" + mystr; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? tempSendstr = DateTime.Now.ToString("HH:mm:ss.fff") + " ?" + mystr; ? ? ? ? ? ? } ? ? ? ? ? ? sw.WriteLine(tempSendstr); ? ? ? ? ? ? sw.Close(); ? ? ? ? ? ? //彈框提示 ? ? ? ? ? ? if (IsHint) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show(mystr, "提示"); ? ? ? ? ? ? } ? ? ? ? ? ? if (listBox1.Items.Count >= 10) listBox1.Items.Clear(); ? ? ? ? ? ? listBox1.Items.Add(tempSendstr); ? ? ? ? }
大部分的筆記本都不帶串口的,我們可以在網上下載 Configure Virtual Serial Port Driver,給自己的筆記本添加幾對虛擬串口(虛擬串口都是成對且一一匹配的),再用串口調試助手就可以進行調試了。
數據的發送:
?//發送 private void button3_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if(!serialPort1.IsOpen) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("串口未打開,請檢查!"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? string mystr1 = textBox1.Text; ? ? ? ? ? ? byte[] a = Encoding.UTF8.GetBytes(mystr1); ? ? ? ? ? ? string mystr2 = Encoding.UTF8.GetString(a); ? ? ? ? ? ? serialPort1.Write(mystr2);//將數據寫入串行端口輸出緩沖區 ? ? ? ? ? ? myLog(0, mystr2); ? ? ? ? }
數據接收:
//串口接收數據 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) ? ? ? ? { ? ? ? ? ? ? Thread.Sleep(100); ? ? ? ? ? ? this.Invoke((EventHandler)(delegate //異步委托一個線程 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (serialPort1.BytesToRead > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? byte[] a = new byte[serialPort1.BytesToRead];//讀出緩沖區串口通信的字節 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int readbytes = 0; ? ? ? ? ? ? ? ? ? ? while (serialPort1.Read(a, readbytes, serialPort1.BytesToRead - readbytes) <= 0) ; ? ? ? ? ? ? ? ? ? ? string str = UTF8Encoding.UTF8.GetString(a); ? ? ? ? ? ? ? ? ? ? textBox2.Text = str + "\r\n"; ? ? ? ? ? ? ? ? ? ? myLog(1, str); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? })); ? ? ? ? }
這是串口之間字符串的發送與接收,如要進行十六進制的傳輸,只需要在數據發送和接收時將字符串轉化成十六進制,再寫進緩沖區或讀取。
多條信息發送時,可能會出現下面的情況。
串口調試助手將三次發送的數據一起讀出來了,此時我們只需要在發送時給上一定的延時即可。
這樣就能實現串口多條信息的發送與接收了。
原文鏈接:https://blog.csdn.net/Iawfy_/article/details/116781212
相關推薦
- 2022-02-25 antd4中Form.create已廢棄
- 2022-04-17 C語言?動態內存管理全面解析_C 語言
- 2022-04-24 詳解golang?定時任務time.Sleep和time.Tick實現結果比較_Golang
- 2022-12-13 Android?itemDecoration接口實現吸頂懸浮標題_Android
- 2022-04-15 C++中構造函數詳解_C 語言
- 2021-12-13 C++??系統IO流介紹_C 語言
- 2022-07-27 Go?error的使用方式詳解_Golang
- 2022-02-17 Centos 安裝docker 警告:正在等候 事務 鎖定 arb/rpm/.rpm.lock 正在
- 最近更新
-
- 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同步修改后的遠程分支