MessageCutter.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;
  2. using Edge.Core.Parser.BinaryParser.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Edge.Core.Processor.Communicator;
  7. namespace ShellChina_EPS_Project_CarPlatePay_EpsClient_App
  8. {
  9. /// <summary>
  10. /// 报文组成格式:2字节报文长度(binary, 不包含自己的2字节) + 5字节TPDU + 2字节MTI + 8字节位图 + 报文内容
  11. /// </summary>
  12. public class MessageCutter : IMessageCutter<byte[]>
  13. {
  14. public byte[] Message { get; private set; }
  15. public event EventHandler OnMessageCut;
  16. public event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
  17. //static ILog innerLogger = log4net.LogManager.GetLogger("StateMachine");
  18. static NLog.Logger innerLogger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("Communicator");
  19. private readonly List<byte> buffer = new List<byte>();
  20. private int fullMsgLength = 0;
  21. ///
  22. /// Message format:
  23. /// ADR CTRL trans_Number trans_Length trans_data CRC-1 CRC-2 ETX(0x03) SF(0xFA)
  24. /// </summary>
  25. public MessageCutter()
  26. {
  27. }
  28. public void Feed(byte[] next)
  29. {
  30. for (int i = 0; i < next.Length; i++)
  31. {
  32. this.buffer.Add(next[i]);
  33. if (this.buffer.Count == 2)
  34. {
  35. this.fullMsgLength = (this.buffer[0] << 8) + this.buffer[1] + 2;
  36. }
  37. if (this.buffer.Count == this.fullMsgLength)
  38. {
  39. var e = this.OnMessageCut;
  40. this.Message = this.buffer.ToArray();
  41. e?.Invoke(this, null);
  42. this.fullMsgLength = 0;
  43. this.buffer.Clear();
  44. }
  45. }
  46. }
  47. }
  48. }