YaskawaNXCRobotHandler.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 YaskawaNXC100RobotHandler : HandlerBase
  24. {
  25. public YaskawaNXC100Robot Device { get; }
  26. public string _commandType;
  27. public string _command;
  28. public string _parameter;
  29. private int _seqNO;
  30. //protected YaskawaTokenGenerator _generator;
  31. public YaskawaNXC100RobotHandler(YaskawaNXC100Robot 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. YaskawaNXC100Message response = msg as YaskawaNXC100Message;
  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)
  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(YaskawaNXC100Message response)
  81. {
  82. }
  83. }
  84. public class NXC100RobotReadHandler: YaskawaNXC100RobotHandler
  85. {
  86. public NXC100RobotReadHandler(YaskawaNXC100Robot 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. YaskawaNXC100Message response = msg as YaskawaNXC100Message;
  95. if(response.IsAck && Device.ParseReadData(_command,response.Data))
  96. {
  97. Device.OnActionDone(null);
  98. transactionComplete = true;
  99. return true;
  100. }
  101. response.IsError = true;
  102. return base.HandleMessage(response, out transactionComplete);
  103. }
  104. }
  105. public class NXC100RobotSetHandler:YaskawaNXC100RobotHandler
  106. {
  107. public NXC100RobotSetHandler(YaskawaNXC100Robot robot, string command, string parameter = null)
  108. : base(robot, command, parameter)
  109. {
  110. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  111. LOG.Write($"{robot.Name} execute set command {command} {temp} in ASCII.");
  112. }
  113. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  114. {
  115. YaskawaNXC100Message response = msg as YaskawaNXC100Message;
  116. if (response.IsAck)
  117. {
  118. Device.OnActionDone(null);
  119. transactionComplete = true;
  120. return true;
  121. }
  122. return base.HandleMessage(response, out transactionComplete);
  123. }
  124. }
  125. public class NXC100RobotMotionHandler: YaskawaNXC100RobotHandler
  126. {
  127. public NXC100RobotMotionHandler(YaskawaNXC100Robot robot, string command, string parameter = null)
  128. : base(robot, command, parameter)
  129. {
  130. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  131. LOG.Write($"{robot.Name} execute motion command {command} {temp} in ASCII.");
  132. }
  133. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  134. {
  135. YaskawaNXC100Message response = msg as YaskawaNXC100Message;
  136. ResponseMessage = msg;
  137. LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
  138. if (msg.IsError)
  139. {
  140. Device.NoteError(response.RawMessage);
  141. transactionComplete = true;
  142. return true;
  143. }
  144. if (msg.IsAck)
  145. {
  146. SetState(EnumHandlerState.Acked);
  147. }
  148. if(msg.IsComplete)
  149. {
  150. //Device.SenACK();
  151. }
  152. if (IsAcked && msg.IsComplete)
  153. {
  154. SetState(EnumHandlerState.Completed);
  155. Device.OnActionDone(null);
  156. transactionComplete = true;
  157. return true;
  158. }
  159. transactionComplete = false;
  160. return false;
  161. }
  162. }
  163. public class NXC100RobotGripAndCheckWaferMotionHandler : YaskawaNXC100RobotHandler
  164. {
  165. public NXC100RobotGripAndCheckWaferMotionHandler(YaskawaNXC100Robot robot)
  166. : base(robot, "CSOL", "F,1,0")
  167. {
  168. Thread.Sleep(500);
  169. LOG.Write($"{robot.Name} execute Grip and Check wafer command CSOL in ASCII.");
  170. }
  171. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  172. {
  173. YaskawaNXC100Message response = msg as YaskawaNXC100Message;
  174. ResponseMessage = msg;
  175. LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
  176. if (msg.IsError)
  177. {
  178. Device.NoteError(response.RawMessage);
  179. transactionComplete = true;
  180. return true;
  181. }
  182. if (msg.IsAck)
  183. {
  184. SetState(EnumHandlerState.Acked);
  185. }
  186. if (msg.IsComplete)
  187. {
  188. //Device.SenACK();
  189. }
  190. if (IsAcked && msg.IsComplete)
  191. {
  192. SetState(EnumHandlerState.Completed);
  193. Device.CheckWaferPresentAndGrip();
  194. Device.OnActionDone(null);
  195. transactionComplete = true;
  196. return true;
  197. }
  198. transactionComplete = false;
  199. return false;
  200. }
  201. }
  202. }