using AUCMA_Air.Business; using Mesnac.Action.Base; using Mesnac.Compressor.Entity; using Mesnac.Compressor.Station; using Mesnac.Equips; using Mesnac.Equips.BaseInfo; using Mesnac.Gui.Workbench; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Mesnac.Action.Compressor { public class LeakageScanBarCode : BaseAction, IAction { private RuntimeParameter _runtime; ControlOffLineScan ControlOffLineScan = new ControlOffLineScan(); BaseEquip _equip; private static bool _isFirstRun = true; int StationCode = 5010; private bool IsScanStationCodeFlag = false; private bool IsScanProductCodeFlag = false; string ProductCode = ""; public void Run(RuntimeParameter runtime) { base.RunIni(runtime); //必须调用 this._runtime = runtime; if (_isFirstRun == true) { //开启串口 //textBox.Text = "***"; WorkbenchSingleton.Workbench.ShutDown += new EventHandler(Workbench_ShutDown); _isFirstRun = false; } //查询PLC FindPLC("A23"); //WritePLCData(); //初始化串口 InitPort(); ControlOffLineScan.RecAutoDataAction += Receive; } private void Workbench_ShutDown(object sender, EventArgs e) { ControlOffLineScan.Dispose(); } public void FindPLC(string plcname) { foreach (BaseEquip equip in Factory.Instance.AllEquips.Values) { if (equip.Name == plcname) { ICSharpCode.Core.LoggingService.Debug("找到设备类型:" + equip.Name); _equip = equip; break; } } } public void InitPort() { try { ControlOffLineScan.SystemInitialization(); Task task = new Task(() => { // 打开串口 if (ControlOffLineScan.Open()) ICSharpCode.Core.LoggingService.Debug("条码枪连接成功!"); }); task.Start(); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(string.Format(ex.ToString())); } } public void Receive(string code) { try { ProductCode = code; IsScanProductCodeFlag = true; base.LogDebug("扫码条码:" + ProductCode); #region 20240728修改兼容20位以下条码 //20240728修改兼容20位以下条码 if (ProductCode.Length < 20) { ProductCode = ProductCode.ToString().PadRight(20, ' '); } else { ProductCode = ProductCode.Substring(0, 20); } //发送条码给PLC //20240728修改兼容20位以下条码 int stationcode = int.Parse(_equip.Group["B1"].Block); WriteString(ProductCode, stationcode); #endregion //if (IsScanStationCodeFlag && IsScanProductCodeFlag) //{ // if (StationCode == 5010) // { // base.LogDebug("发送工位1扫码条码:" + ProductCode); // } // else // { // base.LogDebug("发送工位2扫码条码:" + ProductCode); // } // IsScanStationCodeFlag = false; // IsScanProductCodeFlag = false; // ProductCode = ""; //} } catch (Exception ex) { IsScanStationCodeFlag = false; IsScanProductCodeFlag = false; ProductCode = ""; base.LogError(ex.ToString()); } //发送PLC条码 } /// /// 条码写入PLC /// /// /// public bool WriteString(string code, int StationCode) { try { byte[] tempByte = System.Text.Encoding.ASCII.GetBytes(code); object[] temp = new object[tempByte.Length / 2]; for (int i = 0; i < temp.Length; i++) { temp[i] = BitConverter.ToInt16(tempByte, 2 * i); } bool result1 = _equip.Write(StationCode, 10, temp); ICSharpCode.Core.LoggingService.Debug(StationCode + " 偏移量 " + 10 + "写入结果:" + result1); return true; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(string.Format(ex.ToString())); return false; } } } }