OCSMessage.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GSG.NET.Utils;
  7. namespace OHVConnector
  8. {
  9. public enum eKind
  10. {
  11. Unknown, // Not Define
  12. E, //Error
  13. S, //State
  14. B, //Battery
  15. T, //
  16. C,
  17. P,
  18. I,//수동명령보고,응답
  19. O,
  20. A,//Alive Check
  21. L,
  22. U,
  23. F,
  24. M,//Move
  25. R,
  26. H,
  27. Z,
  28. }
  29. public class OCSMessage
  30. {
  31. const byte STX = 0x02;
  32. const byte ETX = 0x03;
  33. public string Id { get; set; }
  34. public eKind Kind { get; set; }
  35. //SubCode 만들기 위한 Bool
  36. public bool IsSubCode1 { get; set; } = false;
  37. public bool IsSubCode2 { get; set; } = false;
  38. public bool IsSubCode3 { get; set; } = false;
  39. public string RevID { get; set; }
  40. public string SendID { get; set; }
  41. public string Tag { get; set; }
  42. public string SubCode { get; set; }
  43. public byte CheckSum { get; set; }
  44. public OCSMessage()
  45. {
  46. }
  47. public string LogFormat()
  48. {
  49. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.SubCode}]";
  50. }
  51. public int SubCodeToInt()
  52. {
  53. int result = 0;
  54. if ( string.IsNullOrEmpty( this.SubCode ) )
  55. {
  56. StringBuilder sb = new StringBuilder();
  57. sb.Append( this.IsSubCode1 ? "1" : "0" );
  58. sb.Append( this.IsSubCode2 ? "1" : "0" );
  59. sb.Append( this.IsSubCode3 ? "1" : "0" );
  60. int.TryParse( sb.ToString(), out result );
  61. }
  62. else
  63. int.TryParse( this.SubCode, out result );
  64. return result;
  65. }
  66. string MakeSubcode()
  67. {
  68. if ( !string.IsNullOrEmpty(this.SubCode) )
  69. {
  70. return this.SubCode;
  71. }
  72. else
  73. {
  74. StringBuilder sb = new StringBuilder();
  75. sb.Append( this.IsSubCode1 ? "1" : "0" );
  76. sb.Append( this.IsSubCode2 ? "1" : "0" );
  77. sb.Append( this.IsSubCode3 ? "1" : "0" );
  78. return this.SubCode = sb.ToString();
  79. }
  80. }
  81. public MemoryBuffer ToMemoryBuffer()
  82. {
  83. var mb = new MemoryBuffer();
  84. mb.Append( STX );
  85. mb.AppendAscii( RevID );
  86. mb.AppendAscii( SendID );
  87. mb.AppendAscii( this.Kind.ToString() );
  88. mb.AppendAscii( this.Tag );
  89. mb.AppendAscii( MakeSubcode() );
  90. mb.Append( GetCheckSum( mb.ToBytes ) );
  91. mb.Append( ETX );
  92. return mb;
  93. }
  94. public byte GetCheckSum()
  95. {
  96. var mb = new MemoryBuffer( 18 );
  97. mb.Append( STX );
  98. mb.AppendAscii( RevID );
  99. mb.AppendAscii( SendID );
  100. mb.AppendAscii( this.Kind.ToString() );
  101. mb.AppendAscii( this.Tag );
  102. mb.AppendAscii( MakeSubcode() );
  103. return GetCheckSum( mb.ToBytes );
  104. }
  105. byte GetCheckSum( byte[] bs )
  106. {
  107. byte rb = 0;
  108. foreach ( var item in bs )
  109. {
  110. rb += item;
  111. }
  112. return (byte)( rb & 0xff );
  113. }
  114. }
  115. }