PfeifferPumpA100Connection.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.PfeifferPumpA100
  8. {
  9. public class PfeifferPumpA100Message : BinaryMessage
  10. {
  11. //public string Data { get; set; }
  12. public string Address { get; set; }
  13. public byte[] Data { get; set; }
  14. }
  15. public class PfeifferPumpA100Connection : SerialPortConnectionBase
  16. {
  17. //private static string _startLine = "#";
  18. private static string _endLine = "\r";
  19. private List<byte> _msgBuffer = new List<byte>();
  20. private byte _end = 0x0d;
  21. public PfeifferPumpA100Connection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  22. : base(portName, baudRate, dataBits, parity, stopBits, _endLine, false)
  23. {
  24. }
  25. public override bool SendMessage(string message)
  26. {
  27. return base.SendMessage(message);
  28. }
  29. protected override MessageBase ParseResponse(byte[] rawMessage)
  30. {
  31. PfeifferPumpA100Message msg = new PfeifferPumpA100Message();
  32. if (rawMessage == null || rawMessage.Length == 0)
  33. {
  34. return msg;
  35. }
  36. _msgBuffer.AddRange(rawMessage);
  37. var index = _msgBuffer.IndexOf(_end);
  38. if (index > -1)
  39. {
  40. msg.RawMessage = _msgBuffer.Take(index).ToArray();
  41. msg.Data = _msgBuffer.Skip(4).Take(index - 4).ToArray();
  42. _msgBuffer.RemoveRange(0, index);
  43. _msgBuffer.Remove(0x0a);
  44. _msgBuffer.Remove(_end);
  45. msg.IsResponse = true;
  46. msg.IsAck = true;
  47. return msg;
  48. }
  49. return msg;
  50. }
  51. //protected override MessageBase ParseResponse(string rawText)
  52. //{
  53. // PfeifferPumpA100Message msg = new PfeifferPumpA100Message();
  54. // msg.RawMessage = rawText;
  55. // if (rawText.Length <= 0)
  56. // {
  57. // LOG.Error($"empty response");
  58. // msg.IsFormatError = true;
  59. // return msg;
  60. // }
  61. // if (rawText.Length <= 4)
  62. // {
  63. // LOG.Error($"too short response");
  64. // msg.IsFormatError = true;
  65. // return msg;
  66. // }
  67. // if (rawText[0].ToString() != _startLine)
  68. // {
  69. // LOG.Error($"invalid format response");
  70. // msg.IsFormatError = true;
  71. // return msg;
  72. // }
  73. // rawText = rawText.Replace("\r", "").Replace("\n", "").Replace(_startLine, "");//remove start char and end char
  74. // msg.Address = rawText.Substring(0, 3);
  75. // msg.Data = rawText.Substring(4);
  76. // msg.IsResponse = true;
  77. // msg.IsAck = true;
  78. // return msg;
  79. //}
  80. }
  81. }