FormatAttribute.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Edge.Core.Parser.BinaryParser.Attributes
  7. {
  8. [AttributeUsage(AttributeTargets.Property)]
  9. public class FormatAttribute : AttributeBase
  10. {
  11. private FormatAttribute() { }
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. /// <param name="fixedLength">the field length is a constant fixed value, like 1, 2.</param>
  16. /// <param name="encodingType"></param>
  17. /// <param name="index"></param>
  18. public FormatAttribute(int fixedLength, EncodingType encodingType, int index)
  19. {
  20. base.FixedLengthOrCount = fixedLength;
  21. base.EncodingType = encodingType;
  22. base.Index = index;
  23. }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="lengthLink">the field is a variable length and determined by another field with numerical value, and the
  28. /// numerical value directly reflect the original field length, like field 'IamTheLengthLink' has the value 8, then the
  29. /// linking field length is 8.</param>
  30. /// <param name="encodingType"></param>
  31. /// <param name="index"></param>
  32. public FormatAttribute(string lengthLink, EncodingType encodingType, int index)
  33. {
  34. base.LengthOrCountLink = lengthLink;
  35. base.EncodingType = encodingType;
  36. base.Index = index;
  37. }
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="lengthLink">the field is a variable length and determined by another field with numerical value, and the
  42. /// numerical value is NOT directly reflect the original field length, have to use the argument 'lengthLinkExpression' together
  43. /// to detect the length, like field 'IamTheLengthLink' has the value 8, and the 'lengthLinkExpression' contains "8:12;", then the
  44. /// original field length is actually 12.</param>
  45. /// <param name="lengthLinkExpression">like "3:8;A:9", means field 'lengthLink' has value 3, then original field length is 8, if has
  46. /// value A, then original field length is 9</param>
  47. /// <param name="encodingType"></param>
  48. /// <param name="index"></param>
  49. public FormatAttribute(string lengthLink, string lengthLinkExpression, EncodingType encodingType, int index)
  50. {
  51. base.LengthOrCountLink = lengthLink;
  52. base.LengthOrCountLinkExpression = lengthLinkExpression;
  53. base.EncodingType = encodingType;
  54. base.Index = index;
  55. }
  56. public FormatAttribute(int fixedLength, string operationExpression, EncodingType encodingType, int index)
  57. {
  58. base.FixedLengthOrCount = fixedLength;
  59. base.LengthOrCountLink = "";
  60. base.LengthOrCountLinkExpression = operationExpression;
  61. base.EncodingType = encodingType;
  62. base.Index = index;
  63. }
  64. //public FormatAttribute(string switchedBy, byte[] determinedBy, int index)
  65. //{
  66. // this.LengthLink = lengthLink;
  67. // base.EncodingType = encodingType;
  68. // base.Index = index;
  69. //}
  70. /// <summary>
  71. /// it's a variable length, and no LengthLink
  72. /// </summary>
  73. /// <param name="encodingType"></param>
  74. /// <param name="index"></param>
  75. //public FormatAttribute(EncodingType encodingType, int index)
  76. //{
  77. // base.EncodingType = encodingType;
  78. // base.Index = index;
  79. //}
  80. }
  81. [Flags]
  82. public enum EncodingType
  83. {
  84. BIN = 0,
  85. /// <summary>
  86. /// BCD encoded in bytes, like 0x64, after BCD decoded, it's 64 in decimal base.
  87. /// output int value.
  88. /// </summary>
  89. BCD = 1,
  90. /// <summary>
  91. /// ASCII encoded in bytes, like 0x3233, after ASCII decoded, it's a string of 23.
  92. /// If fixed length specified, then pad right with ' ' and then ASCII encoding.
  93. /// </summary>
  94. ASCII = 2,
  95. /// <summary>
  96. /// BCD encoded in bytes, but the output is a string, like 0x02 56 01 28 64 32 16 08 04 02 01 will turn to 0256012864321608040201.
  97. /// typically this kind of case should choose ASCII, but somehow...
  98. /// </summary>
  99. BcdString = 4,
  100. /* following are some 'special' encoding type, most likely the protocol maker
  101. made some unconsistent choice, we have to support it.
  102. */
  103. /// <summary>
  104. /// ASCII encoded in bytes,like 0x3233 output is a int 23.
  105. /// </summary>
  106. ASCIIInt = 8,
  107. /// <summary>
  108. /// ASCII encoded in bytes,like 0x3233 output is a long 23.
  109. /// </summary>
  110. ASCIILong = 16,
  111. /// <summary>
  112. /// Direct assignment of hex string, e.g.
  113. /// "12598A93F22754F46B1720988B8C1C24" output is a byte array: 0x12 0x59...
  114. /// </summary>
  115. HexString,
  116. /// <summary>
  117. /// Hex string stored in reverse order.
  118. /// "6DCA9D06", the physical card number, used in system as: 06 9D CA 6D
  119. /// </summary>
  120. ReverseHexString,
  121. /// <summary>
  122. /// ASCII encoded in bytes, like 0x3233, after ASCII decoded, it's a string of 23.
  123. /// If fixed length specified, then pad left with '0' and then ASCII encoding.
  124. /// </summary>
  125. ASCII_PadLeftWithZero = 32,
  126. }
  127. }