TDKLoadPortConnection.cs 7.3 KB

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