OmronBarcodeReader.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.OnDataChanged += _port_OnDataReceived;
  38. _port.OnErrorHappened += _port_OnErrorHappened;
  39. }
  40. public override bool Initialize()
  41. {
  42. Task.Factory.StartNew(() =>
  43. {
  44. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 3;
  45. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
  46. if (sleep <= 0 || sleep > 10)
  47. sleep = 2;
  48. int retry = 0;
  49. do
  50. {
  51. if (_port.Open())
  52. {
  53. //LOG.Write($"Connected with {Module}.{Name} .");
  54. EV.PostInfoLog(Module, $"Connected with {Module}.{Name} .");
  55. break;
  56. }
  57. if (count>0 && retry++ > count)
  58. {
  59. LOG.Write($"Retry connect {Module}.{Name} stop retry.");
  60. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name} .");
  61. break;
  62. }
  63. {
  64. Thread.Sleep(sleep * 1000);
  65. LOG.Write($"Retry connect {Module}.{Name} for the {retry + 1} time.");
  66. }
  67. } while (true);
  68. });
  69. // RetryInstance.Instance().Execute<bool>(
  70. // ()=> _port.Open(),
  71. // SC.GetValue<int>("System.ComPortRetryDelayTime"),
  72. // SC.GetValue<int>("System.ComPortRetryCount"),
  73. // true
  74. // );
  75. return true;
  76. }
  77. private bool InvokeScan(string arg1, object[] arg2)
  78. {
  79. Scan();
  80. return true;
  81. }
  82. private bool InvokeStopScan(string arg1, object[] arg2)
  83. {
  84. StopScan();
  85. return true;
  86. }
  87. public void Scan()
  88. {
  89. if (!_port.IsOpen())
  90. {
  91. EV.PostWarningLog(Module, _scannerName + " not open, can not scan");
  92. return;
  93. }
  94. lock (_locker)
  95. {
  96. if (!_isScanning)
  97. {
  98. _scanTimer.Start(0);
  99. _cmdScan = true;
  100. _cmdStopScan = false;
  101. }
  102. }
  103. }
  104. public void StopScan()
  105. {
  106. if (!_port.IsOpen())
  107. {
  108. EV.PostWarningLog(Module, _scannerName + " not open, can not stop scan");
  109. return;
  110. }
  111. lock (_locker)
  112. {
  113. if (_isScanning)
  114. {
  115. _cmdScan = false;
  116. _cmdStopScan = true;
  117. }
  118. }
  119. }
  120. private void _port_OnErrorHappened(string obj)
  121. {
  122. EV.PostWarningLog(Module, _scannerName + " error, " + obj);
  123. }
  124. private void _port_OnDataReceived(string obj)
  125. {
  126. lock (_locker)
  127. {
  128. if (_isScanning)
  129. {
  130. _scanResult = obj.Replace("\r","");
  131. _msgScanned = true;
  132. }
  133. }
  134. }
  135. public override void Monitor()
  136. {
  137. lock (_locker)
  138. {
  139. if (_isScanning)
  140. {
  141. if (_cmdStopScan)
  142. {
  143. _cmdStopScan = false;
  144. _isScanning = false;
  145. PerformStopTriggerScan();
  146. }
  147. else
  148. {
  149. if (_msgScanned)
  150. {
  151. _msgScanned = false;
  152. _isScanning = false;
  153. ReadOk(_scanResult);
  154. }
  155. else
  156. {
  157. if (_scanTimer.GetElapseTime() > _timeout)
  158. {
  159. _isScanning = false;
  160. EV.PostWarningLog(Module, _scannerName + " error, timeout");
  161. ReadFailed();
  162. }
  163. }
  164. }
  165. }
  166. else
  167. {
  168. if (_cmdScan)
  169. {
  170. _cmdScan = false;
  171. _isScanning = true;
  172. PerformTriggerScan();
  173. _scanTimer.Start(0);
  174. }
  175. }
  176. }
  177. }
  178. public override void Terminate()
  179. {
  180. _port.Close();
  181. }
  182. public override void Reset()
  183. {
  184. if (!_port.IsOpen())
  185. {
  186. if (_port.Open())
  187. {
  188. EV.PostMessage("System", EventEnum.GeneralInfo, _scannerName + " opened, ");
  189. }
  190. }
  191. }
  192. private void PerformTriggerScan()
  193. {
  194. _port.Write(new byte[] { 0x1B, 0x5A, 0x0D });
  195. }
  196. private void PerformStopTriggerScan()
  197. {
  198. //_port.Write(new byte[] { 22, 85, 13 });
  199. }
  200. }
  201. }