VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C#串口通信程序实现无感知签到与答题(2)

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 (