OmronBarcodeReaderII.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using Aitex.Core.Util;
  5. using MECF.Framework.Common.Communications;
  6. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.CarrierIDReaderBase;
  7. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.LoadPortBase;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO.Ports;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.OmronV640
  17. {
  18. public class OmronBarcodeReaderII : CIDReaderBaseDevice, IConnection
  19. {
  20. private string _scRoot;
  21. private string _portName;
  22. private bool _enableLog;
  23. private int _baudRate;
  24. private int _dataBits;
  25. private Parity _parity;
  26. private int _stopBits;
  27. private AsyncSerialPort _port;
  28. private object _locker = new object();
  29. DateTime _scanStartTimer;
  30. public OmronBarcodeReaderII(string module, string name, string scRoot, LoadPortBaseDevice lp = null) : base(module, name, lp)
  31. {
  32. _scRoot = scRoot;
  33. _portName = SC.GetStringValue($"{_scRoot}.{Name}.PortName");
  34. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  35. _baudRate = SC.GetValue<int>($"{_scRoot}.{Name}.BaudRate");
  36. _dataBits = SC.GetValue<int>($"{_scRoot}.{Name}.DataBits");
  37. _parity = (Parity)Enum.Parse(typeof(Parity), SC.GetStringValue($"{_scRoot}.{Name}.Parity"));
  38. _stopBits = SC.GetValue<int>($"{_scRoot}.{Name}.StopBits");
  39. _port = new AsyncSerialPort(_portName, _baudRate, 8);//, Parity.Even, StopBits.One, "\r", true);
  40. _port.OnDataChanged += _port_OnDataChanged;
  41. _port.OnErrorHappened += _port_OnErrorHappened;
  42. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 5;
  43. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
  44. if (sleep <= 0 || sleep > 10)
  45. sleep = 2;
  46. int retry = 0;
  47. do
  48. {
  49. if (_port.Open())
  50. {
  51. EV.PostInfoLog(Module, $"Connected with {Module}.{Name} .");
  52. break;
  53. }
  54. else
  55. {
  56. EV.PostInfoLog(Module, $"Can't connected with {Module}.{Name},retry time {retry}.");
  57. _port.Close();
  58. Thread.Sleep(sleep * 1000);
  59. }
  60. if (count > 0 && retry++ > count)
  61. {
  62. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name}.");
  63. break;
  64. }
  65. } while (true);
  66. }
  67. private void _port_OnErrorHappened(string obj)
  68. {
  69. OnError();
  70. }
  71. private void _port_OnDataChanged(string package)
  72. {
  73. try
  74. {
  75. lock (_locker)
  76. {
  77. if (DeviceState == CIDReaderStateEnum.ReadCarrierID)
  78. {
  79. if(package.Replace("\r", "").Length > _charNumberLimit)
  80. CarrierIDBeRead = package.Replace("\r", "").Substring(0, _charNumberLimit);
  81. else
  82. CarrierIDBeRead = package.Replace("\r", "");
  83. OnCarrierIDRead(CarrierIDBeRead);
  84. }
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. LOG.Write(ex);
  90. }
  91. }
  92. private int _charNumberLimit
  93. {
  94. get
  95. {
  96. if (SC.ContainsItem($"{_scRoot}.{Name}.CharNumberLimit"))
  97. return SC.GetValue<int>($"{_scRoot}.{Name}.CharNumberLimit");
  98. return 9999;
  99. }
  100. }
  101. public string Address { get; set; }
  102. public bool IsConnected => _port.IsOpen();
  103. public bool Connect()
  104. {
  105. return _port.Open();
  106. }
  107. public bool Disconnect()
  108. {
  109. return _port.Close();
  110. }
  111. protected override bool fStartWriteCarrierID(object[] param)
  112. {
  113. return false;
  114. }
  115. protected override bool fStartReadCarrierID(object[] param)
  116. {
  117. try
  118. {
  119. lock (_locker)
  120. {
  121. if (!_port.IsOpen())
  122. {
  123. EV.PostWarningLog(Module, _portName + " not open, can not scan");
  124. return false;
  125. }
  126. _port.Write(new byte[] { 0x1B, 0x5A, 0x0D });
  127. _scanStartTimer = DateTime.Now;
  128. return true;
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. LOG.Write(ex);
  134. return false;
  135. }
  136. }
  137. protected override bool fMonitorReadCarrierID(object[] param)
  138. {
  139. if(DateTime.Now - _scanStartTimer > TimeSpan.FromSeconds(60))
  140. {
  141. EV.PostWarningLog(Module, _portName + " error, timeout");
  142. OnCarrierIDReadFailed("Timeout");
  143. return true;
  144. }
  145. return false;
  146. }
  147. private string getErrMsg(string error)
  148. {
  149. string msg = "";
  150. switch (error)
  151. {
  152. case "14":
  153. msg = "Format error There is a mistake in the command format";
  154. break;
  155. case "70":
  156. msg = "Communications error Noise or another hindrance occurs during communications with an ID Tag, and communications cannot be completed normally.";
  157. break;
  158. case "71":
  159. msg = "Verification error Correct data cannot be written to an ID Tag";
  160. break;
  161. case "72":
  162. 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";
  163. break;
  164. case "7B":
  165. 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";
  166. break;
  167. case "7E":
  168. msg = "ID system error (1) The ID Tag is in a status where it cannot execute command processing";
  169. break;
  170. case "7F":
  171. msg = "ID system error (2) An inapplicable ID Tag has been used";
  172. break;
  173. case "9A":
  174. msg = "Hardware error in CPU An error occurred when writing to EEPROM.";
  175. break;
  176. }
  177. return msg;
  178. }
  179. public string HEX2ASCII(string hex)
  180. {
  181. string res = String.Empty;
  182. try
  183. {
  184. for (int a = 0; a < hex.Length; a = a + 2)
  185. {
  186. string Char2Convert = hex.Substring(a, 2);
  187. int n = Convert.ToInt32(Char2Convert, 16);
  188. char c = (char)n;
  189. res += c.ToString();
  190. }
  191. }
  192. catch (Exception e)
  193. {
  194. LOG.Write(e);
  195. }
  196. return res;
  197. }
  198. private string GetPage(int startpage, int length)
  199. {
  200. double dpage = 0;
  201. for (int i = 0; i < length; i++)
  202. {
  203. dpage = dpage + Math.Pow(2, startpage + 1 + i);
  204. }
  205. string pageret = String.Format("{0:X}", Convert.ToInt32(dpage));
  206. for (int j = pageret.Length; j < 8; j++)
  207. {
  208. pageret = "0" + pageret;
  209. }
  210. return pageret;
  211. }
  212. private string ASCII2HEX(string src, int length)
  213. {
  214. while (src.Length < length * 8)
  215. {
  216. src = src + '\0';
  217. }
  218. if (src.Length > length * 8)
  219. {
  220. src = src.Substring(0, length * 8);
  221. LOG.Write($"RFID support max {(length * 8).ToString()} characters");
  222. }
  223. string res = String.Empty;
  224. try
  225. {
  226. char[] charValues = src.ToCharArray();
  227. string hexOutput = "";
  228. foreach (char _eachChar in charValues)
  229. {
  230. // Get the integral value of the character.
  231. int value = Convert.ToInt32(_eachChar);
  232. // Convert the decimal value to a hexadecimal value in string form.
  233. hexOutput += String.Format("{0:X2}", value);
  234. // to make output as your eg
  235. // hexOutput +=" "+ String.Format("{0:X}", value);
  236. }
  237. return hexOutput;
  238. }
  239. catch (Exception e)
  240. {
  241. LOG.Write(e);
  242. }
  243. return res;
  244. }
  245. }
  246. }