PfeifferPumpA603Connection.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.PfeifferPumpA603
  8. {
  9. public class PfeifferPumpA603Message : AsciiMessage
  10. {
  11. public string Data { get; set; }
  12. public string Address { get; set; }
  13. }
  14. public class PfeifferPumpA603Connection : SerialPortConnectionBase
  15. {
  16. private static string _startLine = "#";
  17. private static string _endLine = "\r";
  18. public PfeifferPumpA603Connection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  19. : base(portName, baudRate, dataBits, parity, stopBits, _endLine, true)
  20. {
  21. }
  22. public override bool SendMessage(string message)
  23. {
  24. return base.SendMessage(message);
  25. }
  26. protected override MessageBase ParseResponse(string rawText)
  27. {
  28. PfeifferPumpA603Message msg = new PfeifferPumpA603Message();
  29. msg.RawMessage = rawText;
  30. if (rawText.Length <= 0)
  31. {
  32. LOG.Error($"empty response");
  33. msg.IsFormatError = true;
  34. return msg;
  35. }
  36. if (rawText.Length <= 4)
  37. {
  38. LOG.Error($"too short response");
  39. msg.IsFormatError = true;
  40. return msg;
  41. }
  42. if (rawText[0].ToString() != _startLine)
  43. {
  44. LOG.Error($"invalid format response");
  45. msg.IsFormatError = true;
  46. return msg;
  47. }
  48. rawText = rawText.Replace("\r", "").Replace("\n", "").Replace(_startLine, "");//remove start char and end char
  49. msg.Address = rawText.Substring(0, 3);
  50. msg.Data = rawText.Substring(4);
  51. msg.IsResponse = true;
  52. msg.IsAck = true;
  53. return msg;
  54. }
  55. }
  56. }