C#實現(xiàn)簡單串口通信
串口通信(Serial Communications)是指外設(shè)和計算機間通過數(shù)據(jù)信號線、地線等按位(bit)進行傳輸數(shù)據(jù)的一種通信方式,屬于串行通信方式,能夠?qū)崿F(xiàn)遠距離通信,長度可達1200米。盡管比按字節(jié)(byte)的并行通信慢,但是串口可以在使用一根線發(fā)送數(shù)據(jù)的同時用另一根線接收數(shù)據(jù)。串口通信最重要的參數(shù)是波特率、數(shù)據(jù)位、停止位和奇偶校驗。對于兩個進行通信的端口,這些參數(shù)必須匹配。
串口通信的接口標準有很多,有 RS-232C、RS-232、RS-422A、RS-485 等。常用的就是 RS-232 和 RS-485。串口通信使用的大多都是 DB9 接口,如下圖。
1 載波檢測(DCD) 2 接受數(shù)據(jù)(RXD) 3 發(fā)出數(shù)據(jù)(TXD) 4 數(shù)據(jù)終端準備好(DTR) 5 信號地線(SG) 6 數(shù)據(jù)準備好(DSR) 7 請求發(fā)送(RTS) 8 清除發(fā)送(CTS) 9 振鈴指示(RI)
這里我們以 RS-232 接口進行演示。
1、數(shù)據(jù)包格式定為(10bytes):
幀頭(0xAA,0x55),命令字(1byte),地址位(2bytes),數(shù)據(jù)位(2bytes),校驗位(1byte,和校驗),幀尾(0xFE,0xFE)
地址位和數(shù)據(jù)位都是高位在前。
數(shù)據(jù)封裝方法:
//數(shù)據(jù)打包 private byte[] DataPackage(byte cmd, int addr, int data) ? ? ? ? { ? ? ? ? ? ? byte[] package = new byte[10]; ? ? ? ? ? ? package[0] = 0xAA;//幀頭 ? ? ? ? ? ? package[1] = 0x55; ? ? ? ? ? ? package[2] = cmd;//命令字 ? ? ? ? ? ? ? ? byte[] dataaddr = IntToByteArray(addr); ? ? ? ? ? ? package[3] = dataaddr[0];//地址高字節(jié) ? ? ? ? ? ? package[4] = dataaddr[1];//地址低字節(jié) ? ? ? ? ? ? byte[] value = IntToByteArray(data); ? ? ? ? ? ? package[5] = value[0];//數(shù)據(jù)高字節(jié) ? ? ? ? ? ? package[6] = value[1];//數(shù)據(jù)低字節(jié) ? ? ? ? ? ? package[7] = CheckSum(package);//校驗位 ? ? ? ? ? ? package[8] = 0xFE;//幀尾 ? ? ? ? ? ? package[9] = 0xFE; ? ? ? ? ? ? return package; ? ? ? ? } ? ? ? ? ? //將int轉(zhuǎn)換成2位數(shù)組 ? ? ? ? private static byte[] IntToByteArray(int value) ? ? ? ? { ? ? ? ? ? ? int hvalue = (value >> 8) & 0xFF; ? ? ? ? ? ? int lValue = value & 0xFF; ? ? ? ? ? ? byte[] arr = new byte[] { (byte)hvalue, (byte)lValue }; ? ? ? ? ? ? return arr; ? ? ? ? } ? ? ? ? ? //得到和校驗碼 ? ? ? ? private byte CheckSum(byte[] package) ? ? ? ? { ? ? ? ? ? ? byte checksum = 0; ? ? ? ? ? ? for (int i = 0; i < package.Length; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? checksum += package[i]; ? ? ? ? ? ? } ? ? ? ? ? ? return checksum; ? ? ? ? }
2、串口調(diào)用封裝類CommHelper.cs
internal class CommHelper ? ? { ? ? ? ? //委托 ? ? ? ? public delegate void EventHandle(byte[] readBuffer); ? ? ? ? public event EventHandle DataReceived; ? ? ? ? ? public SerialPort serialPort; ? ? ? ? private Thread thread; ? ? ? ? volatile bool _keepReading; ? ? ? ? ? public CommHelper() ? ? ? ? { ? ? ? ? ? ? serialPort = new SerialPort(); ? ? ? ? ? ?? ? ? ? ? ? ? thread = null; ? ? ? ? ? ? _keepReading = false; ? ? ? ? ? ? ? serialPort.ReadTimeout = -1; ? ? ? ? ? ? serialPort.WriteTimeout = 1000; ? ? ? ? } ? ? ? ? ? //獲取串口打開狀態(tài) ? ? ? ? public bool IsOpen ? ? ? ? { ? ? ? ? ? ? get ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return serialPort.IsOpen; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //開始讀取 ? ? ? ? private void StartReading() ? ? ? ? { ? ? ? ? ? ? if (!_keepReading) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? _keepReading = true; ? ? ? ? ? ? ? ? thread = new Thread(new ThreadStart(ReadPort)); ? ? ? ? ? ? ? ? thread.Start(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //停止讀取 ? ? ? ? private void StopReading() ? ? ? ? { ? ? ? ? ? ? if (_keepReading) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? _keepReading = false; ? ? ? ? ? ? ? ? thread.Join(); ? ? ? ? ? ? ? ? thread = null; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //讀數(shù)據(jù) ? ? ? ? private void ReadPort() ? ? ? ? { ? ? ? ? ? ? while (_keepReading) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (serialPort.IsOpen) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? int count = serialPort.BytesToRead; ? ? ? ? ? ? ? ? ? ? if (count > 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? byte[] readBuffer = new byte[count]; ? ? ? ? ? ? ? ? ? ? ? ? try ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Application.DoEvents(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? serialPort.Read(readBuffer, 0, count); ? ? ? ? ? ? ? ? ? ? ? ? ? ? DataReceived?.Invoke(readBuffer); ? ? ? ? ? ? ? ? ? ? ? ? ? ? Thread.Sleep(100); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? catch (TimeoutException) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //寫數(shù)據(jù) ? ? ? ? public void WritePort(byte[] send, int offSet, int count) ? ? ? ? { ? ? ? ? ? ? if (IsOpen) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? serialPort.Write(send, offSet, count); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //打開 ? ? ? ? public void Open() ? ? ? ? { ? ? ? ? ? ? Close(); ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? serialPort.Open(); ? ? ? ? ? ? ? ? serialPort.RtsEnable = true; ? ? ? ? ? ? ? ? if (serialPort.IsOpen) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? StartReading(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("串口打開失??!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch ? ? ? ? ? ? { ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //關(guān)閉 ? ? ? ? public void Close() ? ? ? ? { ? ? ? ? ? ? StopReading(); ? ? ? ? ? ? serialPort.Close(); ? ? ? ? ? ? ? ? ? ? } ? ? }
3、調(diào)用(模擬測試讀寫硬件版本號)
//地址存儲信息定義 private static int HARD_VERSION = 0;?? ??? ? ? ?//硬件版本號 ? ? ? ? //命令定義 private static byte RD_HV = 0;?? ??? ??? ? ? ?//讀硬件版本 private static byte WR_HV = 1;?? ??? ??? ? ? ?//寫硬件版本 ? private CommHelper comm = new CommHelper(); private bool GetCorrect = false; //用來標識是否已經(jīng)接收到返回值 ? private List<byte> buffer = new List<byte>(1024);//默認分配1M內(nèi)存,限制不能超過 private byte[] binary_data = new byte[10];//指定格式的某個完整數(shù)據(jù) ? //串口初始化 private void InitComm() ? ? ? ? { ? ? ? ? ? ? comm.serialPort.PortName = "COM1"; ? ? ? ? ? ? comm.serialPort.BaudRate = 19200; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? comm.Open(); ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show(ex.Message); ? ? ? ? ? ? } ? ? ? ? ? ? if (comm.IsOpen) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? comm.DataReceived += new CommHelper.EventHandle(comm_DataReceived); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //數(shù)據(jù)接收后處理 ? ? ? ? private void comm_DataReceived(byte[] readBuffer) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? buffer.AddRange(readBuffer);//將數(shù)據(jù)放到緩存中 ? ? ? ? ? ? ? ? //完整性判斷 ? ? ? ? ? ? ? ? while (buffer.Count >= 10) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //查找數(shù)據(jù)頭 ? ? ? ? ? ? ? ? ? ? if (buffer[0] == 0xAA && buffer[1] == 0x55) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //和校驗 ? ? ? ? ? ? ? ? ? ? ? ? byte checksum = 0; ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < 7; i++) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? checksum += buffer[i]; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? if (checksum != buffer[7]) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? buffer.RemoveRange(0, 10);//如果校驗失敗,從緩存中刪除這一包數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? buffer.CopyTo(0, binary_data, 0, 10);//復制一條完整的數(shù)據(jù)到具體的數(shù)據(jù)緩存 ? ? ? ? ? ? ? ? ? ? ? ? ? //讀取硬件版本號 ? ? ? ? ? ? ? ? ? ? ? ? if (binary_data[2] == RD_HV && ByteArrayToInt(binary_data[3], binary_data[4]) == HARD_VERSION)//命令字為讀RD_HV,地址HARD_VERSION ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? GetCorrect = true; ? ? ? ? ? ? ? ? ? ? ? ? ? ? string data = ByteArrayToString(binary_data[5], binary_data[6]); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //這里可以處理數(shù)據(jù)更新界面展示 ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? buffer.RemoveRange(0, 10); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? GetCorrect = false; ? ? ? ? ? ? ? ? ? ? ? ? buffer.RemoveAt(0);//如果數(shù)據(jù)開始不是頭,則刪除數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch ? ? ? ? ? ? { ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //將2位數(shù)組轉(zhuǎn)換成字符串 ? ? ? ? private static string ByteArrayToString(byte arr1, byte arr2) ? ? ? ? { ? ? ? ? ? ? int value = ByteArrayToInt(arr1, arr2); ? ? ? ? ? ? string str = Convert.ToString(value, 10); ? ? ? ? ? ? return str; ? ? ? ? } ? ? ? ? ? //將2位數(shù)組轉(zhuǎn)換成10進制數(shù) ? ? ? ? private static int ByteArrayToInt(byte arr1, byte arr2) ? ? ? ? { ? ? ? ? ? ? byte[] arr = new byte[] { arr1, arr2 }; ? ? ? ? ? ? int value = (int)((arr[0] & 0xFF) << 8) ? ? ? ? ? ? ? ? ? ? ? | (arr[1] & 0xFF); ? ? ? ? ? ? return value; ? ? ? ? } ? ? ? ? ? //讀取硬件版本號 ? ? ? ? private void ReadHV() ? ? ? ? { ? ? ? ? ? ? GetCorrect = false; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? comm.WritePort(DataPackage(RD_HV, HARD_VERSION, 0), 0, 10);//發(fā)送RD_E2P命令,地址HARD_VERSION ? ? ? ? ? ? ? ? Delay(100); ? ? ? ? ? ? ? ? if (GetCorrect == true) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("硬件版本號讀取成功!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("硬件版本號讀取失敗!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show(ex.Message); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //寫入硬件版本號 ? ? ? ? private void WriteHV() ? ? ? ? { ? ? ? ? ? ? if (tbVersion.Text.Length == 4 && isLegal(tbVersion.Text))//限定只能寫入4個字符且輸入合法 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? try ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? int buf = Convert.ToInt32(tbVersion.Text); ? ? ? ? ? ? ? ? ? ? GetCorrect = false; ? ? ? ? ? ? ? ? ? ? comm.WritePort(DataPackage(WR_HV, HARD_VERSION, buf), 0, 10);//發(fā)送WR_HV命令,地址HARD_VERSION ? ? ? ? ? ? ? ? ? ? Delay(100); ? ? ? ? ? ? ? ? ? ? if (GetCorrect == true) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("硬件版本號寫入成功!"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("硬件版本號寫入失?。?); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show(ex.Message); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("硬件版本號數(shù)據(jù)格式錯誤!請重新寫入!"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? //判斷輸入是否合法,必須是數(shù)字 ? ? ? ? private const string PATTERN = @"^[0-9]*$"; ? ? ? ? private bool isLegal(string hex) ? ? ? ? { ? ? ? ? ? ? return System.Text.RegularExpressions.Regex.IsMatch(hex, PATTERN); ? ? ? ? } ? ? ? ? ? //延時函數(shù) ? ? ? ? [DllImport("kernel32.dll")] ? ? ? ? static extern uint GetTickCount(); ? ? ? ? static void Delay(uint ms) ? ? ? ? { ? ? ? ? ? ? uint start = GetTickCount(); ? ? ? ? ? ? while (GetTickCount() - start < ms) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Application.DoEvents(); ? ? ? ? ? ? } ? ? ? ? }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# WinForm程序處理后臺繁忙導致前臺控件假死現(xiàn)象解決方法
這篇文章主要介紹了C# WinForm程序處理后臺繁忙導致前臺控件假死現(xiàn)象解決方法,本文通過Application.DoEvents()解決這個問題,并講解了Application.DoEvents()的作用,需要的朋友可以參考下2015-06-06macOS系統(tǒng)下Vscode的python配置教程
這篇文章主要介紹了macOS系統(tǒng)下Vscode的python配置教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04