EdwardsPumpConnection.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.Pumps.EdwardsPump
  8. {
  9. public class EdwardsPumpMessage : AsciiMessage
  10. {
  11. public string Data { get; set; }
  12. public string ErrorText { get; set; }
  13. }
  14. public class EdwardsPumpConnection : SerialPortConnectionBase
  15. {
  16. private static string _endLine = "\r\n";
  17. public EdwardsPumpConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  18. : base(portName, baudRate, dataBits, parity, stopBits, _endLine, true)
  19. {
  20. }
  21. public override bool SendMessage(string message)
  22. {
  23. return base.SendMessage(message);
  24. }
  25. protected override MessageBase ParseResponse(string rawText)
  26. {
  27. EdwardsPumpMessage msg = new EdwardsPumpMessage();
  28. msg.RawMessage = rawText;
  29. if (rawText.Length <= 0)
  30. {
  31. LOG.Error($"empty response");
  32. msg.IsFormatError = true;
  33. return msg;
  34. }
  35. if (rawText.Length <= 4)
  36. {
  37. LOG.Error($"too short response");
  38. msg.IsFormatError = true;
  39. return msg;
  40. }
  41. msg.Data = rawText.Replace("\r", "").Replace("\n", "");
  42. if (msg.Data.ToUpper().StartsWith("ERR"))
  43. {
  44. var errorCodeString = msg.Data.ToUpper().Replace("ERR", "").Replace(" ", "");
  45. int.TryParse(errorCodeString, out int errorCode);
  46. if (errorCode > 0)
  47. {
  48. msg.ErrorText = errorCodeString;
  49. msg.IsError = true;
  50. }
  51. }
  52. msg.IsResponse = true;
  53. msg.IsAck = true;
  54. return msg;
  55. }
  56. }
  57. }