HirataLoadPortConnection.cs 7.8 KB

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