參考之前的資料,寫了一個自己常用的串口類,
字符串發送類型用了兩種方式,char[] 和byte[] ;
數據接收也是采用兩種 char[] 和byte[] 兩種。
類代碼貼出來:
public class SerialPortManager { private bool _recStaus = true;//接收狀態字 private bool _comPortIsOpen; private void SetAfterClose()//成功關閉串口或串口丟失后的設置 { _comPortIsOpen = false;//串口狀態設置為關閉狀態 } private void SetComLose()//成功關閉串口或串口丟失后的設置 { SetAfterClose();//成功關閉串口或串口丟失后的設置 } public SerialPort CurrentSerialPort { get; set; } = new SerialPort(); public byte[] ReceivedDataPacket { get; set; } public bool OpenSerialPort(SerialPort serialPortPara) { CurrentSerialPort = serialPortPara; if (_comPortIsOpen == false) //ComPortIsOpen == false當前串口為關閉狀態,按鈕事件為打開串口 { try //嘗試打開串口 { CurrentSerialPort.ReadTimeout = 8000; //串口讀超時8秒 CurrentSerialPort.WriteTimeout = 8000; //串口寫超時8秒,在1ms自動發送數據時拔掉串口,寫超時5秒后,會自動停止發送,如果無超時設定,這時程序假死 CurrentSerialPort.ReadBufferSize = 1024; //數據讀緩存 CurrentSerialPort.WriteBufferSize = 1024; //數據寫緩存 CurrentSerialPort.DataReceived += ComReceive; //串口接收中斷 CurrentSerialPort.Open(); _comPortIsOpen = true; //串口打開狀態字改為true } catch (Exception exception) //如果串口被其他占用,則無法打開 { _comPortIsOpen = false; ReceiveCompleted = false; throw new Exception("unable open serial port" + exception.Message); } return true; } return true; } public char[] ReceivedDataPacketChar { get; set; } public bool ReceiveCompleted { get; set; } private void ComReceive(object sender, SerialDataReceivedEventArgs e) { ReceiveCompleted = false; if (_recStaus) //如果已經開啟接收 { try { Thread.Sleep(50); ReceivedDataPacket = new byte[CurrentSerialPort.BytesToRead]; ReceivedDataPacketChar = new char[CurrentSerialPort.BytesToRead]; // change to char datas if (ByteMode) { CurrentSerialPort.Read(ReceivedDataPacket, 0, ReceivedDataPacket.Length); } else { CurrentSerialPort.Read(ReceivedDataPacketChar, 0, CurrentSerialPort.BytesToRead); } ReceiveCompleted = true; } catch (Exception) { if (CurrentSerialPort.IsOpen == false) //如果ComPort.IsOpen == false,說明串口已丟失 { SetComLose(); //串口丟失后相關設置 } else { throw new Exception("unable to receive data"); } } } else //暫停接收 { CurrentSerialPort.DiscardInBuffer(); //清接收緩存 } } public bool SendDataPacket(string dataPacket) { char[] dataPacketChar = dataPacket.ToCharArray(); return SendDataPacket(dataPacketChar); } public bool SendDataPacket(byte[] dataPackeg) { try { ByteMode = true; CurrentSerialPort.Write(dataPackeg, 0, dataPackeg.Length); } catch (Exception exception) { Console.WriteLine(exception.Message); return false; } return true; } public bool CloseSerialPort() { try//嘗試關閉串口 { CurrentSerialPort.DiscardOutBuffer();//清發送緩存 CurrentSerialPort.DiscardInBuffer();//清接收緩存 //WaitClose = true;//激活正在關閉狀態字,用於在串口接收方法的invoke里判斷是否正在關閉串口 CurrentSerialPort.Close();//關閉串口 // WaitClose = false;//關閉正在關閉狀態字,用於在串口接收方法的invoke里判斷是否正在關閉串口 SetAfterClose();//成功關閉串口或串口丟失后的設置 _comPortIsOpen = false; } catch//如果在未關閉串口前,串口就已丟失,這時關閉串口會出現異常 { if (CurrentSerialPort.IsOpen == false)//判斷當前串口狀態,如果ComPort.IsOpen==false,說明串口已丟失 { SetComLose(); } else//未知原因,無法關閉串口 { throw new Exception("unable close serial port"); } } return true; } public bool ByteMode { get; set; } public bool SendDataPacket(char[] senddata) { try { ByteMode = false; CurrentSerialPort.Write(senddata, 0, senddata.Length); } catch (Exception exception) { Console.WriteLine(exception.Message); return false; } return true; } }
調用方法:char[]
private SerialPortManager _serialPortManager = new SerialPortManager(); char[] bytes ={(char)0xAA,(char)0xFF,(char)0x55, (char)0xff, (char)0x06, (char)0x20, (char)0xAA,(char)0XEE,(char)0x55,(char)0xEE}; _serialPortManager.SendDataPacket(bytes);
byte[]
byte[] byteOrdor = new byte[] { 0xAA,0XFF,0X55, 0XFF,0X06,0X20,0XAA,0XEE,0X55,0XEE }; _serialPortManager.SendDataPacket(byteOrdor);
數據接收數組對應使用
public byte[] ReceivedDataPacket { get; set; } public char[] ReceivedDataPacketChar { get; set; }
實際可用。