KeyenceBarcodeReader.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using MECF.Framework.Common.Communications;
  5. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.CarrierIDReaderBase;
  6. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
  7. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.LoadPortBase;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.Honeywell
  16. {
  17. public class KeyenceBarcodeReader : CIDReaderBaseDevice, IConnection
  18. {
  19. private string _scRoot;
  20. private DateTime _startTime;
  21. public KeyenceBarcodeReader(string module, string name, string scRoot, LoadPortBaseDevice lp = null) : base(module, name, lp)
  22. {
  23. _scRoot = scRoot;
  24. _address = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  25. bool isenablelog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  26. _socket = new AsyncSocket(_address);
  27. _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged);
  28. _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
  29. ConnectionManager.Instance.Subscribe($"{Name}", this);
  30. //int connecttime = 0;
  31. _socket.Connect(_address);
  32. }
  33. private void OnErrorHandler(ErrorEventArgs args)
  34. {
  35. OnError();
  36. }
  37. private void OnDataChanged(string package)
  38. {
  39. try
  40. {
  41. package = package.ToUpper();
  42. string[] msgs = Regex.Split(package, "\r");
  43. foreach (string msg in msgs)
  44. {
  45. if (!string.IsNullOrEmpty(msg.Replace("\r", "")))
  46. {
  47. //string msgdata = "LOFF\r";
  48. //_socket.Write(msgdata);
  49. if (DeviceState == CIDReaderStateEnum.ReadCarrierID)
  50. OnCarrierIDRead(msg);
  51. }
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. LOG.Write(ex);
  57. }
  58. }
  59. protected override bool fStartReset(object[] param)
  60. {
  61. if (_socket.IsConnected)
  62. return true;
  63. else
  64. {
  65. _address = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  66. _socket.Connect(_address);
  67. }
  68. return true;
  69. }
  70. protected override bool fMonitorReset(object[] param)
  71. {
  72. if (_socket.IsConnected)
  73. return true;
  74. else
  75. {
  76. _address = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  77. _socket.Connect(_address);
  78. if (_socket.IsConnected)
  79. return true;
  80. }
  81. return false;
  82. }
  83. protected override bool fStartReadCarrierID(object[] param)
  84. {
  85. try
  86. {
  87. _startTime = DateTime.Now;
  88. string msgdata = "LON\r";
  89. return _socket.Write(msgdata);
  90. }
  91. catch (Exception ex)
  92. {
  93. LOG.Write(ex);
  94. return false;
  95. }
  96. }
  97. protected override bool fStartWriteCarrierID(object[] param)
  98. {
  99. return false;
  100. }
  101. protected override bool fMonitorReadCarrierID(object[] param)
  102. {
  103. if (DateTime.Now - _startTime > TimeSpan.FromSeconds(30))
  104. {
  105. string msgdata = "LOFF\r";
  106. _socket.Write(msgdata);
  107. OnCarrierIDReadFailed("Timeout");
  108. return true;
  109. }
  110. return base.fMonitorReadCarrierID(param);
  111. }
  112. protected override bool fMonitorWriteCarrierID(object[] param)
  113. {
  114. return base.fMonitorReadCarrierID(param);
  115. }
  116. public string Address => _address;
  117. private string _address;
  118. private AsyncSocket _socket;
  119. public bool IsConnected => _socket.IsConnected;
  120. public int Offset { get; private set; }
  121. public int Length { get; private set; }
  122. public bool Connect()
  123. {
  124. _socket.Connect(_address);
  125. return true;
  126. }
  127. public bool Disconnect()
  128. {
  129. _socket.Dispose();
  130. return true;
  131. }
  132. private string getErrMsg(string error)
  133. {
  134. string msg = "";
  135. switch (error)
  136. {
  137. case "14":
  138. msg = "Format error There is a mistake in the command format";
  139. break;
  140. case "70":
  141. msg = "Communications error Noise or another hindrance occurs during communications with an ID Tag, and communications cannot be completed normally.";
  142. break;
  143. case "71":
  144. msg = "Verification error Correct data cannot be written to an ID Tag";
  145. break;
  146. case "72":
  147. msg = "No Tag error Either there is no ID Tag in front of the CIDRW Head, or the CIDRW Head is unable to detect the ID Tag due to environmental factors";
  148. break;
  149. case "7B":
  150. msg = "Outside write area error A write operation was not completed normally because the ID Tag was in an area in which the ID Tag could be read but not written";
  151. break;
  152. case "7E":
  153. msg = "ID system error (1) The ID Tag is in a status where it cannot execute command processing";
  154. break;
  155. case "7F":
  156. msg = "ID system error (2) An inapplicable ID Tag has been used";
  157. break;
  158. case "9A":
  159. msg = "Hardware error in CPU An error occurred when writing to EEPROM.";
  160. break;
  161. }
  162. return msg;
  163. }
  164. public string HEX2ASCII(string hex)
  165. {
  166. string res = String.Empty;
  167. try
  168. {
  169. for (int a = 0; a < hex.Length; a = a + 2)
  170. {
  171. string Char2Convert = hex.Substring(a, 2);
  172. int n = Convert.ToInt32(Char2Convert, 16);
  173. char c = (char)n;
  174. res += c.ToString();
  175. }
  176. }
  177. catch (Exception e)
  178. {
  179. LOG.Write(e);
  180. }
  181. return res;
  182. }
  183. private string GetPage(int startpage, int length)
  184. {
  185. double dpage = 0;
  186. for (int i = 0; i < length; i++)
  187. {
  188. dpage = dpage + Math.Pow(2, startpage + 1 + i);
  189. }
  190. string pageret = String.Format("{0:X}", Convert.ToInt32(dpage));
  191. for (int j = pageret.Length; j < 8; j++)
  192. {
  193. pageret = "0" + pageret;
  194. }
  195. return pageret;
  196. }
  197. private string ASCII2HEX(string src, int length)
  198. {
  199. while (src.Length < length * 8)
  200. {
  201. src = src + '\0';
  202. }
  203. if (src.Length > length * 8)
  204. {
  205. src = src.Substring(0, length * 8);
  206. LOG.Write($"RFID support max {(length * 8).ToString()} characters");
  207. }
  208. string res = String.Empty;
  209. try
  210. {
  211. char[] charValues = src.ToCharArray();
  212. string hexOutput = "";
  213. foreach (char _eachChar in charValues)
  214. {
  215. // Get the integral value of the character.
  216. int value = Convert.ToInt32(_eachChar);
  217. // Convert the decimal value to a hexadecimal value in string form.
  218. hexOutput += String.Format("{0:X2}", value);
  219. // to make output as your eg
  220. // hexOutput +=" "+ String.Format("{0:X}", value);
  221. }
  222. return hexOutput;
  223. }
  224. catch (Exception e)
  225. {
  226. LOG.Write(e);
  227. }
  228. return res;
  229. }
  230. }
  231. }