DxkdpDcPowerConnection.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using Aitex.Core.RT.Log;
  5. using MECF.Framework.Common.Communications;
  6. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.DCPowers.AE
  7. {
  8. public class DxkdpRfPowerMessage : BinaryMessage
  9. {
  10. public byte Header { get; set; }
  11. public byte CommandNumber { get; set; }
  12. public byte OptionLength { get; set; }
  13. public byte[] Data { get; set; }
  14. public byte CheckSum { get; set; }
  15. public int Address { get; set; }
  16. public int DataLength { get; set; }
  17. public int MessageLength { get; set; }
  18. }
  19. public class DxkdpDcPowerConnection : SerialPortConnectionBase
  20. {
  21. private List<byte> _lstCacheBuffer = new List<byte>();
  22. public DxkdpDcPowerConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  23. : base(portName, baudRate, dataBits, parity, stopBits, "\r", false)
  24. {
  25. }
  26. public override bool SendMessage(byte[] message)
  27. {
  28. _lstCacheBuffer.Clear();
  29. return base.SendMessage(message);
  30. }
  31. protected override MessageBase ParseResponse(byte[] rawBuffer)
  32. {
  33. try
  34. {
  35. _lstCacheBuffer.AddRange(rawBuffer);
  36. DxkdpRfPowerMessage msg = new DxkdpRfPowerMessage();
  37. msg.RawMessage = rawBuffer;
  38. if (_lstCacheBuffer.Count >= 1 && _lstCacheBuffer[0] == 0x06)
  39. {
  40. msg.IsAck = true;
  41. msg.IsResponse = true;
  42. _lstCacheBuffer.RemoveAt(0);
  43. return msg;
  44. }
  45. else if (_lstCacheBuffer.Count > 1 && _lstCacheBuffer[0] == 0xAA)
  46. {
  47. msg.IsAck = true;
  48. }
  49. else if (_lstCacheBuffer.Count == 0)
  50. {
  51. return msg;
  52. }
  53. bool isGetHead = false;
  54. while (!isGetHead && _lstCacheBuffer.Count > 5)
  55. {
  56. if (_lstCacheBuffer[0] != 0xAA)
  57. _lstCacheBuffer.RemoveAt(0);
  58. else
  59. isGetHead = true;
  60. }
  61. if (!isGetHead || _lstCacheBuffer.Count < 5)
  62. {
  63. return msg;
  64. }
  65. msg.Header = _lstCacheBuffer[0];
  66. msg.Address = _lstCacheBuffer[1];
  67. msg.CommandNumber = _lstCacheBuffer[2];
  68. msg.DataLength = _lstCacheBuffer[3];
  69. msg.MessageLength = 5 + msg.DataLength;
  70. if (_lstCacheBuffer.Count < msg.MessageLength)
  71. return msg;
  72. msg.Data = _lstCacheBuffer.GetRange(4, msg.DataLength).ToArray();
  73. byte sum = 0x00;
  74. for (var i = 1; i < msg.MessageLength - 1; i++)
  75. {
  76. sum += _lstCacheBuffer[i];
  77. }
  78. if (_lstCacheBuffer[msg.MessageLength - 1] != sum)
  79. {
  80. LOG.Error($"check sum failed, ");
  81. msg.IsFormatError = true;
  82. _lstCacheBuffer.Clear();
  83. return msg;
  84. }
  85. msg.IsResponse = true;
  86. _lstCacheBuffer.RemoveRange(0, msg.MessageLength);
  87. return msg;
  88. }
  89. catch (Exception ex)
  90. {
  91. throw;
  92. }
  93. }
  94. }
  95. }