StateMachineMessageCutter.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Edge.Core.Processor.Communicator;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace HengshanPaymentTerminal
  8. {
  9. public class StateMachineMessageCutter : IMessageCutter<byte[]>
  10. {
  11. #region Internal State enum
  12. private enum State
  13. {
  14. Uninitialized,
  15. LengthReady,
  16. BodyReady,
  17. EtxReady,
  18. CrcReady
  19. }
  20. #endregion
  21. #region Fields
  22. public byte[] Message { get; private set; }
  23. public event EventHandler OnMessageCut;
  24. public event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
  25. private string loggerAppendix = "HengshanPay Terminal";
  26. private readonly SizableWindow<byte> window;
  27. //private State nextState = State.Uninitialized;
  28. private const int STX = 0xFA;
  29. #endregion
  30. #region Logger
  31. static NLog.Logger innerLogger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("Communicator");
  32. #endregion
  33. public StateMachineMessageCutter()
  34. {
  35. window = new SizableWindow<byte>();
  36. window.OnWindowFull += (data) =>
  37. {
  38. byte[] tempBytes = window.ToArray();
  39. string receiveStr = BitConverter.ToString(tempBytes).Replace("-", " ");
  40. //innerLogger.Info($"receive data : ${receiveStr}");
  41. int index = 0;//开始查找包头的下标
  42. //循环处理,避免数据粘包发送过来时只处理了第一个包
  43. while (true)
  44. {
  45. //查找包头位置
  46. int fristIndex = -1; //第一个包头位置
  47. int secondIndex = -1; //第二个包头位置
  48. for (; index < tempBytes.Length - 1; index++)
  49. {
  50. //fa+fa,为数据中带fa,跳过多加的 1 byte Fa
  51. if (tempBytes[index] == STX && tempBytes[index+1] == STX)
  52. {
  53. index++;
  54. continue;
  55. }
  56. if (tempBytes[index] == STX && tempBytes[index + 1] != STX) // FA+非Fa ,包头
  57. {
  58. if (fristIndex == -1)
  59. {
  60. fristIndex = index;
  61. }
  62. else
  63. {
  64. secondIndex = index;
  65. break;
  66. }
  67. }
  68. }
  69. //innerLogger.Info($"find head fristIndex = {fristIndex},secondIndex = {secondIndex}");
  70. //若没有找到包头,证明目前缓存中都是垃圾数据,清掉
  71. if (fristIndex == -1)
  72. {
  73. window.RemoveRange(0, tempBytes.Length);
  74. return;
  75. }
  76. //长度不够表示部分数据还未接收到,等待下一轮接收数据再处理
  77. //1byte FA + 1 byte 目标地址 + 1 byte 源地址 + 1 byte 帧号 + 2 byte 有效数据长度 + 2 byte 数据校验 = 8byte
  78. if (fristIndex + 8 > tempBytes.Length)
  79. {
  80. innerLogger.Info($"Insufficient data length,at least ,need {fristIndex + 8},but length is {tempBytes.Length}");
  81. return;
  82. }
  83. //获取有效数据长度
  84. int dataLen = Bcd2Int(tempBytes[fristIndex + 4], tempBytes[fristIndex + 5]);
  85. byte[] reduceFAData = Reduce0xFAPair(tempBytes,fristIndex,secondIndex);
  86. string reduceFADataStr = BitConverter.ToString(reduceFAData).Replace("-", " ");
  87. //innerLogger.Info($"data length is ${dataLen},reduceFAData len is ${reduceFAData.Length},value is ${reduceFADataStr}");
  88. //去除多余FA后,实际数据不足有效数据长度,表示有粘包,仍非完整数据,等待下一轮接收数据再处理
  89. //1byte FA + 1 byte 目标地址 + 1 byte 源地址 + 1 byte 帧号 + 2 byte 有效数据长度 + dataLen byte 数据 + 2 byte 数据校验
  90. if (reduceFAData.Length < 6 + dataLen + 2)
  91. {
  92. innerLogger.Info("Insufficient data length");
  93. return;
  94. }
  95. //计算crc,若值对应不上,表示数据错误
  96. ushort calculatorValue = HengshanCRC16.ComputeChecksum(reduceFAData.AsSpan(1, 5 + dataLen).ToArray());
  97. byte highByte = reduceFAData[6 + dataLen];
  98. byte lowByte = reduceFAData[7 + dataLen];
  99. int crcValue = (highByte << 8) | lowByte;
  100. if (calculatorValue != crcValue)
  101. {
  102. //crc验证不通过,第一个包数据不正常,进入下一个循环,包头从第二个包头下标开始找(若没找到下个包头直接返回等待下一轮接收数据)
  103. innerLogger.Info($"crc value error,get value is {crcValue},calculator value is {calculatorValue}");
  104. if(secondIndex == -1)
  105. {
  106. window.RemoveRange(0, tempBytes.Length);
  107. return;
  108. }
  109. }
  110. //获取到实际数据包,并将缓存数据删除
  111. Message = reduceFAData;
  112. if (reduceFAData[6] != 0x10)
  113. {
  114. string reduceFaDataStr = BitConverter.ToString(reduceFAData).Replace("-", " ");
  115. innerLogger.Info($"receive reduceFa data : {reduceFaDataStr}");
  116. }
  117. var safe = OnMessageCut;
  118. safe?.Invoke(this, null);
  119. //没有第二个包,清除缓存并结束
  120. if(secondIndex == -1)
  121. {
  122. window.RemoveRange(0, tempBytes.Length);
  123. return;
  124. }
  125. }
  126. };
  127. }
  128. public void Feed(byte[] data)
  129. {
  130. List<byte> list = new(data);
  131. window.Add(list);
  132. //for (int i = 0; i < data.Length; i++)
  133. //{
  134. // window.Add(data[i]);
  135. //}
  136. }
  137. private int Get0xFAPairCountInWindow(IEnumerable<byte> data)
  138. {
  139. return (int)Math.Round(((double)(data.Count(w => w == 0xFA)) / 2), MidpointRounding.AwayFromZero);
  140. }
  141. /// <summary>
  142. /// 删除多余的FA,在接收到的一个完整数据包中,数据包头为FA,其余数据中每一个FA会替换为两个FA,这里要删除多余的fa来获取实际数据包
  143. /// </summary>
  144. /// <param name="data">原数据包</param>
  145. /// <param name="firstIndex">第一个包头下标</param>
  146. /// <param name="secondIndex">第二个包头下标</param>
  147. /// <returns></returns>
  148. public byte[] Reduce0xFAPair(byte[] data,int firstIndex,int secondIndex)
  149. {
  150. int endIndex = data.Length;
  151. if (secondIndex != -1 && secondIndex > firstIndex) endIndex = secondIndex;
  152. List<byte> list = new();
  153. for (int index = firstIndex; index < endIndex; index++)
  154. {
  155. if (index < data.Length -1 && data[index] == STX && data[index + 1] == STX)
  156. {
  157. list.Add(STX);
  158. index++;
  159. }
  160. else
  161. {
  162. list.Add(data[index]);
  163. }
  164. }
  165. return list.ToArray();
  166. }
  167. public int Bcd2Int(byte byte1,byte byte2)
  168. {
  169. // 提取第一个字节的高四位和低四位
  170. int digit1 = (byte1 >> 4) & 0x0F; // 高四位
  171. int digit2 = byte1 & 0x0F; // 低四位
  172. // 提取第二个字节的高四位和低四位
  173. int digit3 = (byte2 >> 4) & 0x0F; // 高四位
  174. int digit4 = byte2 & 0x0F; // 低四位
  175. // 组合成一个整数
  176. int result = digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4;
  177. return result;
  178. }
  179. }
  180. }