电子药箱通讯服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RfidFixedHeaderCustomDataHandlingAdapter.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Runtime.InteropServices;
  5. using TouchSocket.Core.ByteManager;
  6. using TouchSocket.Sockets;
  7. namespace Rdh.SocketServer.Client.Models
  8. {
  9. public class RfidUdpDataHandlingAdapter : UdpDataHandlingAdapter
  10. {
  11. public override bool CanSendRequestInfo => false;
  12. public override bool CanSplicingSend => false;
  13. public int HeaderLength => 5;
  14. /// <summary>
  15. /// 临时包,此包仅当前实例储存
  16. /// </summary>
  17. private ByteBlock tempByteBlock;
  18. /// <summary>
  19. /// 包剩余长度
  20. /// </summary>
  21. private byte surPlusLength;
  22. protected override void PreviewReceived(EndPoint remoteEndPoint, ByteBlock byteBlock)
  23. {
  24. byte[] buffer = byteBlock.Buffer;
  25. int r = byteBlock.Len;
  26. if (this.tempByteBlock == null)//如果没有临时包,则直接分包。
  27. {
  28. SplitPackage(remoteEndPoint, buffer, 0, r);
  29. }
  30. else
  31. {
  32. if (surPlusLength == r)//接收长度正好等于剩余长度,组合完数据以后直接处理数据。
  33. {
  34. this.tempByteBlock.Write(buffer, 0, surPlusLength);
  35. PreviewHandle(remoteEndPoint, this.tempByteBlock);
  36. this.tempByteBlock = null;
  37. surPlusLength = 0;
  38. }
  39. else if (surPlusLength < r)//接收长度大于剩余长度,先组合包,然后处理包,然后将剩下的分包。
  40. {
  41. this.tempByteBlock.Write(buffer, 0, surPlusLength);
  42. PreviewHandle(remoteEndPoint, this.tempByteBlock);
  43. this.tempByteBlock = null;
  44. SplitPackage(remoteEndPoint, buffer, surPlusLength, r);
  45. }
  46. else//接收长度小于剩余长度,无法处理包,所以必须先组合包,然后等下次接收。
  47. {
  48. this.tempByteBlock.Write(buffer, 0, r);
  49. surPlusLength -= (byte)r;
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// 分解包
  55. /// </summary>
  56. /// <param name="dataBuffer"></param>
  57. /// <param name="index"></param>
  58. /// <param name="r"></param>
  59. private void SplitPackage(EndPoint remoteEndPoint, byte[] dataBuffer, int index, int r)
  60. {
  61. while (index < r)
  62. {
  63. byte length = dataBuffer[index];
  64. int recedSurPlusLength = r - index - 1;
  65. if (recedSurPlusLength >= length)
  66. {
  67. ByteBlock byteBlock = BytePool.GetByteBlock(length);
  68. byteBlock.Write(dataBuffer, index + 1, length);
  69. PreviewHandle(remoteEndPoint, byteBlock);
  70. surPlusLength = 0;
  71. }
  72. else//半包
  73. {
  74. this.tempByteBlock = BytePool.GetByteBlock(length);
  75. surPlusLength = (byte)(length - recedSurPlusLength);
  76. this.tempByteBlock.Write(dataBuffer, index + 1, recedSurPlusLength);
  77. }
  78. index += length + 1;
  79. }
  80. }
  81. /// <summary>
  82. /// 处理数据
  83. /// </summary>
  84. /// <param name="byteBlock"></param>
  85. private void PreviewHandle(EndPoint remoteEndPoint, ByteBlock byteBlock)
  86. {
  87. try
  88. {
  89. var requestInfo = new RfidFixedHeaderRequestInfo();
  90. byteBlock.Read(out byte[] header, this.HeaderLength);
  91. requestInfo.OnParsingHeader(header);
  92. byteBlock.Read(out byte[] body, requestInfo.BodyLength);
  93. requestInfo.OnParsingBody(body);
  94. this.GoReceived(remoteEndPoint, byteBlock, requestInfo);
  95. }
  96. finally
  97. {
  98. byteBlock.Dispose();//在框架里面将内存块释放
  99. }
  100. }
  101. protected override void Reset()
  102. {
  103. }
  104. protected override void PreviewSend(EndPoint endPoint, byte[] buffer, int offset, int length, bool isAsync)
  105. {
  106. this.GoSend(endPoint, buffer, offset, length, isAsync);
  107. }
  108. protected override void PreviewSend(IRequestInfo requestInfo, bool isAsync)
  109. {
  110. }
  111. protected override void PreviewSend(EndPoint endPoint, IList<ArraySegment<byte>> transferBytes, bool isAsync)
  112. {
  113. throw new System.NotImplementedException();
  114. }
  115. }
  116. public class RfidFixedHeaderRequestInfo : IFixedHeaderRequestInfo
  117. {
  118. private const int head = 0xA5;
  119. /// <summary>
  120. /// 数据包头
  121. /// </summary>
  122. public static int Head { get { return head; } }
  123. private int bodyLength;
  124. public int BodyLength
  125. {
  126. get { return bodyLength; }
  127. }
  128. private byte[] reserved;
  129. /// <summary>
  130. /// 485地址码
  131. /// </summary>
  132. public byte[] Reserved { get { return reserved; } }
  133. private int cmd;
  134. /// <summary>
  135. /// 指令码
  136. /// </summary>
  137. public int CMD { get { return cmd; } }
  138. private byte[] header;
  139. public byte[] Header { get { return header; } }
  140. private byte[] body;
  141. public byte[] Body
  142. {
  143. get { return body; }
  144. }
  145. public bool OnParsingBody(byte[] body)
  146. {
  147. this.bodyLength = body.Length;
  148. this.body = body;
  149. return true;
  150. }
  151. public bool OnParsingHeader(byte[] header)
  152. {
  153. if (header.Length == 5 && header[0] == 0xA5)
  154. {
  155. this.bodyLength = header[1];
  156. this.reserved = new byte[2];
  157. Buffer.BlockCopy(header, 2, this.reserved, 0, 2);
  158. this.cmd = header[4];
  159. this.header = header;
  160. return true;
  161. }
  162. return false;
  163. }
  164. }
  165. [StructLayout(LayoutKind.Sequential)]
  166. public struct MtBarcodeResponseProtocol
  167. {
  168. #region Properties
  169. /// <summary>
  170. /// 起始位
  171. /// </summary>
  172. public Byte Head { get { return 0xA5; } }
  173. /// <summary>
  174. /// Length(不含)之后的数据长度
  175. /// </summary>
  176. public Byte Length { get; set; }
  177. /// <summary>
  178. /// 低字节在前,高字节在后
  179. /// </summary>
  180. public UInt16 Password { get; set; }
  181. /// <summary>
  182. /// 指令码
  183. /// </summary>
  184. public Byte Command { get; set; }
  185. /// <summary>
  186. /// 0x01:数据包标识
  187. /// </summary>
  188. public Byte Flags { get; set; }
  189. /// <summary>
  190. /// 读取的频点
  191. /// </summary>
  192. public Byte Frequency { get; set; }
  193. /// <summary>
  194. /// 天线号
  195. /// </summary>
  196. public Byte Anntennea { get; set; }
  197. /// <summary>
  198. /// 标签的 PC 协议控制字,固定 2Byte
  199. /// </summary>
  200. public UInt16 PC { get; set; }
  201. /// <summary>
  202. /// 标签的 EPC 号(条码),长度可变化
  203. /// </summary>
  204. public String EPC { get; set; }
  205. /// <summary>
  206. /// 标签的 CRC,固定 2Byte
  207. /// </summary>
  208. public UInt16 CRC { get; set; }
  209. /// <summary>
  210. /// 标签的实时 RSSI,固定 2Byte
  211. /// </summary>
  212. public UInt16 RSSI { get; set; }
  213. /// <summary>
  214. /// 校验和(除校验本身以外所有字节求和后,按位取反,再加 1)
  215. /// </summary>
  216. public Byte Check { get; set; }
  217. #endregion
  218. public Byte[] GetBytes()
  219. {
  220. return StructToBytes<MtBarcodeResponseProtocol>(this);
  221. }
  222. public override string ToString()
  223. {
  224. return ToString(" ");
  225. }
  226. public String ToString(String seperator)
  227. {
  228. return String.Join(seperator, GetBytes());
  229. }
  230. public static MtBarcodeResponseProtocol? ReadFromBytes(Byte[] source, Int32 dataLength)
  231. {
  232. if (dataLength < 16 || source.Length < 16)
  233. {
  234. return null;
  235. }
  236. MtBarcodeResponseProtocol rlt = new MtBarcodeResponseProtocol();
  237. rlt.Length = source[1];
  238. rlt.Password = (UInt16)(source[2] | (source[3] << 8));
  239. rlt.Command = source[4];
  240. rlt.Flags = source[5];
  241. rlt.Frequency = source[6];
  242. rlt.Anntennea = source[7];
  243. rlt.PC = (UInt16)(source[8] | (source[9] << 8));
  244. Int32 epcDataLength = rlt.Length - 13;
  245. for (Int32 i = 0; i < epcDataLength; i++)
  246. {
  247. rlt.EPC += source[10 + i].ToString("X2");
  248. }
  249. rlt.Check = source[dataLength - 1];
  250. rlt.RSSI = (UInt16)(source[dataLength - 2] << 8 | source[dataLength - 3]);
  251. rlt.CRC = (UInt16)(source[dataLength - 4] | source[dataLength - 5]);
  252. return rlt;
  253. }
  254. public static MtBarcodeResponseProtocol? ReadFromBytes(Byte[] source)
  255. {
  256. var dataLength = source.Length;
  257. if (dataLength < 16 || source.Length < 16)
  258. {
  259. return null;
  260. }
  261. MtBarcodeResponseProtocol rlt = new MtBarcodeResponseProtocol();
  262. rlt.Length = source[1];
  263. rlt.Password = (UInt16)(source[2] | (source[3] << 8));
  264. rlt.Command = source[4];
  265. rlt.Flags = source[5];
  266. rlt.Frequency = source[6];
  267. rlt.Anntennea = source[7];
  268. rlt.PC = (UInt16)(source[8] | (source[9] << 8));
  269. Int32 epcDataLength = rlt.Length - 13;
  270. for (Int32 i = 0; i < epcDataLength; i++)
  271. {
  272. rlt.EPC += source[10 + i].ToString("X2");
  273. }
  274. rlt.Check = source[dataLength - 1];
  275. rlt.RSSI = (UInt16)(source[dataLength - 2] << 8 | source[dataLength - 3]);
  276. rlt.CRC = (UInt16)(source[dataLength - 4] | source[dataLength - 5]);
  277. return rlt;
  278. }
  279. public static byte[] StructToBytes<T>(T structObj)
  280. {
  281. int size = Marshal.SizeOf(structObj);
  282. IntPtr buffer = Marshal.AllocHGlobal(size);
  283. try
  284. {
  285. Marshal.StructureToPtr(structObj, buffer, false);
  286. byte[] bytes = new byte[size];
  287. Marshal.Copy(buffer, bytes, 0, size);
  288. return bytes;
  289. }
  290. finally
  291. {
  292. Marshal.FreeHGlobal(buffer);
  293. }
  294. }
  295. }
  296. }