RobotSiasun1500C800CConnection.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.Robots.Siasun1500C800C
  7. {
  8. public class RobotSiasun1500C800CMessage : AsciiMessage
  9. {
  10. public string Data { get; set; }
  11. public string ErrorText { get; set; }
  12. }
  13. public class RobotSiasun1500C800CConnection : SerialPortConnectionBase
  14. {
  15. private bool _isAckMode = false;//should read from config file
  16. private string _cachedBuffer = string.Empty;
  17. public RobotSiasun1500C800CConnection(string portName, int baudRate = 19200, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  18. : base(portName, baudRate, dataBits, parity, stopBits, "\r", true)
  19. {
  20. }
  21. public override bool SendMessage(byte[] message)
  22. {
  23. return base.SendMessage(message);
  24. }
  25. protected override MessageBase ParseResponse(string rawText)
  26. {
  27. RobotSiasun1500C800CMessage msg = new RobotSiasun1500C800CMessage();
  28. msg.RawMessage = rawText;
  29. if (rawText.Length <= 0)
  30. {
  31. LOG.Error($"empty response,");
  32. msg.IsFormatError = true;
  33. return msg;
  34. }
  35. rawText = rawText.Substring(0, rawText.Length - 1);
  36. if (rawText.Contains("_ERR"))
  37. {
  38. msg.IsError = true;
  39. var rewTextArray = rawText.Split(' ');
  40. msg.Data = rewTextArray[1];
  41. }
  42. else if (rawText == "_RDY")
  43. {
  44. msg.Data = rawText;
  45. if (_isAckMode)
  46. {
  47. msg.IsResponse = true;
  48. msg.IsAck = true;
  49. msg.IsComplete = false;
  50. }
  51. else
  52. {
  53. msg.IsResponse = true;
  54. msg.IsAck = true;
  55. msg.IsComplete = true;
  56. }
  57. }
  58. else if(rawText == "BG_RDY")
  59. {
  60. if (!_isAckMode)
  61. {
  62. LOG.Error($"response format check failed, BG_RDY is not valid in Mode 1");
  63. msg.IsFormatError = true;
  64. return msg;
  65. }
  66. msg.Data = rawText;
  67. msg.IsResponse = true;
  68. msg.IsAck = true;
  69. msg.IsComplete = true;
  70. }
  71. else
  72. {
  73. msg.Data = rawText;
  74. msg.IsResponse = true;
  75. msg.IsAck = true;
  76. msg.IsComplete = true;
  77. }
  78. return msg;
  79. }
  80. }
  81. public class RobotSiasun1500C800CTCPConnection : TCPPortConnectionBase
  82. {
  83. private bool _isAckMode = false;//should read from config file
  84. public RobotSiasun1500C800CTCPConnection(string address)
  85. : base(address, "\r", true)
  86. {
  87. }
  88. public override bool SendMessage(string message)
  89. {
  90. return base.SendMessage(message);
  91. }
  92. protected override MessageBase ParseResponse(string rawText)
  93. {
  94. RobotSiasun1500C800CMessage msg = new RobotSiasun1500C800CMessage();
  95. msg.RawMessage = rawText;
  96. if (rawText.Length <= 0)
  97. {
  98. LOG.Error($"empty response,");
  99. msg.IsFormatError = true;
  100. return msg;
  101. }
  102. rawText = rawText.Substring(0, rawText.Length - 1);
  103. if (rawText.Contains("_ERR"))
  104. {
  105. msg.IsError = true;
  106. var rewTextArray = rawText.Split(' ');
  107. msg.Data = rewTextArray[1];
  108. msg.IsComplete = true;
  109. }
  110. else if (rawText == "_RDY")
  111. {
  112. msg.Data = rawText;
  113. if (_isAckMode)
  114. {
  115. msg.IsResponse = true;
  116. msg.IsAck = true;
  117. msg.IsComplete = false;
  118. }
  119. else
  120. {
  121. msg.IsResponse = true;
  122. msg.IsAck = true;
  123. msg.IsComplete = true;
  124. }
  125. }
  126. else if (rawText == "BG_RDY")
  127. {
  128. if (!_isAckMode)
  129. {
  130. LOG.Error($"response format check failed, BG_RDY is not valid in Mode 1");
  131. msg.IsFormatError = true;
  132. return msg;
  133. }
  134. msg.Data = rawText;
  135. msg.IsResponse = true;
  136. msg.IsAck = true;
  137. msg.IsComplete = true;
  138. }
  139. else
  140. {
  141. msg.Data = rawText;
  142. msg.IsResponse = true;
  143. }
  144. return msg;
  145. }
  146. }
  147. }