1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;
- using Edge.Core.Parser.BinaryParser.Util;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Edge.Core.Processor.Communicator;
- namespace LanTian_Pump_664_Or_886
- {
- public class MessageCutter : IMessageCutter<byte[]>
- {
- private readonly List<byte> buffer = new List<byte>();
- //private List<char> validMessageFirstChars = new List<char>() { 'M', 'D', 'T', 'V', 'X', 'A' };
- public byte[] Message { get; private set; }
- public event EventHandler OnMessageCut;
- public event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
- public void Feed(byte[] data)
- {
- for (int i = 0; i < data.Length; i++)
- {
- this.buffer.Add(data[i]);
- if (this.buffer[0] != 0xF5)
- {
- this.OnInvalidMessageRead?.Invoke(this,
- new MessageCutterInvalidMessageReadEventArg()
- {
- Message =
- "invalid byte[0]: 0x"
- + this.buffer[0].ToString("x2")
- + " clear buf and continue reading..."
- });
- this.buffer.Clear();
- continue;
- }
- if (this.buffer.Count >= 2
- && this.buffer[1] != 0x01)
- {
- this.OnInvalidMessageRead?.Invoke(this,
- new MessageCutterInvalidMessageReadEventArg()
- {
- Message =
- "invalid byte[1]: 0x"
- + this.buffer[1].ToString("x2")
- + " clear buf and continue reading..."
- });
- this.buffer.Clear();
- continue;
- }
- if (this.buffer.Count >= 3
- && ((this.buffer[2] & 0xF0) != 0xA0))
- {
- this.OnInvalidMessageRead?.Invoke(this,
- new MessageCutterInvalidMessageReadEventArg()
- {
- Message =
- "invalid byte[2]: 0x"
- + this.buffer[2].ToString("x2")
- + " clear buf and continue reading..."
- });
- this.buffer.Clear();
- continue;
- }
- if (this.buffer.Count >= 5 &&
- this.buffer.Count == ((this.buffer[2] & 0x0F) + 3))
- {
- this.Message = this.buffer.ToArray();
- var safe = this.OnMessageCut;
- safe?.Invoke(this, null);
- this.buffer.Clear();
- }
- if (this.buffer.Count >= 500)
- {
- this.OnInvalidMessageRead?.Invoke(this,
- new MessageCutterInvalidMessageReadEventArg()
- {
- Message = "Long length(len: " + this.buffer.Count + ") message is still constructing in MsgCutter: 0x" + this.buffer.ToHexLogString()
- });
- this.buffer.Clear();
- continue;
- }
- }
- }
- }
- }
|