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("A20"); //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 { //if(code.Length == 20 || code.Length== 10) //{ // ProductCode = code; // IsScanProductCodeFlag = true; // if (IsScanStationCodeFlag && IsScanProductCodeFlag) // { // if (StationCode == 5010) // { // base.LogDebug("发送工位1扫码条码:" + ProductCode); // } // else if (StationCode == 5510) // { // base.LogDebug("发送工位2扫码条码:" + ProductCode); // } // //发送条码给PLC // WritePLCBarCode(ProductCode, StationCode); // IsScanStationCodeFlag = false; // IsScanProductCodeFlag = false; // ProductCode = ""; // } //} //判断条码位数 if (code.Trim() == "LeakTestST1") { StationCode = 5010; IsScanStationCodeFlag = true; base.LogDebug("完成扫描工位1工位码!"); } else if (code.Trim() == "LeakTestST2") { StationCode = 5510; IsScanStationCodeFlag = true; base.LogDebug("完成扫描工位2工位码!"); } else { ProductCode = code; IsScanProductCodeFlag = true; base.LogDebug("扫码条码:" + ProductCode); //if (IsScanStationCodeFlag && IsScanProductCodeFlag) //{ // if (StationCode == 5010) // { // base.LogDebug("发送工位1扫码条码:" + ProductCode); // } // else if (StationCode == 5510) // { // base.LogDebug("发送工位2扫码条码:" + ProductCode); // } // //发送条码给PLC // WritePLCBarCode(ProductCode, StationCode); // IsScanStationCodeFlag = false; // IsScanProductCodeFlag = false; // ProductCode = ""; //} } if (IsScanStationCodeFlag && IsScanProductCodeFlag) { if (StationCode == 5010) { base.LogDebug("发送工位1扫码条码:" + ProductCode); } else { base.LogDebug("发送工位2扫码条码:" + ProductCode); } //发送条码给PLC WritePLCBarCode(ProductCode, StationCode); IsScanStationCodeFlag = false; IsScanProductCodeFlag = false; ProductCode = ""; } } catch (Exception ex) { IsScanStationCodeFlag = false; IsScanProductCodeFlag = false; ProductCode = ""; base.LogError(ex.ToString()); } //发送PLC条码 } /// /// 条码写入PLC /// /// /// public bool WritePLCBarCode(string code,int StationCode) { //主线条码第10位 byte[] buffer = new byte[code.Length]; string strCode = ""; buffer = System.Text.ASCIIEncoding.Default.GetBytes(code); for (int i = 0; i < buffer.Length; i++) { string Trim = ","; if (i == buffer.Length - 2) { Trim = ""; } strCode += buffer[i+1].ToString("X") + buffer[i].ToString("X") + Trim; i = i + 1; } string[] message = strCode.Trim().Split(','); object[] bytes = new object[message.Length]; for (int i = 0; i < message.Length; i++) { bytes[i] = Convert.ToInt32(message[i],16); } bool result1 = _equip.Write(StationCode, 10, bytes); ICSharpCode.Core.LoggingService.Debug(StationCode + " 偏移量 " + 10 + "写入结果:" + result1); return true; } } }