MessageCutter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Edge.Core.Parser.BinaryParser.Util;
  2. using Edge.Core.Processor.Communicator;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace FR_Pump_HaoSheng
  8. {
  9. public class MessageCutter : IMessageCutter<byte[]>
  10. {
  11. private readonly List<byte> buffer = new List<byte>();
  12. public byte[] Message { get; private set; }
  13. public event EventHandler OnMessageCut;
  14. public event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
  15. /// <summary>
  16. /// 富仁豪升通讯协议补充
  17. /// 同步头 FA + 00 + 00 + 00 + 有效数据长度(2Bytes) + 有效数据 + CRC16(2Bytes)
  18. /// </summary>
  19. /// <param name="data"></param>
  20. public void Feed(byte[] data)
  21. {
  22. for (int i = 0; i < data.Length; i++)
  23. {
  24. this.buffer.Add(data[i]);
  25. if (this.buffer[0] != 0xFA)
  26. {
  27. this.OnInvalidMessageRead?.Invoke(this,
  28. new MessageCutterInvalidMessageReadEventArg()
  29. {
  30. Message =
  31. "invalid byte[0]: 0x"
  32. + this.buffer[0].ToString("x2")
  33. + " clear buf and continue reading..."
  34. });
  35. this.buffer.Clear();
  36. continue;
  37. }
  38. if (this.buffer.Count >= 2
  39. && this.buffer[1] != 0x00)
  40. {
  41. this.OnInvalidMessageRead?.Invoke(this,
  42. new MessageCutterInvalidMessageReadEventArg()
  43. {
  44. Message =
  45. "invalid byte[1]: 0x"
  46. + this.buffer[1].ToString("x2")
  47. + " clear buf and continue reading..."
  48. });
  49. this.buffer.Clear();
  50. continue;
  51. }
  52. if (this.buffer.Count >= 3
  53. //第三个字节为油枪状态,0 – 挂枪, 1 – 抬枪
  54. && this.buffer[2] > 1)
  55. {
  56. this.OnInvalidMessageRead?.Invoke(this,
  57. new MessageCutterInvalidMessageReadEventArg()
  58. {
  59. Message =
  60. "invalid byte[2]: 0x"
  61. + this.buffer[2].ToString("x2")
  62. + ", 第三个字节为油枪状态,0 – 挂枪, 1 – 抬枪, clear buf and continue reading..."
  63. });
  64. this.buffer.Clear();
  65. continue;
  66. }
  67. if (this.buffer.Count >= 4
  68. //00 为 1 号枪,01 为 2 号枪
  69. && this.buffer[3] > 1)
  70. {
  71. this.OnInvalidMessageRead?.Invoke(this,
  72. new MessageCutterInvalidMessageReadEventArg()
  73. {
  74. Message =
  75. "invalid byte[3]: 0x"
  76. + this.buffer[2].ToString("x2")
  77. + ", 00 为 1 号枪,01 为 2 号枪, clear buf and continue reading..."
  78. });
  79. this.buffer.Clear();
  80. continue;
  81. }
  82. if (this.buffer.Count >= 8 &&
  83. this.buffer.Count == (this.buffer.Skip(4).Take(2).GetBCD() + 8))
  84. {
  85. this.Message = this.buffer.ToArray();
  86. var safe = this.OnMessageCut;
  87. safe?.Invoke(this, null);
  88. this.buffer.Clear();
  89. }
  90. if (this.buffer.Count >= 500)
  91. {
  92. this.OnInvalidMessageRead?.Invoke(this,
  93. new MessageCutterInvalidMessageReadEventArg()
  94. {
  95. Message = "Long length(len: " + this.buffer.Count + ") message is still constructing in MsgCutter: 0x" + this.buffer.ToHexLogString()
  96. });
  97. this.buffer.Clear();
  98. continue;
  99. }
  100. }
  101. }
  102. }
  103. }