AEOR4000TConnection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Aitex.Core.RT.Log;
  4. using MECF.Framework.Common.Communications;
  5. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temperatures.AEs
  6. {
  7. public class AETempMessage : BinaryMessage
  8. {
  9. public byte DeviceAddress { get; set; }
  10. public byte FunctionCode { get; set; }
  11. public byte Length { get; set; }
  12. public byte[] Data { get; set; }
  13. }
  14. public class AETempAsciiMessage : AsciiMessage
  15. {
  16. public string Data { get; set; }
  17. public string ErrorText { get; set; }
  18. }
  19. class AEOR4000TConnection : TCPPortConnectionBase
  20. {
  21. private List<byte> _lstCacheBuffer = new List<byte>();
  22. public AEOR4000TConnection(string address) : base(address, "\r\n", true)
  23. {
  24. }
  25. public override bool SendMessage(string message)
  26. {
  27. return base.SendMessage(message);
  28. }
  29. public override bool SendMessage(byte[] message)
  30. {
  31. _lstCacheBuffer.Clear();
  32. return base.SendMessage(message);
  33. }
  34. protected override MessageBase ParseResponse(byte[] rawMessage)
  35. {
  36. _lstCacheBuffer.AddRange(rawMessage);
  37. byte[] temps = _lstCacheBuffer.ToArray();
  38. AETempMessage msg = new AETempMessage();
  39. msg.IsResponse = false;
  40. msg.IsAck = false;
  41. msg.IsComplete = false;
  42. msg.RawMessage = _lstCacheBuffer.ToArray();
  43. if (temps.Length < 9) return msg;
  44. msg.DeviceAddress = temps[6];
  45. msg.FunctionCode = temps[7];
  46. if (msg.FunctionCode == 0x03)
  47. {
  48. msg.Length = temps[8];
  49. if (temps.Length != 9)
  50. {
  51. if (temps.Length < msg.Length + 9) return msg;
  52. }
  53. msg.Data = _lstCacheBuffer.Skip(9).Take(msg.Length).ToArray();
  54. }
  55. else
  56. {
  57. msg.Data = _lstCacheBuffer.ToArray();
  58. }
  59. msg.IsResponse = true;
  60. msg.IsAck = true;
  61. msg.IsComplete = true;
  62. return msg;
  63. }
  64. protected override MessageBase ParseResponse(string rawText)
  65. {
  66. AETempAsciiMessage msg = new AETempAsciiMessage();
  67. msg.RawMessage = rawText;
  68. if (rawText.Length <= 0)
  69. {
  70. LOG.Error($"empty response");
  71. msg.IsFormatError = true;
  72. return msg;
  73. }
  74. //if (rawText.Length <= 4)
  75. //{
  76. // LOG.Error($"too short response");
  77. // msg.IsFormatError = true;
  78. // return msg;
  79. //}
  80. msg.Data = rawText.Replace("\r", "").Replace("\n", "");
  81. if (msg.Data.ToUpper().StartsWith("ERR"))
  82. {
  83. var errorCodeString = msg.Data.ToUpper().Replace("ERR", "").Replace(" ", "");
  84. int.TryParse(errorCodeString, out int errorCode);
  85. if (errorCode > 0)
  86. {
  87. msg.ErrorText = errorCodeString;
  88. msg.IsError = true;
  89. }
  90. }
  91. msg.IsResponse = true;
  92. msg.IsAck = true;
  93. return msg;
  94. }
  95. }
  96. }