BrooksSMIFConnection.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.SMIFs.Brooks
  8. {
  9. public class BrooksSMIFMessage : AsciiMessage
  10. {
  11. public string Data { get; set; }
  12. public string ErrorText { get; set; }
  13. public bool IsAlarm { get; set; }
  14. }
  15. public class BrooksSMIFConnection : SerialPortConnectionBase
  16. {
  17. //private bool _isAckMode = false;//should read from config file
  18. private string _cachedBuffer = string.Empty;
  19. BrooksSMIF _device;
  20. public BrooksSMIFConnection(BrooksSMIF device, string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  21. : base(portName, baudRate, dataBits, parity, stopBits, "\r\n", true)
  22. {
  23. _device = device;
  24. }
  25. public override bool SendMessage(byte[] message)
  26. {
  27. return base.SendMessage(message);
  28. }
  29. protected override MessageBase ParseResponse(string rawText)
  30. {
  31. BrooksSMIFMessage msg = new BrooksSMIFMessage();
  32. msg.RawMessage = rawText;
  33. rawText = rawText.Replace("\r","").Replace("\n", "");
  34. if (rawText.Length <= 4)
  35. {
  36. LOG.Error($"empty response,");
  37. msg.IsFormatError = true;
  38. return msg;
  39. }
  40. msg.MessagePart = rawText.Split(' ');
  41. if (msg.MessagePart.Length < 2)
  42. {
  43. LOG.Error($"too short response,");
  44. msg.IsFormatError = true;
  45. return msg;
  46. }
  47. if(msg.MessagePart[0] == "HCA")
  48. {
  49. if(msg.MessagePart[1] == "OK")
  50. {
  51. msg.IsAck = true;
  52. }
  53. else
  54. {
  55. msg.IsError = true;
  56. }
  57. msg.Data = msg.MessagePart[1];
  58. }
  59. else if(msg.MessagePart[0] == "AERS")
  60. {
  61. msg.IsEvent = true;
  62. msg.Data = msg.MessagePart[1];
  63. }
  64. else if (msg.MessagePart[0] == "ARS")
  65. {
  66. if(msg.MessagePart[1] != "0000") //0000 means no alarm
  67. {
  68. msg.IsAlarm = true;
  69. OnAlarmArrived(msg);
  70. }
  71. msg.Data = msg.MessagePart[1];
  72. }
  73. else if (msg.MessagePart[0] == "ECD")
  74. {
  75. msg.Data = msg.MessagePart[1];
  76. }
  77. else if (msg.MessagePart[0].Contains("FSD"))
  78. {
  79. msg.Data = rawText.Substring(5);
  80. }
  81. return msg;
  82. }
  83. protected override void OnEventArrived(MessageBase msg)
  84. {
  85. BrooksSMIFMessage message = msg as BrooksSMIFMessage;
  86. _device.OnEventArrived(message.Data);
  87. }
  88. private void OnAlarmArrived(BrooksSMIFMessage msg)
  89. {
  90. _device.OnAlarmArrived(msg.Data);
  91. }
  92. }
  93. }