1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using Edge.Core.Parser.BinaryParser;
- using Edge.Core.Parser.BinaryParser.MessageEntity;
- using Edge.Core.Parser.BinaryParser.Util;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LanTian_Pump_664_Or_886
- {
- public class Parser : ParserBase
- {
- public Parser() : base(new MessageTemplateLookup())
- {
- }
- public override byte[] Serialize(MessageTemplateBase message)
- {
- //F5 01 An dd dd ... dd dd CmdByte XRL
- var bytes = base.Serialize(message).ToList();
- // fill length
- bytes.RemoveAt(2);
- bytes.Insert(2, (byte)(0xA0 + bytes.Count - 3 + 1));
- // get special XRL
- var xrl = GetXRL(bytes.Take(bytes.Count - 1).ToList());
- return bytes.Take(bytes.Count - 1).Concat(new byte[] { xrl }).ToArray();
- }
- //public override MessageTemplateBase Deserialize(byte[] rawData)
- //{
- // if (rawData.Length > 4)
- // {
- // // exclude last 4 byte which are (2 bytes CRC + 1 byte EOT + 1byte 0xFA)
- // return base.Deserialize(rawData.Take(rawData.Length - 4).ToArray());
- // }
- // else
- // {
- // // exclude last 1 byte of 0xFA
- // return base.Deserialize(rawData.Take(rawData.Length - 1).ToArray());
- // }
- //}
- //private List<byte> ProcessXRLInMsgBody(List<byte> bytesWithoutXRL)
- //{
- // var bytesUsedToComputeXRL = bytesWithoutXRL.GetRange(2, bytesWithoutXRL.Count - 3).ToArray();
- // byte XRL = 0;
- // foreach (var b in bytesUsedToComputeXRL)
- // XRL = (byte)(XRL ^ b);
- // bytesWithoutXRL[bytesWithoutXRL.Count - 1] = XRL;
- // return bytesWithoutXRL;
- //}
- public static byte GetXRL(List<byte> bytes)
- {
- byte XRL = 0;
- foreach (var b in bytes)
- XRL = (byte)(XRL ^ b);
- var final = XRL & 0x7F;
- return (byte)final;
- }
- }
- }
|