最近公司項目上線,之前利用串口通訊實現校牌的無感知簽到程序, 項目上線以后剛剛好有時間把之前的出現的問題做下記錄,廢話不多,直接到主題
串口介紹:
串行接口簡稱串口,也稱串行通信接口或串行通訊接口(通常指COM接口),是采用串行通信方式的擴展接口。(至於再詳細,自己百度)
正文:
最近在公司讓用C#寫一個串口通訊程序,下面我將這次遇到的問題和解決方法奉獻出來,希望對工作中需要的朋友有所幫助!
我們來看具體的實現步驟。
公司要求實現以下幾個功能:
1.)啟動程序打開串口通信,接受嵌入式校牌發送過來的16進制形式的數據指令執行業務操作,業務操作完做出回應。
2.)根據需要設置串口通信的必要參數。
3.)通過校牌指令執行相關業務,拉取數據通過訪問java的http接口獲取數據,並將數據進行處理轉換為16進制形式下發給校牌
4.)配置相關接口地址
5.)校牌答題與教室端互動通過本地UPD傳遞給教室端,
看着好像挺復雜,其實都是紙老虎,一戳就破,前提是你敢去戳。我盡量講的詳細一些,爭取說到每個知識點。
C#代碼實現:采用SerialPort
實例化一個SerialPort
1. private SerialPort ComDevice = new SerialPort();
我自己寫了個串口的類就直接上代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.IO.Ports; 5 using System.Linq; 6 using System.Text; 7 using System.Threading; 8 using System.Threading.Tasks; 9 10 namespace ZPZSerialPort.ComSerialPort 11 { 12 public sealed class ComDeviceManager 13 { 14 private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();//NLog日志記錄串口信息 15 private static ComDeviceManager _comManager; 16 private static readonly object _instAsync = new object(); 17 public SerialPort ComDeviceComputerChip { get; private set; } 18 19 public Action<Byte[]> ActionComputerChip { get; set; } 20 21 /// <summary> 22 /// 此處配置根據實際串口進行配置,也可以配置為可變的參數 23 /// </summary> 24 private ComDeviceManager() 25 { 26 ComDeviceComputerChip = new SerialPort();//實例化一個SerialPort 27 ComDeviceComputerChip.PortName = ConfigurationManager.AppSettings["protnamexyt"];//端口號此處端口號不固定此處配置為可變參數 28 ComDeviceComputerChip.BaudRate = 115200;// 串行波特率指定為115200 29 ComDeviceComputerChip.Parity = (Parity)Convert.ToInt32("0");// 30 ComDeviceComputerChip.DataBits = Convert.ToInt32("8"); 31 ComDeviceComputerChip.StopBits = (StopBits)Convert.ToInt32("1"); 32 ComDeviceComputerChip.DataReceived += ComDevice1_DataReceived; 33 34 } 35 /// <summary> 36 /// 接受端口數據事件 37 /// </summary> 38 /// <param name="sender"></param> 39 /// <param name="e"></param> 40 private void ComDevice1_DataReceived(object sender, SerialDataReceivedEventArgs e) 41 { 42 byte[] buffers = new byte[ComDeviceComputerChip.BytesToRead]; 43 ComDeviceComputerChip.Read(buffers, 0, buffers.Length); 44 ActionComputerChip?.Invoke(buffers); 45 } 46 /// <summary> 47 /// 當前設備 48 /// </summary> 49 public static ComDeviceManager CurrentDevice 50 { 51 get 52 { 53 if (_comManager == null) 54 { 55 lock (_instAsync) 56 { 57 if (_comManager == null) 58 { 59 return _comManager = new ComDeviceManager(); 60 } 61 } 62 } 63 64 return _comManager; 65 } 66 } 67 /// <summary> 68 /// 打開端口 69 /// </summary> 70 /// <returns></returns> 71 public bool OpenDevice() 72 { 73 try 74 { 75 if (!ComDeviceComputerChip.IsOpen) 76 { 77 ComDeviceComputerChip.Open(); 78 } 79 return true; 80 } 81 catch (Exception ex) 82 { 83 logger.Error("打開設備錯誤:"+ex); 84 } 85 86 return false; 87 } 88 /// <summary> 89 /// 發送數據 90 /// </summary> 91 /// <param name="data"></param> 92 /// <returns></returns> 93 public bool SendDzxp(byte[] data) 94 { 95 try 96 { 97 if (ComDeviceComputerChip.IsOpen) 98 { 99 Thread.Sleep(10);// 延遲發送必須做延遲發送不然發送給校牌接受不到,這個問題浪費了一上午事件才發送在發送得時候需要做延遲 100 ComDeviceComputerChip.Write(data, 0, data.Length);//發送數據給串口端口 101 Thread.Sleep(10);// 延遲發送 102 return true; 103 } 104 } 105 catch (Exception ex) 106 { 107 logger.Error(ex); 108 } 109 110 return false; 111 } 112 113 114 } 115 }
設備操作類已經編寫完畢,接着就是我們收到指令主動執行操作:操作的步驟如下幾點
1.)同步時間
收到同步時間指令獲取當前系統時間轉換為16進制字節,進行CRC校驗之后帶上,發送給基站,發送的格式為
引導碼+發送碼+卡號+響應成功碼+長度+內容(當前時間)+校驗碼
2.)同步課程
收到同步課程指令先通過接口拉取數據,把拉取到json數據解析,上課的開始時間,頻點,日期,星期 數據進行解析為16進制字節數組
引導碼+發送碼+卡號+響應成功碼+長度+內容(一天課程上課時間)+校驗碼
拉取到的課程與校牌成功以后 把卡號,頻點,同步成功最后課程的時間 提交給接口保存
3.)簽到
收到簽到指令 進行回復
引導碼+發送碼+卡號+響應成功碼+長度+內容(校牌發送的簽到指令)+校驗碼
把校牌卡號與課程ID 提交給接口保存
一 通訊層格式:
請求/控制數據幀
引導碼 |
數據傳輸方向 |
設備IC卡號 |
命令碼 |
數據包長度 |
數據內容 |
校驗碼 (CRC16) |
|
FA FA |
D0/D1 |
4 bytes |
0x00~0xFF |
0x00~0x3F |
0~N |
CRC_L |
CRC_H |
-
引導碼:2 bytes,0xFA 0xFA;
-
數據傳輸方向:1 byte,0xD0為電子校牌上傳數據給服務器,0xD1為服務器下發數據到電子校牌;
-
設備IC卡號:4 byte,對應內嵌電子校牌的IC卡號;
-
命令碼:1 byte,取值范圍為0x00 – 0xFF;
-
數據包長度:1 byte,0x00 – 0x3F;
-
數據內容:傳輸的數據信息,長度大小與數據包長度一致;
-
校驗碼:2 bytes,低字節在前,高字節在后,采用CRC16校驗方式,校驗數據包括從數據傳輸方向到數據內容;
響應數據幀
引導碼 |
數據傳輸方向 |
設備IC卡號 |
命令碼 |
響應標志碼 |
數據包長度 |
數據內容 |
校驗碼 (CRC16) |
|
FA FA |
D0/D1 |
4 bytes |
0x00~0xFF |
0x80/0x81 |
0x00~0x3F |
0~N |
CRC_L |
CRC_H |
-
引導碼:2 bytes,0xFA 0xFA;
-
數據傳輸方向:1 byte,0xD0為終端設備上傳數據給服務器,0xD1為服務器下發數據到終端設備;
-
設備IC卡號:4 byte,對應內嵌電子校牌的IC卡號;
-
命令碼:1 byte,取值范圍為0x00 – 0xFF;
-
響應標志碼:1 byte,0x80-----接收正確;0x81----接收有誤;
數據有誤碼:0x01-----數據格式有誤
0x02-----校驗碼錯誤
0x03-----題型有誤
-
數據包長度:1 byte,0x00 – 0x3F;
-
數據內容:傳輸的數據信息,長度大小與數據包長度一致;
-
校驗碼:2 bytes,低字節在前,高字節在后,采用CRC16校驗方式,校驗數據包括從數據傳輸方向到數據內容;
二 詳細命令解析:
(以設備IC卡號為0xA0 0xA1 0xA2 0xA3為例)
-
電子校牌連接基站服務器 0x00
命令碼: 0x00
數據內容:年/月/日/星期/時/分/秒 7 bytes
舉例:
Send: FA FA D0 A0 A1 A2 A3 00 00 CRC16
Recv: FA FA D1 A0 A1 A2 A3 00 80 07 YY MM DD WW hh mm ss CRC16 // 連接成功
-
電子校牌請求服務器同步課程表 0x01
命令碼: 0x01
數據內容:ID號:A0 A1 A2 A3
FF FF FF FF 表示對所有電子校牌統一下發
N=2n+1:課程表(時間、頻點) 星期幾+(時間(小時/分鍾)+頻點)* n(課節數,最大10)
Weekday:星期一 ~ 星期六(1~6), 星期日: 0
時間(H/M):((H-6)<< 4) | (M/5) 分鍾為5的倍數
舉例:
Send: FA FA D0 A0 A1 A2 A3 01 00 CRC16 // 校牌請求下發課程表
Recv: FA FA D1 A0 A1 A2 A3 01 80 N weekday 1...2n CRC16 // 服務器下發課程表
Send: FA FA D0 A0 A1 A2 A3 01 80 01 weekday CRC16 //校牌回復設置課程表成功
-
電子校牌完成簽到功能 0x02
命令碼: 0x02
數據內容: 年/月/日/時/分/秒 6 bytes
舉例:
Send: FA FA D0 A0 A1 A2 A3 02 06 YY MM DD hh mm ss CRC16
Recv: FA FA D1 A0 A1 A2 A3 02 80 06 YY MM DD hh mm ss CRC16 // 簽到成功
處理相關業務邏輯使用工廠模式
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ZPZSerialPort.Factory 8 { 9 public interface ICommunication 10 { 11 bool Send(object data); 12 } 13 /// <summary> 14 /// 同步時間 15 /// </summary> 16 public class SyncTime : ICommunication// 17 { 18 public bool Send(object data) 19 { 20 Console.WriteLine("同步時間接受的數據"); 21 return true; 22 } 23 } 24 /// <summary> 25 /// 同步課程 26 /// </summary> 27 public class SyncCourse : ICommunication 28 { 29 public bool Send(object data) 30 { 31 Console.WriteLine("同步課程接受的數據"); 32 return true; 33 } 34 } 35 /// <summary> 36 /// 簽到 37 /// </summary> 38 public class Sign : ICommunication 39 { 40 public bool Send(object data) 41 { 42 Console.WriteLine("同步課程接受的數據"); 43 return true; 44 } 45 46 } 47 /// <summary> 48 /// 答題 49 /// </summary> 50 public class Answer : ICommunication 51 { 52 public bool Send(object data) 53 { 54 Console.WriteLine("答題接受的數據"); 55 return true; 56 } 57 } 58 59 60 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ZPZSerialPort.Factory 8 { 9 /// <summary> 10 /// 通訊工廠 11 /// </summary> 12 public class CommunicationFactory 13 { 14 public ICommunication CreateCommunicationFactory(string style) 15 { 16 switch (style) 17 { 18 case "SyncTime"://同步時間 19 return new SyncTime(); 20 case "SyncCourse"://同步課程 21 return new SyncCourse(); 22 case "Sign"://簽到 23 return new Sign(); 24 case "Answer"://答題 25 return new Answer(); 26 } 27 return null; 28 } 29 } 30 }
處理接受得數據實體
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ZPZSerialPort.COM_USB 8 { 9 /// <summary> 10 /// 響應數據幀 11 /// </summary> 12 public class USBComReceiveEntity 13 { 14 //引導碼 2 bytes,0xFA 0xFA 15 public string header { get; set; } 16 17 //數據傳輸方向 1 byte,0xD0為電子校牌上傳數據給服務器,0xD1為服務器下發數據到電子校牌 18 public string direction { get; set; } 19 20 //設備IC卡號 4 byte,對應內嵌電子校牌的IC卡號 21 public string icCard { get; set; } 22 23 //命令碼 1 byte,取值范圍為0x00 – 0xFF 24 public string code { get; set; } 25 26 //響應標志碼:1 byte,0x80-----接收正確;0x81----接收有誤 27 public string response { get; set; } 28 29 //數據包長度 1 byte,0x00 – 0x3F 30 public string length { get; set; } 31 32 //數據內容 傳輸的數據信息,長度大小與數據包長度一致 33 public string content { get; set; } 34 35 //校驗碼CRC16 2 bytes,低字節在前,高字節在后,采用CRC16校驗方式,校驗數據包括從數據傳輸方向到數據內容 36 public string check { get; set; } 37 38 /// <summary> 39 /// set 實體 40 /// </summary> 41 /// <param name="str"></param> 42 /// <returns></returns> 43 public static USBComReceiveEntity SetReceiveEntity(string str) 44 { 45 if (str == null || str.Length == 0) return null; 46 USBComReceiveEntity entity = new USBComReceiveEntity(); 47 str = str.Replace(" ", ""); 48 if (str.Length >= 4) entity.header = str.Substring(0, 4); 49 if (str.Length >= 6) entity.direction = str.Substring(4, 2); 50 if (str.Length >= 14) entity.icCard = str.Substring(6, 8); 51 if (str.Length >= 16) entity.code = str.Substring(14, 2); 52 if (str.Length >= 18) entity.response = str.Substring(16, 2); 53 if (str.Length >= 20) entity.length = str.Substring(18, 2); 54 int count = 0; 55 if (entity.length != null && entity.length.Length > 0) count = int.Parse(entity.length) * 2; 56 if (count > 0 && str.Length >= 20 + count) entity.content = str.Substring(20, count); 57 if (str.Length >= count + 20 + 4) entity.check = str.Substring(20 + count, 4); 58 return entity; 59 } 60 61 /// <summary> 62 /// 校驗碼CRC16 63 /// </summary> 64 /// <param name="sendEntity"></param> 65 /// <returns></returns> 66 public static string getCheckString(USBComReceiveEntity sendEntity) 67 { 68 string str = ""; 69 if (sendEntity.direction == null || sendEntity.direction.Length == 0) str = str + USBComUtil.Com_Send; 70 else str = str + sendEntity.direction; 71 if (sendEntity.icCard == null || sendEntity.icCard.Length == 0) str = str + ""; 72 else str = str + sendEntity.icCard; 73 if (sendEntity.code == null || sendEntity.code.Length == 0) str = str + ""; 74 else str = str + sendEntity.code; 75 if (sendEntity.response == null || sendEntity.response.Length == 0) str = str + ""; 76 else str = str + sendEntity.response; 77 if (sendEntity.length == null || sendEntity.length.Length == 0) str = str + ""; 78 else str = str + sendEntity.length; 79 if (sendEntity.content == null || sendEntity.content.Length == 0) str = str + ""; 80 else str = str + sendEntity.content; 81 return CRCUtil.ToModbusCRC16(str); 82 } 83 84 /// <summary> 85 /// 返回實體字符串 86 /// </summary> 87 /// <param name="sendEntity"></param> 88 /// <returns></returns> 89 public static string getEntityToString(USBComReceiveEntity sendEntity) 90 { 91 string str = ""; 92 if (sendEntity.header == null || sendEntity.header.Length == 0) str = USBComUtil.Com_Header; 93 else str = sendEntity.header; 94 if (sendEntity.direction == null || sendEntity.direction.Length == 0) str = str + USBComUtil.Com_Send; 95 else str = str + sendEntity.direction; 96 if (sendEntity.icCard == null || sendEntity.icCard.Length == 0) str = str + ""; 97 else str = str + sendEntity.icCard; 98 if (sendEntity.code == null || sendEntity.code.Length == 0) str = str + ""; 99 else str = str + sendEntity.code; 100 if (sendEntity.response == null || sendEntity.response.Length == 0) str = str + ""; 101 else str = str + sendEntity.response; 102 if (sendEntity.length == null || sendEntity.length.Length == 0) str = str + ""; 103 else str = str + sendEntity.length; 104 if (sendEntity.content == null || sendEntity.content.Length == 0) str = str + ""; 105 else str = str + sendEntity.content; 106 if (sendEntity.check == null || sendEntity.check.Length == 0) str = str + ""; 107 else str = str + sendEntity.check; 108 return str; 109 } 110 } 111 }
CRC16校驗 算法類
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ZPZSerialPort.COM_USB 8 { 9 public class CRCUtil 10 { 11 #region CRC16 12 public static byte[] CRC16(byte[] data) 13 { 14 int len = data.Length; 15 if (len > 0) 16 { 17 ushort crc = 0xFFFF; 18 19 for (int i = 0; i < len; i++) 20 { 21 crc = (ushort)(crc ^ (data[i])); 22 for (int j = 0; j < 8; j++) 23 { 24 crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1); 25 } 26 } 27 byte hi = (byte)((crc & 0xFF00) >> 8); //高位置 28 byte lo = (byte)(crc & 0x00FF); //低位置 29 30 return new byte[] { lo, hi }; 31 } 32 return new byte[] { 0, 0 }; 33 } 34 #endregion 35 36 #region ToCRC16 37 public static string ToCRC16(string content) 38 { 39 return ToCRC16(content, Encoding.UTF8); 40 } 41 42 public static string ToCRC16(string content, bool isReverse) 43 { 44 return ToCRC16(content, Encoding.UTF8, isReverse); 45 } 46 47 public static string ToCRC16(string content, Encoding encoding) 48 { 49 return ByteToString(CRC16(encoding.GetBytes(content)), true); 50 } 51 52 public static string ToCRC16(string content, Encoding encoding, bool isReverse) 53 { 54 return ByteToString(CRC16(encoding.GetBytes(content)), isReverse); 55 } 56 57 public static string ToCRC16(byte[] data) 58 { 59 return ByteToString(CRC16(data), true); 60 } 61 62 public static string ToCRC16(byte[] data, bool isReverse) 63 { 64 return ByteToString(CRC16(data), isReverse); 65 } 66 #endregion 67 68 #region ToModbusCRC16 69 public static string ToModbusCRC16(string s) 70 { 71 return ToModbusCRC16(s, true); 72 } 73 74 public static string ToModbusCRC16(string s, bool isReverse) 75 { 76 return ByteToString(CRC16(StringToHexByte(s)), isReverse); 77 } 78 79 public static string ToModbusCRC16(byte[] data) 80 { 81 return ToModbusCRC16(data, true); 82 } 83 84 public static string ToModbusCRC16(byte[] data, bool isReverse) 85 { 86 return ByteToString(CRC16(data), isReverse); 87 } 88 #endregion 89 90 #region ByteToString 91 public static string ByteToString(byte[] arr, bool isReverse) 92 { 93 try 94 { 95 byte hi = arr[0], lo = arr[1]; 96 return Convert.ToString(isReverse ? hi + lo * 0x100 : hi * 0x100 + lo, 16).ToUpper().PadLeft(4, '0'); 97 } 98 catch (Exception ex) { throw (ex); } 99 } 100 101 public static string ByteToString(byte[] arr) 102 { 103 try 104 { 105 return ByteToString(arr, true); 106 } 107 catch (Exception ex) { throw (ex); } 108 } 109 #endregion 110 111 #region StringToHexString 112 public static string StringToHexString(string str) 113 { 114 StringBuilder s = new StringBuilder(); 115 foreach (short c in str.ToCharArray()) 116 { 117 s.Append(c.ToString("X4")); 118 } 119 return s.ToString(); 120 } 121 #endregion 122 123 #region StringToHexByte 124 private static string ConvertChinese(string str) 125 { 126 StringBuilder s = new StringBuilder(); 127 foreach (short c in str.ToCharArray()) 128 { 129 if (c <= 0 || c >= 127) 130 { 131 s.Append(c.ToString("X4")); 132 } 133 else 134 { 135 s.Append((char)c); 136 } 137 } 138 return s.ToString(); 139 } 140 141 private static string FilterChinese(string str) 142 { 143 StringBuilder s = new StringBuilder(); 144 foreach (short c in str.ToCharArray()) 145 { 146 if (c > 0 && c < 127) 147 { 148 s.Append((char)c); 149 } 150 } 151 return s.ToString(); 152 } 153 154 /// <summary> 155 /// 字符串轉16進制字符數組 156 /// </summary> 157 /// <param name="hex"></param> 158 /// <returns></returns> 159 public static byte[] StringToHexByte(string str) 160 { 161 return StringToHexByte(str, false); 162 } 163 164 /// <summary> 165 /// 字符串轉16進制字符數組 166 /// </summary> 167 /// <param name="str"></param> 168 /// <param name="isFilterChinese">是否過濾掉中文字符</param> 169 /// <returns></returns> 170 public static byte[] StringToHexByte(string str, bool isFilterChinese) 171 { 172 string hex = isFilterChinese ? FilterChinese(str) : ConvertChinese(str); 173 174 //清除所有空格 175 hex = hex.Replace(" ", ""); 176 //若字符個數為奇數,補一個0 177 hex += hex.Length % 2 != 0 ? "0" : ""; 178 179 byte[] result = new byte[hex.Length / 2]; 180 for (int i = 0, c = result.Length; i < c; i++) 181 { 182 result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); 183 } 184 return result; 185 } 186 #endregion 187 } 188 }
具體得業務代碼就不貼出來了,由於是公司產品項目,大家都明白我也不多說。
代碼下載:ZPZSerialPort.rar
不足之處,還望見諒!