You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.0 KiB
C#

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 MaterialBinding : BaseAction, IAction
{
private RuntimeParameter _runtime;
ControlOffLineScan ControlOffLineScan = new ControlOffLineScan();
BaseEquip _equip;
private static bool _isFirstRun = true;
int StationCode = 5010;
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");
//初始化串口
InitPort();
ControlOffLineScan.RecAutoDataAction += Receive;
}
private void Workbench_ShutDown(object sender, EventArgs e)
{
ControlOffLineScan.Dispose();
}
public void FindPLC(string plcname)
{
try
{
foreach (BaseEquip equip in Factory.Instance.AllEquips.Values)
{
if (equip.Name == plcname)
{
ICSharpCode.Core.LoggingService.Debug("找到设备类型:" + equip.Name);
_equip = equip;
break;
}
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(string.Format(ex.ToString()));
}
}
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()));
}
}
private void InitForm()
{
try
{
var lblReqSerialName = base.GetControlById("MCLabel217") as System.Windows.Forms.Label;//计划数
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(string.Format(ex.ToString()));
}
}
public void Receive(string code)
{
try
{
ProductCode = code;
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
}
catch (Exception ex)
{
ProductCode = "";
base.LogError(ex.ToString());
}
//发送PLC条码
}
/// <summary>
/// 条码写入PLC
/// </summary>
/// <param name="station"></param>
/// <returns></returns>
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;
}
}
}
}