TruPlasmaRF1001Connection.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Collections.Generic;
  2. using System.IO.Ports;
  3. using System.Linq;
  4. using Aitex.Core.RT.Log;
  5. using MECF.Framework.Common.Communications;
  6. using MECF.Framework.Common.Utilities;
  7. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFs.TruPlasmaRF
  8. {
  9. public class TruPlasmaRF1001Message : BinaryMessage
  10. {
  11. public string Command { get; set; }
  12. public string ErrorCode { get; set; }
  13. public byte[] Data { get; set; }
  14. }
  15. public class TruPlasmaRF1001Connection : SerialPortConnectionBase
  16. {
  17. private static byte _start = 0xAA;
  18. //private static byte _address = 0x02;
  19. //private static byte _GS = 0x00;
  20. private static byte _stop = 0x55;
  21. private static byte _ack = 0x06;
  22. private List<byte> _msgBuffer = new List<byte>();
  23. public TruPlasmaRF1001Connection(string portName,
  24. int baudRate = 57600,
  25. int dataBits = 8,
  26. Parity parity = Parity.None,
  27. StopBits stopBits = StopBits.One)
  28. : base(portName, baudRate, dataBits, parity, stopBits, "")
  29. {
  30. }
  31. protected override MessageBase ParseResponse(byte[] rawMessage)
  32. {
  33. if (rawMessage == null || rawMessage.Length == 0)
  34. {
  35. LOG.Error("报文长度为空");
  36. return null;
  37. }
  38. TruPlasmaRF1001Message msg = new TruPlasmaRF1001Message();
  39. if (rawMessage.Length == 1 && rawMessage[0] == _ack)
  40. {
  41. msg.Data = rawMessage;
  42. msg.IsResponse = true;
  43. msg.IsAck = true;
  44. return msg;
  45. }
  46. _msgBuffer.AddRange(rawMessage);
  47. if (rawMessage[rawMessage.Length - 1] != _stop)
  48. return msg;
  49. if (_msgBuffer[0] == _ack)
  50. _msgBuffer.RemoveAt(0);
  51. msg.RawMessage = _msgBuffer.ToArray();
  52. _msgBuffer.Clear();
  53. if (msg.RawMessage.Length < 11)
  54. {
  55. msg.IsFormatError = true;
  56. LOG.Error($"报文中数据信息长度错误");
  57. return msg;
  58. }
  59. //if ((msg.RawMessage[3] & 0b_1000_0000) > 0 || (msg.RawMessage[3] & 0b_0100_0000) > 0)
  60. //{
  61. // msg.IsError = true;
  62. // msg.ErrorCode = $"Alarms or error messages are pending";
  63. // LOG.Error($"Alarms or error messages are pending");
  64. // return msg;
  65. //}
  66. if ((msg.RawMessage[3] & 0x08) == 0)
  67. {
  68. msg.IsFormatError = true;
  69. LOG.Error($"RS232 interface is not active");
  70. return msg;
  71. }
  72. if (msg.RawMessage[4] == 0xFE)
  73. {
  74. msg.IsFormatError = true;
  75. LOG.Error($"telegram error");
  76. return msg;
  77. }
  78. if (msg.RawMessage[0] != _start)
  79. {
  80. msg.IsFormatError = true;
  81. LOG.Error($"invalid start byte");
  82. return msg;
  83. }
  84. if (msg.RawMessage.Last() != _stop)
  85. {
  86. msg.IsFormatError = true;
  87. LOG.Error($"invalid stop byte");
  88. return msg;
  89. }
  90. var contentBuffer = msg.RawMessage.Take(msg.RawMessage.Length - 3).ToArray();
  91. var checkSum = Crc16.Crc16Ccitt(contentBuffer);
  92. var recSum = msg.RawMessage[msg.RawMessage.Length - 3] + msg.RawMessage[msg.RawMessage.Length - 2] * 256;
  93. if (recSum != checkSum)
  94. {
  95. LOG.Error($"check sum failed,");
  96. msg.IsFormatError = true;
  97. return msg;
  98. }
  99. if (msg.RawMessage[8] != 0xFF && msg.RawMessage[8] != 0x00)
  100. {
  101. msg.IsError = true;
  102. msg.ErrorCode = msg.RawMessage[8].ToString("X2");
  103. }
  104. var msgArray = msg.RawMessage.Select(bt => bt.ToString("X2")).ToArray();
  105. string msgCommand = string.Join(",", msgArray, 4, 4);
  106. msg.Command = msgCommand;
  107. if (msg.RawMessage.Length > 12 && msg.RawMessage[8] == 0x00)
  108. msg.Data = msg.RawMessage.Skip(10).Take(msg.RawMessage.Length - 13).ToArray();
  109. msg.IsResponse = true;
  110. msg.IsAck = true;
  111. msg.IsComplete = true;
  112. return msg;
  113. }
  114. }
  115. }