Parser.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Edge.Core.Parser.BinaryParser;
  2. using Edge.Core.Parser.BinaryParser.MessageEntity;
  3. using Edge.Core.Parser.BinaryParser.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace LanTian_Pump_664_Or_886
  10. {
  11. public class Parser : ParserBase
  12. {
  13. public Parser() : base(new MessageTemplateLookup())
  14. {
  15. }
  16. public override byte[] Serialize(MessageTemplateBase message)
  17. {
  18. //F5 01 An dd dd ... dd dd CmdByte XRL
  19. var bytes = base.Serialize(message).ToList();
  20. // fill length
  21. bytes.RemoveAt(2);
  22. bytes.Insert(2, (byte)(0xA0 + bytes.Count - 3 + 1));
  23. // get special XRL
  24. var xrl = GetXRL(bytes.Take(bytes.Count - 1).ToList());
  25. return bytes.Take(bytes.Count - 1).Concat(new byte[] { xrl }).ToArray();
  26. }
  27. //public override MessageTemplateBase Deserialize(byte[] rawData)
  28. //{
  29. // if (rawData.Length > 4)
  30. // {
  31. // // exclude last 4 byte which are (2 bytes CRC + 1 byte EOT + 1byte 0xFA)
  32. // return base.Deserialize(rawData.Take(rawData.Length - 4).ToArray());
  33. // }
  34. // else
  35. // {
  36. // // exclude last 1 byte of 0xFA
  37. // return base.Deserialize(rawData.Take(rawData.Length - 1).ToArray());
  38. // }
  39. //}
  40. //private List<byte> ProcessXRLInMsgBody(List<byte> bytesWithoutXRL)
  41. //{
  42. // var bytesUsedToComputeXRL = bytesWithoutXRL.GetRange(2, bytesWithoutXRL.Count - 3).ToArray();
  43. // byte XRL = 0;
  44. // foreach (var b in bytesUsedToComputeXRL)
  45. // XRL = (byte)(XRL ^ b);
  46. // bytesWithoutXRL[bytesWithoutXRL.Count - 1] = XRL;
  47. // return bytesWithoutXRL;
  48. //}
  49. public static byte GetXRL(List<byte> bytes)
  50. {
  51. byte XRL = 0;
  52. foreach (var b in bytes)
  53. XRL = (byte)(XRL ^ b);
  54. var final = XRL & 0x7F;
  55. return (byte)final;
  56. }
  57. }
  58. }