SiasunVCEConnection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections.Generic;
  2. using System.IO.Ports;
  3. using System.Text;
  4. using Aitex.Core.RT.Log;
  5. using MECF.Framework.Common.Communications;
  6. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.VCE.SiasunVCEII
  7. {
  8. public class SiasunVCEMessage : AsciiMessage
  9. {
  10. public string Data { get; set; }
  11. public string ErrorText { get; set; }
  12. public bool IsRDY { get; set; }
  13. }
  14. public class SiasunVCEConnection : SerialPortConnectionBase
  15. {
  16. //private bool _isBKGPlusMode = true;//should read from config file
  17. private string _cachedBuffer = string.Empty;
  18. public SiasunVCEConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  19. : base(portName, baudRate, dataBits, parity, stopBits, "\r", true)
  20. {
  21. }
  22. public override bool SendMessage(byte[] message)
  23. {
  24. return base.SendMessage(message);
  25. }
  26. //1) Format: id,X,category,data<LF><CR>
  27. //2) request response
  28. //00,R,ARM,R,EX
  29. //00,X,ARM,R,EX,0169500
  30. //3) action response
  31. //S,BUFFER,NS,2
  32. //_RDY
  33. //_BKGRDY
  34. //A,DO
  35. //_RDY
  36. //_BKGERR A6
  37. //BAD,COMMAND
  38. //_ERR S4
  39. //_RDY
  40. protected override MessageBase ParseResponse(string rawText)
  41. {
  42. SiasunVCEMessage msg = new SiasunVCEMessage();
  43. msg.RawMessage = rawText;
  44. if (rawText.Length <= 0)
  45. {
  46. LOG.Error($"empty response,");
  47. msg.IsFormatError = true;
  48. return msg;
  49. }
  50. rawText = rawText.Replace("\r", "").Replace("\n", "");
  51. msg.MessagePart = rawText.Split(',');
  52. if (msg.MessagePart.Length < 2)
  53. {
  54. LOG.Error($"too short response,");
  55. msg.IsFormatError = true;
  56. return msg;
  57. }
  58. if (msg.MessagePart[0] == "M")
  59. {
  60. if (msg.MessagePart[1] == "OK")
  61. {
  62. msg.Data = rawText;
  63. msg.IsComplete = true;
  64. }
  65. else
  66. {
  67. msg.Data = msg.MessagePart[1];
  68. msg.IsError = true;
  69. }
  70. }
  71. else if (msg.MessagePart[0] == "X")
  72. {
  73. if (msg.MessagePart.Length > 2)
  74. msg.Data = string.Join(",", msg.MessagePart, 1, msg.MessagePart.Length - 1);
  75. else
  76. msg.Data = msg.MessagePart[1];
  77. msg.IsComplete = true;
  78. }
  79. else if (msg.MessagePart[0] == "H")
  80. {
  81. if (msg.MessagePart.Length != 3)
  82. {
  83. msg.IsError = true;
  84. }
  85. else
  86. {
  87. if (msg.MessagePart[2] == "OK")
  88. {
  89. msg.Data = rawText;
  90. msg.IsComplete = true;
  91. }
  92. else
  93. {
  94. msg.IsError = true;
  95. }
  96. }
  97. }
  98. return msg;
  99. }
  100. }
  101. }