KaJiLianDongV11MessageTemplateBase.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Edge.Core.Parser.BinaryParser.Attributes;
  2. using Edge.Core.Parser.BinaryParser.MessageEntity;
  3. using Edge.Core.Parser.BinaryParser.Util;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. namespace FuRen_Sinopec_IcCardReader
  10. {
  11. [Serializable]
  12. public abstract class KaJiLianDongV11MessageTemplateBase : MessageTemplateBase
  13. {
  14. /// <summary>
  15. /// which side send the message, the value is parsed from `FrameSequenceAndControlRaw`.
  16. /// </summary>
  17. public enum MessageCallerSide
  18. {
  19. Pump,
  20. PC
  21. }
  22. /// <summary>
  23. /// 数据包头,同步头
  24. /// </summary>
  25. [Format(1, EncodingType.BIN, -100)]
  26. public virtual byte Prefix
  27. {
  28. get { return 0xFA; }
  29. set
  30. {
  31. ;
  32. }
  33. }
  34. /// <summary>
  35. /// 在 PC 机到加油 机的通讯中是 加油机的通讯 终端的逻辑编 号 POS-P;一个 逻辑编号对应 一个通讯的物 理端口
  36. /// </summary>
  37. [Format(1, EncodingType.BIN, -99)]
  38. public virtual byte TargetAddress { get; set; }
  39. /// <summary>
  40. /// 在加油机到 PC 机 的通讯是加油机 的通讯终端的逻 辑编号 POS-P .
  41. /// PC 机的地址范围:0xE0~0xF9
  42. /// </summary>
  43. [Format(1, EncodingType.BIN, -98)]
  44. public virtual byte SourceAddress { get; set; }
  45. /// <summary>
  46. /// 帧号/控制
  47. /// </summary>
  48. [Format(1, EncodingType.BIN, -97)]
  49. public virtual byte FrameSequenceAndControlRaw { get; set; }
  50. /// <summary>
  51. /// which side send the message, the value is parsed from `FrameSequenceAndControlRaw`.
  52. /// </summary>
  53. public virtual MessageCallerSide GetMessageCallerSide()
  54. {
  55. var callerBit = this.FrameSequenceAndControlRaw.GetBit(6);
  56. if (callerBit == 1) return MessageCallerSide.PC;
  57. return MessageCallerSide.Pump;
  58. }
  59. /// <summary>
  60. /// which side send the message, the value is parsed from `FrameSequenceAndControlRaw`.
  61. /// </summary>
  62. public void SetMessageCallerSide(MessageCallerSide side)
  63. {
  64. this.FrameSequenceAndControlRaw = this.FrameSequenceAndControlRaw.SetBit(6, 6, side == MessageCallerSide.PC ? 1 : 0);
  65. }
  66. /// <summary>
  67. /// sequence number of the message, the value is parsed from `FrameSequenceAndControlRaw`.
  68. /// </summary>
  69. public virtual int GetMessageSequenceNumber()
  70. {
  71. var debug = (this.FrameSequenceAndControlRaw << 2);
  72. var d = debug.GetBinBytes(4).Last();
  73. var r = d >> 2;
  74. return r;
  75. }
  76. /// <summary>
  77. /// sequence number of the message, the value is parsed from `FrameSequenceAndControlRaw`.
  78. /// 主叫方每发 送一新帧, 此帧号加一,应答方 回送此帧号
  79. /// </summary>
  80. public virtual void SetMessageSequenceNumber(int sequenceNumber)
  81. {
  82. // sequence number is max 5 bits.
  83. if (sequenceNumber > 63) throw new ArgumentOutOfRangeException("maximum sequenceNumber is 63(total 6 bits).");
  84. var debug = this.FrameSequenceAndControlRaw >> 6 << 6;
  85. this.FrameSequenceAndControlRaw = (byte)(debug + sequenceNumber);
  86. }
  87. /// <summary>
  88. /// 有效数据长度, 压缩 BCD,转 义字符 不计入 其中
  89. /// </summary>
  90. [FormatAttribute(2, "%OnSerializingBytesCount", EncodingType.BCD, -96)]
  91. public virtual int DataLength { get; set; }
  92. ///// <summary>
  93. ///// 缩 BCD,转 义字符 不计入 其中 数据 的长 度为 “有 效数 据长 度” 的值
  94. ///// </summary>
  95. [FormatAttribute(1, EncodingType.BIN, 0)]
  96. public virtual byte HANDLE { get; set; }
  97. [EnumerableFormat(2, 1000, EncodingType = EncodingType.BIN)]
  98. public virtual List<byte> CRC16 { get; set; }
  99. /// <summary>
  100. /// Gets the code for the message, the code is variant length, most of them are 1 or 2 byte.
  101. /// </summary>
  102. //public virtual List<byte> Code { get; protected set; }
  103. //protected KaJiLianDongV11MessageTemplateBase()
  104. //{
  105. // // fusion always the host, the pump side msg sender is from a system embeded in pump.
  106. // this.SetMessageCallerSide(MessageCallerSide.Host);
  107. //}
  108. public override string ToLogString()
  109. {
  110. return this.GetType().Name + " "
  111. + base.ToLogString().Replace("FrameSequenceAndControlRaw:",
  112. "(HostSide: " + GetMessageCallerSide() + ", SeqNo.: " + this.GetMessageSequenceNumber() + ")FrameSequenceAndControlRaw:");
  113. }
  114. }
  115. }