SELP8LoadPortConnection.cs 7.7 KB

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