StateMachineMessageCutter.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //查找包头位置
  40. int findIndex = -1;
  41. for (int index = 0;index < tempBytes.Length - 1;index++)
  42. {
  43. if (tempBytes[index] == STX && tempBytes[index + 1] != STX)
  44. {
  45. findIndex = index;
  46. break;
  47. }
  48. }
  49. //若没有找到包头,证明目前缓存中都是垃圾数据,清掉
  50. if (findIndex == -1)
  51. {
  52. window.RemoveRange(0, tempBytes.Length);
  53. return;
  54. }
  55. //长度不够表示有粘包,部分数据还未接收到,等待下一轮接收数据再处理
  56. //1byte FA + 1 byte 目标地址 + 1 byte 源地址 + 1 byte 帧号 + 2 byte 有效数据长度 + 2 byte 数据校验 = 8byte
  57. if(findIndex + 8 > tempBytes.Length)
  58. {
  59. innerLogger.Info($"Insufficient data length,at least ,need {findIndex + 8},but length is {tempBytes.Length}");
  60. return;
  61. }
  62. //获取有效数据长度
  63. int dataLen = Bcd2Int(tempBytes[4], tempBytes[5]);
  64. byte[] reduceFAData = Reduce0xFAPair(tempBytes);
  65. //去除多余FA后,实际数据不足有效数据长度,表示有粘包,仍非完整数据,等待下一轮接收数据再处理
  66. if(reduceFAData.Length < dataLen + 2)
  67. {
  68. innerLogger.Info("Insufficient data length");
  69. return;
  70. }
  71. //计算crc,若值对应不上,表示数据错误
  72. ushort calculatorValue = HengshanCRC16.ComputeChecksum(reduceFAData.AsSpan(1,reduceFAData.Length - 3).ToArray());
  73. byte highByte = reduceFAData[reduceFAData.Length - 2];
  74. byte lowByte = reduceFAData[reduceFAData.Length - 1];
  75. int crcValue = (highByte << 8) | lowByte;
  76. if (calculatorValue != crcValue)
  77. {
  78. innerLogger.Info($"crc value error,get value is {crcValue},calculator value is {calculatorValue}");
  79. return;
  80. }
  81. //获取到实际数据包,并将缓存数据删除
  82. Message = reduceFAData;
  83. var safe = OnMessageCut;
  84. safe?.Invoke(this, null);
  85. window.RemoveRange(0, tempBytes.Length);
  86. };
  87. }
  88. public void Feed(byte[] data)
  89. {
  90. List<byte> list = new(data);
  91. window.Add(list);
  92. //for (int i = 0; i < data.Length; i++)
  93. //{
  94. // window.Add(data[i]);
  95. //}
  96. }
  97. private int Get0xFAPairCountInWindow(IEnumerable<byte> data)
  98. {
  99. return (int)Math.Round(((double)(data.Count(w => w == 0xFA)) / 2), MidpointRounding.AwayFromZero);
  100. }
  101. /// <summary>
  102. /// 删除多余的FA,在接收到的一个完整数据包中,数据包头为FA,其余数据中每一个FA会替换为两个FA,这里要删除多余的fa来获取实际数据包
  103. /// </summary>
  104. /// <param name="data">原数据包</param>
  105. /// <returns></returns>
  106. public byte[] Reduce0xFAPair(byte[] data)
  107. {
  108. List<byte> list = new();
  109. for (int index = 0; index < data.Length; index++)
  110. {
  111. if (index < data.Length -1 && data[index] == STX && data[index + 1] == STX)
  112. {
  113. list.Add(STX);
  114. index++;
  115. }
  116. else
  117. {
  118. list.Add(data[index]);
  119. }
  120. }
  121. return list.ToArray();
  122. }
  123. public int Bcd2Int(byte byte1,byte byte2)
  124. {
  125. // 提取第一个字节的高四位和低四位
  126. int digit1 = (byte1 >> 4) & 0x0F; // 高四位
  127. int digit2 = byte1 & 0x0F; // 低四位
  128. // 提取第二个字节的高四位和低四位
  129. int digit3 = (byte2 >> 4) & 0x0F; // 高四位
  130. int digit4 = byte2 & 0x0F; // 低四位
  131. // 组合成一个整数
  132. int result = digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4;
  133. return result;
  134. }
  135. }
  136. }