OmronBarcodeReader.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Aitex.Core.RT.Device;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.Communications;
  10. using MECF.Framework.Common.Equipment;
  11. using MECF.Framework.Common.Utilities;
  12. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.OmronBarcode
  13. {
  14. public class OmronBarcodeReader : CarrierIdReader
  15. {
  16. private AsyncSerialPort _port;
  17. private string _scannerName;
  18. //private int _scan;
  19. private int _timeout;
  20. private int _retryTime;
  21. DeviceTimer _scanTimer = new DeviceTimer();
  22. private bool _cmdScan;
  23. private bool _cmdStopScan;
  24. //private bool _msgError;
  25. private bool _msgScanned;
  26. public string _scanResult;
  27. private object _locker = new object();
  28. private bool _isScanning;
  29. public OmronBarcodeReader(string module, string scannerName, string portName, int timeout, int retryTime):base(module, scannerName)
  30. {
  31. Module = module;
  32. Name = scannerName;
  33. _scannerName = scannerName;
  34. _timeout = timeout*1000;
  35. _retryTime = retryTime;
  36. _port = new AsyncSerialPort(portName, 9600, 8 );
  37. _port.EnableLog = true;
  38. _port.OnDataChanged += _port_OnDataReceived;
  39. _port.OnErrorHappened += _port_OnErrorHappened;
  40. }
  41. public override bool Initialize()
  42. {
  43. Task.Factory.StartNew(() =>
  44. {
  45. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 50;
  46. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 1;
  47. if (sleep <= 0 || sleep > 10)
  48. sleep = 2;
  49. int retry = 0;
  50. do
  51. {
  52. if (_port.Open())
  53. {
  54. //LOG.Write($"Connected with {Module}.{Name} .");
  55. EV.PostInfoLog(Module, $"Connected with {Module}.{Name} .");
  56. break;
  57. }
  58. if (count>0 && retry++ > count)
  59. {
  60. LOG.Write($"Retry connect {Module}.{Name} stop retry.");
  61. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name} .");
  62. break;
  63. }
  64. {
  65. Thread.Sleep(sleep * 1000);
  66. LOG.Write($"Retry connect {Module}.{Name} for the {retry + 1} time.");
  67. }
  68. } while (true);
  69. });
  70. // RetryInstance.Instance().Execute<bool>(
  71. // ()=> _port.Open(),
  72. // SC.GetValue<int>("System.ComPortRetryDelayTime"),
  73. // SC.GetValue<int>("System.ComPortRetryCount"),
  74. // true
  75. // );
  76. return true;
  77. }
  78. public override bool Read(out string reason)
  79. {
  80. Scan();
  81. reason = "";
  82. return true;
  83. }
  84. private bool InvokeScan(string arg1, object[] arg2)
  85. {
  86. Scan();
  87. return true;
  88. }
  89. private bool InvokeStopScan(string arg1, object[] arg2)
  90. {
  91. StopScan();
  92. return true;
  93. }
  94. public void Scan()
  95. {
  96. if (!_port.IsOpen())
  97. {
  98. EV.PostWarningLog(Module, _scannerName + " not open, can not scan");
  99. return;
  100. }
  101. lock (_locker)
  102. {
  103. if (!_isScanning)
  104. {
  105. _scanTimer.Start(0);
  106. _cmdScan = true;
  107. _cmdStopScan = false;
  108. }
  109. }
  110. }
  111. public void StopScan()
  112. {
  113. if (!_port.IsOpen())
  114. {
  115. EV.PostWarningLog(Module, _scannerName + " not open, can not stop scan");
  116. return;
  117. }
  118. lock (_locker)
  119. {
  120. if (_isScanning)
  121. {
  122. _cmdScan = false;
  123. _cmdStopScan = true;
  124. }
  125. }
  126. }
  127. private void _port_OnErrorHappened(string obj)
  128. {
  129. EV.PostWarningLog(Module, _scannerName + " error, " + obj);
  130. }
  131. private void _port_OnDataReceived(string obj)
  132. {
  133. lock (_locker)
  134. {
  135. if (_isScanning)
  136. {
  137. _scanResult = obj.Replace("\r","");
  138. _msgScanned = true;
  139. }
  140. }
  141. }
  142. public override void Monitor()
  143. {
  144. lock (_locker)
  145. {
  146. if (_isScanning)
  147. {
  148. if (_cmdStopScan)
  149. {
  150. _cmdStopScan = false;
  151. _isScanning = false;
  152. PerformStopTriggerScan();
  153. }
  154. else
  155. {
  156. if (_msgScanned)
  157. {
  158. _msgScanned = false;
  159. _isScanning = false;
  160. ReadOk(_scanResult);
  161. }
  162. else
  163. {
  164. if (_scanTimer.GetElapseTime() > _timeout)
  165. {
  166. _isScanning = false;
  167. EV.PostWarningLog(Module, _scannerName + " error, timeout");
  168. ReadFailed();
  169. }
  170. }
  171. }
  172. }
  173. else
  174. {
  175. if (_cmdScan)
  176. {
  177. _cmdScan = false;
  178. _isScanning = true;
  179. PerformTriggerScan();
  180. _scanTimer.Start(0);
  181. }
  182. }
  183. }
  184. }
  185. public override void Terminate()
  186. {
  187. _port.Close();
  188. }
  189. public override void Reset()
  190. {
  191. if (!_port.IsOpen())
  192. {
  193. if (_port.Open())
  194. {
  195. EV.PostMessage("System", EventEnum.GeneralInfo, _scannerName + " opened, ");
  196. }
  197. }
  198. }
  199. private void PerformTriggerScan()
  200. {
  201. _port.Write(new byte[] { 0x1B, 0x5A, 0x0D });
  202. }
  203. private void PerformStopTriggerScan()
  204. {
  205. //_port.Write(new byte[] { 22, 85, 13 });
  206. }
  207. }
  208. }