StateMachineMessageCutter.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Edge.Core.Processor.Communicator;
  9. namespace OPW_VaporRecoveryOnlineWatch_PressureGage
  10. {
  11. public class StateMachineMessageCutter : IMessageCutter<byte[]>
  12. {
  13. public byte[] Message { get; private set; }
  14. public event EventHandler OnMessageCut;
  15. public event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
  16. static NLog.Logger innerLogger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("PumpHandler");
  17. private string loggerAppendix = "OPW_VaporRecoveryOnlineWatch_PressureGage msgCutter ";
  18. private readonly List<byte> buffer = new List<byte>();
  19. /// <summary>
  20. /// </summary>
  21. public StateMachineMessageCutter()
  22. {
  23. }
  24. public void Feed(byte[] next)
  25. {
  26. for (int i = 0; i < next.Length; i++)
  27. {
  28. if ((next[i] == 0xFE && this.buffer.Count == 0)
  29. || (next[i] != 0xFE && this.buffer.Count != 0))
  30. {
  31. this.buffer.Add(next[i]);
  32. }
  33. if (this.buffer.Count == 7)
  34. {
  35. this.Message = this.buffer.ToArray();
  36. var safe = this.OnMessageCut;
  37. safe?.Invoke(this, null);
  38. this.buffer.Clear();
  39. }
  40. }
  41. }
  42. }
  43. }