GeneralBarcodeReader.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. public GeneralBarcodeReader(string module, string name, string scRoot, string readcmd, LoadPortBaseDevice lp = null,string endcmd="",int readerIndex=1) : base(module, name, lp,readerIndex)
  35. {
  36. _scRoot = scRoot;
  37. _readCmd = readcmd;
  38. _endCmd = endcmd;
  39. _portName = SC.GetStringValue($"{_scRoot}.{Name}.PortName");
  40. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  41. _baudRate = SC.GetValue<int>($"{_scRoot}.{Name}.BaudRate");
  42. _dataBits = SC.GetValue<int>($"{_scRoot}.{Name}.DataBits");
  43. _parity = (Parity)Enum.Parse(typeof(Parity), SC.GetStringValue($"{_scRoot}.{Name}.Parity"));
  44. _stopBits = SC.GetValue<int>($"{_scRoot}.{Name}.StopBits");
  45. _port = new AsyncSerialPort(_portName, _baudRate, 8);//, Parity.Even, StopBits.One, "\r", true);
  46. _port.EnableLog = _enableLog;
  47. _port.OnDataChanged += _port_OnDataChanged;
  48. _port.OnErrorHappened += _port_OnErrorHappened;
  49. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 5;
  50. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
  51. if (sleep <= 0 || sleep > 10)
  52. sleep = 2;
  53. int retry = 0;
  54. do
  55. {
  56. if (_port.Open())
  57. {
  58. EV.PostInfoLog(Module, $"Connected with {Module}.{Name} .");
  59. break;
  60. }
  61. else
  62. {
  63. EV.PostInfoLog(Module, $"Can't connected with {Module}.{Name},retry time {retry}.");
  64. _port.Close();
  65. Thread.Sleep(sleep * 1000);
  66. }
  67. if (count > 0 && retry++ > count)
  68. {
  69. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name}.");
  70. break;
  71. }
  72. } while (true);
  73. Reset();
  74. }
  75. private void _port_OnErrorHappened(string obj)
  76. {
  77. OnError();
  78. }
  79. private void _port_OnDataChanged(string package)
  80. {
  81. try
  82. {
  83. lock (_locker)
  84. {
  85. if (DeviceState == CIDReaderStateEnum.ReadCarrierID)
  86. {
  87. CarrierIDBeRead = CarrierIDBeRead + package;
  88. if (!CarrierIDBeRead.Contains("\r")) return;
  89. CarrierIDBeRead = CarrierIDBeRead.Split('\r')[0].Replace("\n", "");
  90. if (string.IsNullOrEmpty(CarrierIDBeRead) || CarrierIDBeRead == "BR" || CarrierIDBeRead.Contains("UNKNOWN_CMD"))
  91. {
  92. CarrierIDBeRead = "";
  93. if (_retryTimes <10)
  94. {
  95. Thread.Sleep(200);
  96. _port.Write(_readCmd);
  97. _retryTimes++;
  98. return;
  99. }
  100. OnCarrierIDReadFailed("Scan Failed.");
  101. return;
  102. }
  103. OnCarrierIDRead(CarrierIDBeRead.Replace("\r","").Replace("\n",""));
  104. }
  105. else
  106. {
  107. CarrierIDBeRead = CarrierIDBeRead + package;
  108. if (!CarrierIDBeRead.Contains("\r")) return;
  109. OnCarrierIDRead(CarrierIDBeRead.Replace("\r", "").Replace("\n", ""));
  110. CarrierIDBeRead = "";
  111. }
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. LOG.Write(ex);
  117. }
  118. }
  119. public string Address { get; set; }
  120. public bool IsConnected => _port.IsOpen();
  121. public bool Connect()
  122. {
  123. return _port.Open();
  124. }
  125. public bool Disconnect()
  126. {
  127. return _port.Close();
  128. }
  129. protected override bool fStartWriteCarrierID(object[] param)
  130. {
  131. return false;
  132. }
  133. protected override bool fStartReadCarrierID(object[] param)
  134. {
  135. try
  136. {
  137. lock (_locker)
  138. {
  139. if (!_port.IsOpen())
  140. {
  141. EV.PostWarningLog(Module, _portName + " not open, can not scan");
  142. return false;
  143. }
  144. CarrierIDBeRead = string.Empty;
  145. _port.Write(_readCmd);
  146. _scanStartTimer = DateTime.Now;
  147. _retryTimes = 0;
  148. return true;
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. LOG.Write(ex);
  154. return false;
  155. }
  156. }
  157. protected override bool fMonitorReadCarrierID(object[] param)
  158. {
  159. if (DateTime.Now - _scanStartTimer > TimeSpan.FromSeconds(90))
  160. {
  161. EV.PostWarningLog(Module, _portName + " error, timeout");
  162. OnCarrierIDReadFailed("Timeout");
  163. if(!string.IsNullOrEmpty(_endCmd))
  164. _port.Write(_endCmd);
  165. return true;
  166. }
  167. return false;
  168. }
  169. }
  170. }