GM201LVPRobotHandler.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using MECF.Framework.Common.Communications;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.HRC100Robots.GM201LVPRobot
  11. {
  12. public abstract class GM201LVPRobotHandler : HandlerBase
  13. {
  14. public GM201LVPRobot Device { get; }
  15. public string _commandType;
  16. public string _command;
  17. public string _parameter;
  18. public GM201LVPRobotHandler(GM201LVPRobot device, string command, string parameter = null)
  19. : base(BuildMessage(command, parameter))
  20. {
  21. Device = device;
  22. _command = command;
  23. _parameter = parameter;
  24. Name = command;
  25. }
  26. protected static string BuildMessage(string command, string parameter)
  27. {
  28. if (string.IsNullOrEmpty(parameter))
  29. return command + "\r";
  30. return $"{command},{parameter}\r";
  31. }
  32. private static string Checksum(byte[] bytes)
  33. {
  34. int sum = 0;
  35. foreach (byte code in bytes)
  36. {
  37. sum += code;
  38. }
  39. string hex = String.Format("{0:X2}", sum % 256);
  40. return hex;
  41. }
  42. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  43. {
  44. GM201LVPRobotMessage response = msg as GM201LVPRobotMessage;
  45. ResponseMessage = msg;
  46. if (msg.IsError)
  47. {
  48. Device.NoteError(response.RawMessage);
  49. transactionComplete = true;
  50. return true;
  51. }
  52. if (msg.IsAck)
  53. {
  54. SetState(EnumHandlerState.Acked);
  55. }
  56. if (msg.IsComplete && response.RawMessage.Split(',')[0] == "!")
  57. {
  58. SetState(EnumHandlerState.Completed);
  59. //Device.SenACK();
  60. Device.OnActionDone(null);
  61. transactionComplete = true;
  62. return true;
  63. }
  64. transactionComplete = false;
  65. return false;
  66. }
  67. protected virtual void NoteCommandResult(YaskawaRobotMessage response)
  68. {
  69. }
  70. public List<string> replayWithErrorAtCmd = new List<string>() { "INIT", "MTRS", "MTCH", "MABS", "MREL" };
  71. }
  72. public class GM201LVPRobotReadHandler : GM201LVPRobotHandler
  73. {
  74. public GM201LVPRobotReadHandler(GM201LVPRobot robot, string command, string parameter = null, int timeout = 60)
  75. : base(robot, command, parameter)
  76. {
  77. AckTimeout = TimeSpan.FromSeconds(timeout);
  78. CompleteTimeout = TimeSpan.FromSeconds(timeout);
  79. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  80. LOG.Write($"{robot.Name} execute read command {command} {temp} in ASCII.");
  81. }
  82. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  83. {
  84. GM201LVPRobotMessage response = msg as GM201LVPRobotMessage;
  85. if (msg.IsError)
  86. {
  87. Device.NoteError(response.RawMessage);
  88. transactionComplete = true;
  89. return true;
  90. }
  91. if (!response.RawMessage.Contains(_command))
  92. {
  93. transactionComplete = false;
  94. return false;
  95. }
  96. if (Device.ParseReadData(_command, response.Data))
  97. {
  98. Device.OnActionDone(null);
  99. transactionComplete = true;
  100. return true;
  101. }
  102. response.IsError = true;
  103. return base.HandleMessage(response, out transactionComplete);
  104. }
  105. }
  106. public class GM201LVPRobotSetHandler : GM201LVPRobotHandler
  107. {
  108. public GM201LVPRobotSetHandler(GM201LVPRobot robot, string command, string parameter = null, int timeout = 60)
  109. : base(robot, command, parameter)
  110. {
  111. AckTimeout = TimeSpan.FromSeconds(timeout);
  112. CompleteTimeout = TimeSpan.FromSeconds(timeout);
  113. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  114. LOG.Write($"{robot.Name} execute set command {command} {temp} in ASCII.");
  115. }
  116. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  117. {
  118. GM201LVPRobotMessage response = msg as GM201LVPRobotMessage;
  119. if (msg.IsError)
  120. {
  121. Device.NoteError(response.RawMessage);
  122. transactionComplete = true;
  123. return true;
  124. }
  125. if (!response.RawMessage.Contains(_command))
  126. {
  127. transactionComplete = false;
  128. return false;
  129. }
  130. if (response.IsAck)
  131. {
  132. Device.OnActionDone(null);
  133. transactionComplete = true;
  134. return true;
  135. }
  136. return base.HandleMessage(response, out transactionComplete);
  137. }
  138. }
  139. public class GM201LVPRobotMotionHandler : GM201LVPRobotHandler
  140. {
  141. public GM201LVPRobotMotionHandler(GM201LVPRobot robot, string command, string parameter = null, int timeout = 60)
  142. : base(robot, command, parameter)
  143. {
  144. AckTimeout = TimeSpan.FromSeconds(timeout);
  145. CompleteTimeout = TimeSpan.FromSeconds(timeout);
  146. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  147. LOG.Write($"{robot.Name} execute motion command {command} {temp} in ASCII.");
  148. }
  149. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  150. {
  151. GM201LVPRobotMessage response = msg as GM201LVPRobotMessage;
  152. ResponseMessage = msg;
  153. LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
  154. if (msg.IsError)
  155. {
  156. Device.NoteError(response.RawMessage);
  157. transactionComplete = true;
  158. return true;
  159. }
  160. if (msg.IsAck)
  161. {
  162. SetState(EnumHandlerState.Acked);
  163. }
  164. if (msg.IsComplete)
  165. {
  166. //Device.SenACK();
  167. }
  168. if (IsAcked && msg.IsComplete)
  169. {
  170. SetState(EnumHandlerState.Completed);
  171. Device.OnActionDone(null);
  172. transactionComplete = true;
  173. return true;
  174. }
  175. transactionComplete = false;
  176. return false;
  177. }
  178. }
  179. }