TDKB200LoadPortConnection.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Aitex.Core.RT.Log;
  2. using MECF.Framework.Common.Communications;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO.Ports;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDKB
  12. {
  13. public class TDKB200LoadPortMessage : AsciiMessage
  14. {
  15. public string CommandType { get; set; }
  16. public string Command { get; set; }
  17. public string Data { get; set; }
  18. }
  19. public class TDKB200LoadPortConnection : SerialPortConnectionBase
  20. {
  21. private TDKB200LoadPort _device;
  22. public TDKB200LoadPortConnection(TDKB200LoadPort device, string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  23. : base(portName, baudRate, dataBits, parity, stopBits, "\r", true)
  24. {
  25. _device = device;
  26. }
  27. protected override MessageBase ParseResponse(string rawMsg)
  28. {
  29. TDKB200LoadPortMessage msg = new TDKB200LoadPortMessage();
  30. msg.RawMessage = rawMsg;
  31. msg.IsAck = false;
  32. msg.IsResponse = false;
  33. msg.IsComplete = false;
  34. msg.IsResponse = false;
  35. msg.IsNak = false;
  36. msg.IsError = false;
  37. msg.CommandType = rawMsg.Split(':')[0].Replace("s00", "");
  38. msg.Command = Regex.Match(rawMsg, "(?<=:).*?(?=;)").Value;
  39. if (msg.CommandType.Contains("ACK")) msg.IsAck = true;
  40. if (msg.CommandType.Contains("NAK"))
  41. {
  42. msg.IsNak = true;
  43. _device.OnError($"Received NAK:{rawMsg}");
  44. }
  45. if (msg.CommandType.Contains("MOV")) msg.IsAck = true;
  46. if (msg.CommandType.Contains("GET")) msg.IsAck = true;
  47. if (msg.CommandType.Contains("INF"))
  48. {
  49. msg.IsEvent = true;
  50. }
  51. if (msg.CommandType.Contains("ABS"))
  52. {
  53. _device.OnAbs(rawMsg);
  54. msg.IsError = true;
  55. }
  56. LOG.Write($"{Address} received message:{rawMsg}");
  57. return msg;
  58. }
  59. //protected override MessageBase ParseResponse(byte[] byteMsg)
  60. //{
  61. // byte[] temp = new byte[] { };
  62. // foreach (byte bmsg in byteMsg)
  63. // {
  64. // _lstCacheBuffer.Add(bmsg);
  65. // if (bmsg == 0xD)
  66. // {
  67. // temp = _lstCacheBuffer.ToArray();
  68. // _lstCacheBuffer.Clear();
  69. // }
  70. // }
  71. // HirataLoadPortMessage msg = new HirataLoadPortMessage();
  72. // msg.RawMessage = Encoding.ASCII.GetString(temp);
  73. // msg.IsAck = false;
  74. // msg.IsResponse = false;
  75. // msg.IsComplete = false;
  76. // msg.IsResponse = false;
  77. // msg.IsNak = false;
  78. // msg.IsError = false;
  79. // if (temp.LastOrDefault() != 0xD)
  80. // {
  81. // return msg;
  82. // }
  83. // msg.CommandType = msg.RawMessage.Substring(5, 3);
  84. // msg.Command = Regex.Match(msg.RawMessage, "(?<=:).*?(?=;)").Value;
  85. // if (msg.CommandType.Contains("ACK")) msg.IsAck = true;
  86. // if (msg.CommandType.Contains("NAK")) msg.IsNak = true;
  87. // if (msg.CommandType.Contains("MOV")) msg.IsAck = true;
  88. // if (msg.CommandType.Contains("GET")) msg.IsAck = true;
  89. // if (msg.CommandType.Contains("INF"))
  90. // {
  91. // msg.IsEvent = true;
  92. // }
  93. // if (msg.CommandType.Contains("ABS"))
  94. // {
  95. // msg.IsError = true;
  96. // }
  97. // LOG.Write($"{Address} received message:{msg.RawMessage}");
  98. // return msg;
  99. //}
  100. protected override void OnEventArrived(MessageBase msg)
  101. {
  102. TDKB200LoadPortMessage message = msg as TDKB200LoadPortMessage;
  103. string evtcontent = message.RawMessage;
  104. if (!IsBusy)
  105. _device.OnEvent(out _);
  106. if (evtcontent.Contains("PODON"))
  107. {
  108. _device.OnCarrierPresent();
  109. _device.OnCarrierPlaced();
  110. }
  111. if (evtcontent.Contains("PODOF"))
  112. {
  113. _device.OnCarrierNotPlaced();
  114. _device.OnCarrierNotPresent();
  115. }
  116. if (evtcontent.Contains("ABNST"))
  117. {
  118. _device.OnCarrierNotPlaced();
  119. _device.OnCarrierPresent();
  120. }
  121. if (evtcontent.Contains("SMTON"))
  122. {
  123. _device.OnCarrierNotPlaced();
  124. _device.OnCarrierPresent();
  125. }
  126. if (evtcontent.Contains("MANSW"))
  127. {
  128. _device.OnSwitchKey1();
  129. }
  130. }
  131. }
  132. public class TDKB200LoadPortTCPConnection : TCPPortConnectionBase
  133. {
  134. private TDKB200LoadPort _device;
  135. public TDKB200LoadPortTCPConnection(TDKB200LoadPort device, string ipaddress)
  136. : base(ipaddress)
  137. {
  138. _device = device;
  139. }
  140. protected override MessageBase ParseResponse(string rawMsg)
  141. {
  142. TDKBLoadPortMessage msg = new TDKBLoadPortMessage();
  143. msg.RawMessage = rawMsg;
  144. msg.IsAck = false;
  145. msg.IsResponse = false;
  146. msg.IsComplete = false;
  147. msg.IsResponse = false;
  148. msg.IsNak = false;
  149. msg.IsError = false;
  150. msg.CommandType = rawMsg.Split(':')[0].Replace("s00","");
  151. msg.Command = Regex.Match(rawMsg, "(?<=:).*?(?=;)").Value;
  152. if (msg.CommandType.Contains("ACK")) msg.IsAck = true;
  153. if (msg.CommandType.Contains("NAK"))
  154. {
  155. msg.IsNak = true;
  156. _device.OnError($"Received NAK:{rawMsg}");
  157. }
  158. if (msg.CommandType.Contains("MOV")) msg.IsAck = true;
  159. if (msg.CommandType.Contains("GET")) msg.IsAck = true;
  160. if (msg.CommandType.Contains("INF"))
  161. {
  162. msg.IsEvent = true;
  163. }
  164. if (msg.CommandType.Contains("ABS"))
  165. {
  166. _device.OnAbs(rawMsg);
  167. msg.IsError = true;
  168. }
  169. LOG.Write($"{Address} received message:{rawMsg}");
  170. return msg;
  171. }
  172. protected override void OnEventArrived(MessageBase msg)
  173. {
  174. TDKBLoadPortMessage message = msg as TDKBLoadPortMessage;
  175. string evtcontent = message.RawMessage;
  176. if (evtcontent.Contains("PODON"))
  177. {
  178. _device.OnCarrierPresent();
  179. _device.OnCarrierPlaced();
  180. }
  181. if (evtcontent.Contains("PODOF"))
  182. {
  183. _device.OnCarrierNotPlaced();
  184. _device.OnCarrierNotPresent();
  185. }
  186. if (evtcontent.Contains("ABNST"))
  187. {
  188. _device.OnCarrierNotPlaced();
  189. _device.OnCarrierPresent();
  190. }
  191. if (evtcontent.Contains("SMTON"))
  192. {
  193. _device.OnCarrierNotPlaced();
  194. _device.OnCarrierPresent();
  195. }
  196. if (evtcontent.Contains("MANSW"))
  197. {
  198. _device.OnSwitchKey1();
  199. }
  200. _device.OnEvent(out _);
  201. }
  202. }
  203. }