YaskawaRobotHandler.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.YaskawaRobots
  11. {
  12. public class YaskawaProtocolTag
  13. {
  14. public const string tag_end = "\r";
  15. public const string tag_cmd_start = "$";
  16. public const string cmd_token = ",";
  17. public const string resp_tag_normal = "$";
  18. public const string resp_tag_error = "?";
  19. public const string resp_tag_excute = "!";
  20. public const string resp_tag_event = ">";
  21. public const string resp_evt_error = "100";
  22. }
  23. public abstract class YaskawaSR100RobotHandler : HandlerBase
  24. {
  25. public YaskawaSR100Robot Device { get; }
  26. public string _commandType;
  27. public string _command;
  28. public string _parameter;
  29. private int _seqNO;
  30. //protected YaskawaTokenGenerator _generator;
  31. public YaskawaSR100RobotHandler(YaskawaSR100Robot device,string command,string parameter =null)
  32. :base(BuildMessage(command,parameter))
  33. {
  34. Device = device;
  35. _command = command;
  36. _parameter = parameter;
  37. Name = command;
  38. }
  39. protected static string BuildMessage(string command, string parameter)
  40. {
  41. if (string.IsNullOrEmpty(parameter))
  42. return command;
  43. return $"{command},{parameter}";
  44. }
  45. private static string Checksum(byte[] bytes)
  46. {
  47. int sum = 0;
  48. foreach (byte code in bytes)
  49. {
  50. sum += code;
  51. }
  52. string hex = String.Format("{0:X2}", sum % 256);
  53. return hex;
  54. }
  55. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  56. {
  57. YaskawaRobotMessage response = msg as YaskawaRobotMessage;
  58. ResponseMessage = msg;
  59. if (msg.IsError)
  60. {
  61. Device.NoteError(response.RawMessage);
  62. transactionComplete = true;
  63. return true;
  64. }
  65. if(msg.IsAck)
  66. {
  67. SetState(EnumHandlerState.Acked);
  68. }
  69. if (msg.IsComplete && response.RawMessage.Split(',')[0]=="!")
  70. {
  71. SetState(EnumHandlerState.Completed);
  72. //Device.SenACK();
  73. Device.OnActionDone(null);
  74. transactionComplete = true;
  75. return true;
  76. }
  77. transactionComplete = false;
  78. return false;
  79. }
  80. protected virtual void NoteCommandResult(YaskawaRobotMessage response)
  81. {
  82. }
  83. }
  84. public class SR100RobotReadHandler: YaskawaSR100RobotHandler
  85. {
  86. public SR100RobotReadHandler(YaskawaSR100Robot robot, string command,string parameter=null)
  87. :base(robot,command,parameter)
  88. {
  89. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  90. LOG.Write($"{robot.Name} execute read command {command} {temp} in ASCII.");
  91. }
  92. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  93. {
  94. YaskawaRobotMessage response = msg as YaskawaRobotMessage;
  95. if(!response.RawMessage.Contains(_command))
  96. {
  97. transactionComplete = false;
  98. return false;
  99. }
  100. if(Device.ParseReadData(_command,response.Data))
  101. {
  102. Device.OnActionDone(null);
  103. transactionComplete = true;
  104. return true;
  105. }
  106. response.IsError = true;
  107. return base.HandleMessage(response, out transactionComplete);
  108. }
  109. }
  110. public class SR100RobotSetHandler:YaskawaSR100RobotHandler
  111. {
  112. public SR100RobotSetHandler(YaskawaSR100Robot robot, string command, string parameter = null)
  113. : base(robot, command, parameter)
  114. {
  115. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  116. LOG.Write($"{robot.Name} execute set command {command} {temp} in ASCII.");
  117. }
  118. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  119. {
  120. YaskawaRobotMessage response = msg as YaskawaRobotMessage;
  121. if (!response.RawMessage.Contains(_command))
  122. {
  123. transactionComplete = false;
  124. return false;
  125. }
  126. if (response.IsAck)
  127. {
  128. Device.OnActionDone(null);
  129. transactionComplete = true;
  130. return true;
  131. }
  132. return base.HandleMessage(response, out transactionComplete);
  133. }
  134. }
  135. public class SR100RobotMotionHandler: YaskawaSR100RobotHandler
  136. {
  137. public SR100RobotMotionHandler(YaskawaSR100Robot robot, string command, string parameter = null)
  138. : base(robot, command, parameter)
  139. {
  140. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  141. LOG.Write($"{robot.Name} execute motion command {command} {temp} in ASCII.");
  142. }
  143. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  144. {
  145. YaskawaRobotMessage response = msg as YaskawaRobotMessage;
  146. ResponseMessage = msg;
  147. LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
  148. if (msg.IsError)
  149. {
  150. Device.NoteError(response.RawMessage);
  151. transactionComplete = true;
  152. return true;
  153. }
  154. if (msg.IsAck)
  155. {
  156. SetState(EnumHandlerState.Acked);
  157. }
  158. if(msg.IsComplete)
  159. {
  160. //Device.SenACK();
  161. }
  162. if (IsAcked && msg.IsComplete)
  163. {
  164. SetState(EnumHandlerState.Completed);
  165. Device.HandlerMotion(_command, response.Data);
  166. Device.OnActionDone(null);
  167. transactionComplete = true;
  168. return true;
  169. }
  170. transactionComplete = false;
  171. return false;
  172. }
  173. }
  174. public class SR100RobotGripAndCheckWaferMotionHandler : YaskawaSR100RobotHandler
  175. {
  176. public SR100RobotGripAndCheckWaferMotionHandler(YaskawaSR100Robot robot)
  177. : base(robot, "CSOL", "F,1,0")
  178. {
  179. LOG.Write($"{robot.Name} execute Grip and Check wafer command CSOL in ASCII.");
  180. }
  181. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  182. {
  183. YaskawaRobotMessage response = msg as YaskawaRobotMessage;
  184. ResponseMessage = msg;
  185. LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
  186. if (msg.IsError)
  187. {
  188. Device.NoteError(response.RawMessage);
  189. transactionComplete = true;
  190. return true;
  191. }
  192. if (msg.IsAck)
  193. {
  194. SetState(EnumHandlerState.Acked);
  195. }
  196. if (msg.IsComplete)
  197. {
  198. //Device.SenACK();
  199. }
  200. if (IsAcked && msg.IsComplete)
  201. {
  202. SetState(EnumHandlerState.Completed);
  203. Thread.Sleep(3000);
  204. Device.OnActionDone(null);
  205. transactionComplete = true;
  206. return true;
  207. }
  208. transactionComplete = false;
  209. return false;
  210. }
  211. }
  212. public class SR100RobotCheckWaferHandler : YaskawaSR100RobotHandler
  213. {
  214. public SR100RobotCheckWaferHandler(YaskawaSR100Robot robot)
  215. : base(robot, "RSTS", null)
  216. {
  217. LOG.Write($"{robot.Name} execute Check wafer command RSTS in ASCII.");
  218. }
  219. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  220. {
  221. YaskawaRobotMessage response = msg as YaskawaRobotMessage;
  222. ResponseMessage = msg;
  223. LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
  224. if (msg.IsError)
  225. {
  226. Device.NoteError(response.RawMessage);
  227. transactionComplete = true;
  228. return true;
  229. }
  230. if (msg.IsAck)
  231. {
  232. SetState(EnumHandlerState.Acked);
  233. SetState(EnumHandlerState.Completed);
  234. Device.ParseReadData(_command, response.Data);
  235. Device.CheckWaferPresentAndGrip();
  236. Device.OnActionDone(null);
  237. transactionComplete = true;
  238. return true;
  239. }
  240. transactionComplete = false;
  241. return false;
  242. }
  243. }
  244. }