RisshiChillerConnection.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections.Generic;
  2. using System.IO.Ports;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.RT.Log;
  6. using MECF.Framework.Common.Communications;
  7. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Chillers.RisshiChiller
  8. {
  9. public class RisshiChillerMessage : AsciiMessage
  10. {
  11. public string Data { get; set; }
  12. public string ErrorText { get; set; }
  13. }
  14. public class RisshiChillerConnection : SerialPortConnectionBase
  15. {
  16. private static char _endLine = (char)3;
  17. //private bool _isAckMode = false;//should read from config file
  18. private string _cachedBuffer = string.Empty;
  19. public RisshiChillerConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  20. : base(portName, baudRate, dataBits, parity, stopBits, _endLine.ToString(), true)
  21. {
  22. }
  23. public override bool SendMessage(byte[] message)
  24. {
  25. return base.SendMessage(message);
  26. }
  27. protected override MessageBase ParseResponse(string rawText)
  28. {
  29. RisshiChillerMessage msg = new RisshiChillerMessage();
  30. msg.RawMessage = rawText;
  31. if (rawText.Length <= 0)
  32. {
  33. LOG.Error($"empty response,");
  34. msg.IsFormatError = true;
  35. return msg;
  36. }
  37. if (rawText.Length <= 4)
  38. {
  39. LOG.Error($"too short response,");
  40. msg.IsFormatError = true;
  41. return msg;
  42. }
  43. rawText = rawText.Substring(1, rawText.Length - 2);//remove start char and end char
  44. if(!CheckSum(rawText))
  45. {
  46. LOG.Error($"check sum failed,");
  47. msg.IsFormatError = true;
  48. return msg;
  49. }
  50. rawText = rawText.Substring(2, rawText.Length - 4);// remove length and sum
  51. if (rawText.Contains("_ERR"))
  52. {
  53. msg.IsError = true;
  54. var rewTextArray = rawText.Split(' ');
  55. msg.Data = rewTextArray[1];
  56. }
  57. else if (rawText == "_RDY")
  58. {
  59. msg.Data = rawText;
  60. msg.IsResponse = true;
  61. msg.IsAck = true;
  62. msg.IsComplete = true;
  63. }
  64. else
  65. {
  66. msg.Data = rawText;
  67. msg.IsResponse = true;
  68. msg.IsAck = true;
  69. msg.IsComplete = true;
  70. }
  71. return msg;
  72. }
  73. private bool CheckSum(string responseContent)
  74. {
  75. string responseSum = responseContent.Substring(responseContent.Length - 2);
  76. string checkContent = responseContent.Substring(0, responseContent.Length - 2);
  77. var charArray = checkContent.ToArray();
  78. int sum = charArray.Sum(chr => (int)chr);
  79. string sSum = sum.ToString("X");
  80. if (sSum.Length < 2)
  81. sSum = "0" + sSum;
  82. else
  83. sSum = sSum.Substring(sSum.Length - 2);
  84. return responseSum == sSum;
  85. }
  86. }
  87. }