SiasunVCEConnection.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.SiasunVCE
  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.Remove(rawText.Length - 1).Substring(3);
  51. if (rawText.Contains("_ERR"))
  52. {
  53. msg.IsError = true;
  54. msg.IsAck = true;
  55. var rewTextArray = rawText.Split(' ');
  56. msg.Data = rewTextArray[1];
  57. }
  58. else if (rawText.Contains("_BKGERR"))
  59. {
  60. msg.IsError = true;
  61. msg.IsComplete = true;
  62. var rewTextArray = rawText.Split(' ');
  63. msg.Data = rewTextArray[1];
  64. }
  65. else if (rawText == "_RDY")
  66. {
  67. msg.IsRDY = true;
  68. }
  69. else if (rawText == "_BKGRDY")
  70. {
  71. msg.Data = rawText;
  72. msg.IsComplete = true;
  73. }
  74. //Request response axisPosition
  75. //R,ARM,R,RE
  76. //X,ARM,R,RE,0000000
  77. else
  78. {
  79. msg.MessagePart = rawText.Split(',');
  80. if (msg.MessagePart.Length < 2)
  81. {
  82. LOG.Error($"too short response,");
  83. msg.IsFormatError = true;
  84. return msg;
  85. }
  86. if (msg.MessagePart[0] != "X")
  87. {
  88. LOG.Error($"invalid record type for response ,");
  89. msg.IsFormatError = true;
  90. return msg;
  91. }
  92. if (msg.MessagePart.Length > 2)
  93. {
  94. msg.Data = string.Join(",", msg.MessagePart, 2, msg.MessagePart.Length - 2);
  95. }
  96. msg.IsComplete = true;
  97. }
  98. return msg;
  99. }
  100. }
  101. }