網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了C#基于SerialPort類實現(xiàn)串口通訊的具體代碼,供大家參考,具體內(nèi)容如下
最終效果
窗體設(shè)置:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Ports; using System.Threading; using System.Windows.Forms; namespace SerialCommunication { ? ? public partial class Form1 : Form ? ? { ? ? ? ? System.Threading.SynchronizationContext m_SyncContext = null; ? ? ? ? SerialPort serialPort = null; ? ? ? ? ToHexadecimalString toHexadecimalString = new ToHexadecimalString(); ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? m_SyncContext = SynchronizationContext.Current; ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? //Data initialization ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? string[] ports = SerialPort.GetPortNames(); ? ? ? ? ? ? SerialPortNumber.Items.AddRange(ports); ? ? ? ? ? ? SerialPortNumber.SelectedIndex = SerialPortNumber.Items.Count > 0 ? 0 : -1; ? ? ? ? ? ? BaudRateCom.Text = "9600"; ?//Set parameters ? ? ? ? ? ? CheckBitCom.Text = "None"; ? ? ? ? ? ? DataBitCom.Text = "8"; ? ? ? ? ? ? StopBitCom.Text = "One"; ? ? ? ? } ? ? ? ? ? ? //Send data ? ? ? ? ?private void Send_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //Send data and handle exceptions ? ? ? ? ? ? ? ? byte[] bytes = new byte[SendMessage.Text.Length]; ? ? ? ? ? ? ? ? bytes = Encoding.Default.GetBytes(SendMessage.Text); ? ? ? ? ? ? ? ? serialPort.Write(bytes, 0, bytes.Length); ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception error) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show(error.Message); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? ? ? ? // Byte to hexadecimal string? ? ? ? ? //Clear data ? ? ? ? private void Clear_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? ReceiveMessage.Text = ""; ? ? ? ? } ? ? ? ? private void Open_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? string portName = SerialPortNumber.Text; ? ? ? ? ? ? int buadRate = int.Parse(BaudRateCom.Text); ? ? ? ? ? ? Parity parity = 0; ? ? ? ? ? ? switch (CheckBitCom.Text) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? case "None": ? ? ? ? ? ? ? ? ? ? parity = Parity.None; break; ? ? ? ? ? ? ? ? case "Odd": ? ? ? ? ? ? ? ? ? ? parity = Parity.Odd; break; ? ? ? ? ? ? ? ? case "Even": ? ? ? ? ? ? ? ? ? ? parity = Parity.Even; break; ? ? ? ? ? ? ? ? case "Mark": ? ? ? ? ? ? ? ? ? ? parity = Parity.Mark; break; ? ? ? ? ? ? ? ? case "Space": ? ? ? ? ? ? ? ? ? ? parity = Parity.Space; break; ? ? ? ? ? ? } ? ? ? ? ? ? int dataBit = int.Parse(DataBitCom.Text); ? ? ? ? ? ? StopBits stopBits = 0; ? ? ? ? ? ? switch (StopBitCom.Text) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? case "None": ? ? ? ? ? ? ? ? ? ? stopBits = StopBits.None; break; ? ? ? ? ? ? ? ? case "One": ? ? ? ? ? ? ? ? ? ? stopBits = StopBits.One; break; ? ? ? ? ? ? ? ? case "Tow": ? ? ? ? ? ? ? ? ? ? stopBits = StopBits.Two; break; ? ? ? ? ? ? ? ? case "OnePointFive": ? ? ? ? ? ? ? ? ? ? stopBits = StopBits.OnePointFive; break; ? ? ? ? ? ? } ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (Open.Text == "Open") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? serialPort = new SerialPort(portName, buadRate, parity, dataBit, stopBits); ? ? ? ? ? ? ? ? ? ? serialPort.Open(); ? ? ? ? ? ? ? ? ? ? Open.Text = "Close"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (Open.Text == "Close") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? serialPort.Close(); ? ? ? ? ? ? ? ? ? ? Open.Text = "Open"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception errror) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show(errror.Message); ? ? ? ? ? ? } ? ? ? ? ? ? serialPort.DataReceived += onDataReceived; ? ? ? ? } ? ? ? ? private void onDataReceived(object sender, SerialDataReceivedEventArgs e) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Byte[] bytes = new byte[serialPort.BytesToRead]; ? ? ? ? ? ? ? ? serialPort.Read(bytes, 0, serialPort.BytesToRead); ? ? ? ? ? ? ? ? m_SyncContext.Post(new SendOrPostCallback((obj) => ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ReceiveMessage.AppendText(Encoding.Default.GetString(bytes)); ? ? ? ? ? ? ? ? }), bytes); ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception error) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show(error.Message); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
原文鏈接:https://blog.csdn.net/qq_43069920/article/details/119509012
相關(guān)推薦
- 2022-07-09 Android開發(fā)Dart?Constructors構(gòu)造函數(shù)使用技巧整理_Android
- 2022-11-16 Python中Pygame模塊的詳細(xì)安裝過程_python
- 2022-01-12 出現(xiàn)Logging initialized using ‘class org.apache.ibat
- 2022-11-06 python?pandas?數(shù)據(jù)排序的幾種常用方法_python
- 2022-05-16 C語言中const和define的區(qū)別你了解嘛_C 語言
- 2022-01-22 Redis學(xué)習(xí)之旅--與SpringBoot的結(jié)合
- 2023-01-19 利用Python構(gòu)建Flutter應(yīng)用的教程詳解_python
- 2022-08-23 Python中應(yīng)用Winsorize縮尾處理的操作經(jīng)驗_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支