TazmoRobotHandler.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.SCCore;
  4. using MECF.Framework.Common.Communications;
  5. using Newtonsoft.Json.Linq;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.TazmoRobot
  11. {
  12. public abstract class TazmoRobotHandler : HandlerBase
  13. {
  14. public TazmoRobot Device { get; }
  15. public string Command;
  16. public string Parameter;
  17. protected TazmoRobotHandler(TazmoRobot device, string command, string para) : base(BuildMessage(command, para))
  18. {
  19. Device = device;
  20. Command = command;
  21. Name = command;
  22. }
  23. private static byte[] BuildMessage(string command, string para)
  24. {
  25. List<byte> ret = new List<byte>();
  26. foreach (char c in command)
  27. {
  28. ret.Add((byte)c);
  29. }
  30. if (!string.IsNullOrEmpty(para))
  31. {
  32. ret.Add((byte)0x2C);
  33. foreach (char b in para) ret.Add((byte)b);
  34. }
  35. ret.Add(0x0D);
  36. return ret.ToArray();
  37. }
  38. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  39. {
  40. TazmoRobotMessageBin response = msg as TazmoRobotMessageBin;
  41. ResponseMessage = msg;
  42. transactionComplete = false;
  43. if (response.IsAck)
  44. {
  45. SetState(EnumHandlerState.Acked);
  46. transactionComplete = true;
  47. }
  48. if (response.IsBusy)
  49. {
  50. SetState(EnumHandlerState.Completed);
  51. transactionComplete = true;
  52. }
  53. if (response.IsComplete)
  54. {
  55. SetState(EnumHandlerState.Completed);
  56. transactionComplete = true;
  57. }
  58. if (response.IsError)
  59. {
  60. SetState(EnumHandlerState.Completed);
  61. transactionComplete = true;
  62. }
  63. if (response.IsNak)
  64. {
  65. SetState(EnumHandlerState.Completed);
  66. transactionComplete = true;
  67. }
  68. if (response.IsEvent)
  69. {
  70. SendAck();
  71. if (response.CMD == Encoding.ASCII.GetBytes(Command))
  72. {
  73. }
  74. SetState(EnumHandlerState.Completed);
  75. transactionComplete = true;
  76. }
  77. if (response.IsResponse)
  78. {
  79. SendAck();
  80. SetState(EnumHandlerState.Completed);
  81. transactionComplete = true;
  82. }
  83. return true;
  84. }
  85. public void SendAck()
  86. {
  87. var isSimulatorMode = SC.GetValue<bool>("System.IsSimulatorMode");
  88. if (isSimulatorMode)
  89. Device.Connection.SendMessage(new byte[] { 0x06, 0x0D });
  90. else Device.Connection.SendMessage(new byte[] { 0x06 });
  91. }
  92. }
  93. public class TazmoRobotSingleTransactionHandler : TazmoRobotHandler
  94. {
  95. public static string _command = null;
  96. public TazmoRobotSingleTransactionHandler(TazmoRobot device, string command, string para) : base(device, BuildData(command), para)
  97. {
  98. }
  99. private static string BuildData(string command)
  100. {
  101. _command = command;
  102. return command;
  103. }
  104. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  105. {
  106. TazmoRobotMessageBin response = msg as TazmoRobotMessageBin;
  107. ResponseMessage = msg;
  108. Device.TaExecuteSuccss = true;
  109. transactionComplete = false;
  110. if (response.IsAck)
  111. {
  112. SetState(EnumHandlerState.Completed);
  113. transactionComplete = true;
  114. if (_command == "PAU")
  115. {
  116. Device.IsPause = true;
  117. EV.PostWarningLog("Robot", "Robot is in Pause State.");
  118. }
  119. if (_command == "CNT")
  120. {
  121. Device.IsPause = false;
  122. EV.PostInfoLog("Robot", "Pause State of Robot is cancled.");
  123. }
  124. }
  125. if (response.IsBusy)
  126. {
  127. SetState(EnumHandlerState.Completed);
  128. transactionComplete = true;
  129. }
  130. if (response.IsNak)
  131. {
  132. SetState(EnumHandlerState.Completed);
  133. Device.TaExecuteSuccss = false;
  134. transactionComplete = true;
  135. //ParseError(response.Data);
  136. }
  137. if (response.IsError)
  138. {
  139. SetState(EnumHandlerState.Completed);
  140. Device.TaExecuteSuccss = false;
  141. transactionComplete = true;
  142. Device.ParseStatus(Encoding.ASCII.GetString(response.Data));
  143. }
  144. if (response.IsResponse)
  145. {
  146. SetState(EnumHandlerState.Completed);
  147. Device.TaExecuteSuccss = true;
  148. transactionComplete = true;
  149. if (Encoding.Default.GetString(response.CMD).Contains("STS"))
  150. Device.ParseStatus(Encoding.ASCII.GetString(response.Data));
  151. if (Encoding.Default.GetString(response.CMD).Contains("STA"))
  152. Device.ParseStatusAndPostion("STA",Encoding.ASCII.GetString(response.Data));
  153. if (Encoding.Default.GetString(response.CMD).Contains("RMP"))
  154. {
  155. Device.ParseReadData("RMP", Encoding.ASCII.GetString(response.Data).Split(','));
  156. }
  157. if (Encoding.Default.GetString(response.CMD).Contains("RED"))
  158. {
  159. Device.ParseAxisPosition(Encoding.ASCII.GetString(response.Data));
  160. }
  161. }
  162. return true;
  163. }
  164. }
  165. public class TazmoRobotTwinTransactionHandler : TazmoRobotHandler
  166. {
  167. public TazmoRobotTwinTransactionHandler(TazmoRobot device, string command, string para) : base(device, BuildData(command), para)
  168. {
  169. }
  170. private static string BuildData(string command)
  171. {
  172. return command;
  173. }
  174. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  175. {
  176. TazmoRobotMessageBin response = msg as TazmoRobotMessageBin;
  177. ResponseMessage = msg;
  178. Device.TaExecuteSuccss = true;
  179. transactionComplete = false;
  180. if (response.IsAck)
  181. {
  182. //string command = Encoding.Default.GetString(response.CMD);
  183. //if (command=="GET")
  184. SetState(EnumHandlerState.Acked);
  185. transactionComplete = false;
  186. }
  187. if (response.IsBusy)
  188. {
  189. SetState(EnumHandlerState.Completed);
  190. Device.OnError("RobotBusy");
  191. transactionComplete = true;
  192. }
  193. if (response.IsNak)
  194. {
  195. SetState(EnumHandlerState.Completed);
  196. Device.TaExecuteSuccss = false;
  197. transactionComplete = true;
  198. Device.OnError("CommandNak");
  199. //ParseError(response.Data);
  200. }
  201. if (response.IsError)
  202. {
  203. SetState(EnumHandlerState.Completed);
  204. Device.TaExecuteSuccss = false;
  205. transactionComplete = true;
  206. Device.ParseStatus(Encoding.ASCII.GetString(response.Data));
  207. }
  208. if (response.IsResponse)
  209. {
  210. string command = Encoding.Default.GetString(response.CMD);
  211. //if (command == "RST" || command == "HOM")
  212. //Device.Initalized = true;
  213. SetState(EnumHandlerState.Completed);
  214. Device.TaExecuteSuccss = true;
  215. transactionComplete = true;
  216. SendAck();
  217. if (Encoding.Default.GetString(response.CMD) == "WCH") Device.ParseStatusAndPostion("WCH",Encoding.ASCII.GetString(response.Data));
  218. if (Encoding.Default.GetString(response.CMD) == "VCH") Device.ParseStatusAndPostion("VCH",Encoding.ASCII.GetString(response.Data));
  219. }
  220. return true;
  221. }
  222. }
  223. }