GeneralBarcodeReader.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 GeneralBarcodeReader : 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 string _readCmd;
  29. private string _endCmd;
  30. private string _receivedMsg = string.Empty;
  31. private object _locker = new object();
  32. DateTime _scanStartTimer;
  33. private int _retryTimes;
  34. private int _idLength;
  35. public GeneralBarcodeReader(string module, string name, string scRoot, string readcmd, LoadPortBaseDevice lp = null,string endcmd="",int readerIndex=1) : base(module, name, lp,readerIndex)
  36. {
  37. _scRoot = scRoot;
  38. _readCmd = readcmd;
  39. _endCmd = endcmd;
  40. _portName = SC.GetStringValue($"{_scRoot}.{Name}.PortName");
  41. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  42. _baudRate = SC.GetValue<int>($"{_scRoot}.{Name}.BaudRate");
  43. _dataBits = SC.GetValue<int>($"{_scRoot}.{Name}.DataBits");
  44. _parity = (Parity)Enum.Parse(typeof(Parity), SC.GetStringValue($"{_scRoot}.{Name}.Parity"));
  45. _stopBits = SC.GetValue<int>($"{_scRoot}.{Name}.StopBits");
  46. _port = new AsyncSerialPort(_portName, _baudRate, 8);//, Parity.Even, StopBits.One, "\r", true);
  47. _port.EnableLog = _enableLog;
  48. _port.OnDataChanged += _port_OnDataChanged;
  49. _port.OnErrorHappened += _port_OnErrorHappened;
  50. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 5;
  51. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
  52. if (sleep <= 0 || sleep > 10)
  53. sleep = 2;
  54. int retry = 0;
  55. do
  56. {
  57. if (_port.Open())
  58. {
  59. EV.PostInfoLog(Module, $"Connected with {Module}.{Name} .");
  60. break;
  61. }
  62. else
  63. {
  64. EV.PostInfoLog(Module, $"Can't connected with {Module}.{Name},retry time {retry}.");
  65. _port.Close();
  66. Thread.Sleep(sleep * 1000);
  67. }
  68. if (count > 0 && retry++ > count)
  69. {
  70. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name}.");
  71. break;
  72. }
  73. } while (true);
  74. Reset();
  75. }
  76. public GeneralBarcodeReader(string module, string name, string scRoot, string readcmd, bool fullparameterbySc,LoadPortBaseDevice lp = null, string endcmd = "", int readerIndex = 1,int idLength =0) : base(module, name, lp, readerIndex)
  77. {
  78. _scRoot = scRoot;
  79. _readCmd = readcmd;
  80. _endCmd = endcmd;
  81. _idLength = idLength;
  82. _portName = SC.GetStringValue($"{_scRoot}.{Name}.PortName");
  83. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  84. _baudRate = SC.GetValue<int>($"{_scRoot}.{Name}.BaudRate");
  85. _dataBits = SC.GetValue<int>($"{_scRoot}.{Name}.DataBits");
  86. _parity = (Parity)Enum.Parse(typeof(Parity), Enum.GetName(typeof(Parity), int.Parse(SC.GetStringValue($"{_scRoot}.{Name}.Parity"))));
  87. StopBits stopBits = (StopBits)Enum.Parse(typeof(StopBits), Enum.GetName(typeof(StopBits), int.Parse(SC.GetStringValue($"{_scRoot}.{Name}.StopBits"))));
  88. _port = new AsyncSerialPort(_portName, _baudRate, _dataBits, _parity, stopBits);//, Parity.Even, StopBits.One, "\r", true);
  89. _port.EnableLog = _enableLog;
  90. _port.OnDataChanged += _port_OnDataChanged;
  91. _port.OnErrorHappened += _port_OnErrorHappened;
  92. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 5;
  93. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
  94. if (sleep <= 0 || sleep > 10)
  95. sleep = 2;
  96. int retry = 0;
  97. do
  98. {
  99. if (_port.Open())
  100. {
  101. EV.PostInfoLog(Module, $"Connected with {Module}.{Name} .");
  102. break;
  103. }
  104. else
  105. {
  106. EV.PostInfoLog(Module, $"Can't connected with {Module}.{Name},retry time {retry}.");
  107. _port.Close();
  108. Thread.Sleep(sleep * 1000);
  109. }
  110. if (count > 0 && retry++ > count)
  111. {
  112. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name}.");
  113. break;
  114. }
  115. } while (true);
  116. Reset();
  117. }
  118. public override void Reset()
  119. {
  120. if(!_port.IsOpen())
  121. {
  122. if (_port.Open())
  123. {
  124. EV.PostInfoLog(Module, $"Connected with {Module}.{Name}");
  125. }
  126. else
  127. {
  128. EV.PostInfoLog(Module, $"Can't connected with {Module}.{Name}");
  129. _port.Close();
  130. }
  131. }
  132. base.Reset();
  133. }
  134. private void _port_OnErrorHappened(string obj)
  135. {
  136. OnError();
  137. }
  138. private void _port_OnDataChanged(string package)
  139. {
  140. try
  141. {
  142. lock (_locker)
  143. {
  144. if (DeviceState == CIDReaderStateEnum.ReadCarrierID)
  145. {
  146. CarrierIDBeRead = CarrierIDBeRead + package;
  147. if (!CarrierIDBeRead.Contains("\r")) return;
  148. CarrierIDBeRead = CarrierIDBeRead.Split('\r')[0].Replace("\n", "");
  149. if(_idLength !=0 && CarrierIDBeRead.Length > _idLength)
  150. {
  151. CarrierIDBeRead = CarrierIDBeRead.Substring(0, _idLength);
  152. }
  153. if (string.IsNullOrEmpty(CarrierIDBeRead) || CarrierIDBeRead == "BR" || CarrierIDBeRead.Contains("UNKNOWN_CMD"))
  154. {
  155. CarrierIDBeRead = "";
  156. if (_retryTimes <10)
  157. {
  158. Thread.Sleep(200);
  159. _port.Write(_readCmd);
  160. _retryTimes++;
  161. return;
  162. }
  163. OnCarrierIDReadFailed("Scan Failed.");
  164. return;
  165. }
  166. OnCarrierIDRead(CarrierIDBeRead.Replace("\r","").Replace("\n",""));
  167. }
  168. else
  169. {
  170. CarrierIDBeRead = CarrierIDBeRead + package;
  171. if (!CarrierIDBeRead.Contains("\r")) return;
  172. OnCarrierIDRead(CarrierIDBeRead.Replace("\r", "").Replace("\n", ""));
  173. CarrierIDBeRead = "";
  174. }
  175. }
  176. }
  177. catch (Exception ex)
  178. {
  179. LOG.Write(ex);
  180. }
  181. }
  182. public string Address { get; set; }
  183. public bool IsConnected => _port.IsOpen();
  184. public bool Connect()
  185. {
  186. return _port.Open();
  187. }
  188. public bool Disconnect()
  189. {
  190. return _port.Close();
  191. }
  192. protected override bool fStartWriteCarrierID(object[] param)
  193. {
  194. return false;
  195. }
  196. protected override bool fStartReadCarrierID(object[] param)
  197. {
  198. try
  199. {
  200. lock (_locker)
  201. {
  202. if (!_port.IsOpen())
  203. {
  204. EV.PostWarningLog(Module, _portName + " not open, can not scan");
  205. return false;
  206. }
  207. CarrierIDBeRead = string.Empty;
  208. if(!string.IsNullOrEmpty(_readCmd))
  209. _port.Write(_readCmd);
  210. _scanStartTimer = DateTime.Now;
  211. _retryTimes = 0;
  212. return true;
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. LOG.Write(ex);
  218. return false;
  219. }
  220. }
  221. protected override bool fMonitorReadCarrierID(object[] param)
  222. {
  223. if (DateTime.Now - _scanStartTimer > TimeSpan.FromSeconds(ActionTimeLimit))
  224. {
  225. EV.PostWarningLog(Module, _portName + " error, timeout");
  226. OnCarrierIDReadFailed("Timeout");
  227. if(!string.IsNullOrEmpty(_endCmd))
  228. _port.Write(_endCmd);
  229. return true;
  230. }
  231. return false;
  232. }
  233. public override void Terminate()
  234. {
  235. _port.Close();
  236. base.Terminate();
  237. }
  238. }
  239. }