178 lines
5.6 KiB
C#
178 lines
5.6 KiB
C#
using Cognex.DataMan.SDK;
|
|
using Cognex.DataMan.SDK.Utils;
|
|
using Cowain.Bake.Common;
|
|
using Cowain.Bake.Common.Core;
|
|
using Cowain.Bake.Communication.Interface;
|
|
using HslCommunication;
|
|
using Newtonsoft.Json;
|
|
using Prism.Services.Dialogs;
|
|
using System;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using Unity;
|
|
|
|
namespace Cowain.Bake.Communication.Scan
|
|
{
|
|
public class Cognex : IScanCodeBase
|
|
{
|
|
IUnityContainer _unityContainer;
|
|
private bool? isConnect = null;
|
|
private DataManSystem _system = null;
|
|
string DecodeStr = "";
|
|
ManualResetEvent resetEvent = new ManualResetEvent(false);
|
|
public override bool IsConnect
|
|
{
|
|
get
|
|
{
|
|
return isConnect??false;
|
|
}
|
|
set
|
|
{
|
|
isConnect = value;
|
|
}
|
|
}
|
|
|
|
public Cognex(IUnityContainer unityContainer, IDialogService dialogService) : base(unityContainer, dialogService)
|
|
{
|
|
_unityContainer = unityContainer;
|
|
}
|
|
private void MySystem_ImageArrived(object sender, ImageArrivedEventArgs args)
|
|
{
|
|
}
|
|
|
|
private void MySystem_ReadStringArrived(object sender, ReadStringArrivedEventArgs args)
|
|
{
|
|
//AcqCount += 1;
|
|
DecodeStr = args.ReadString;
|
|
resetEvent.Set(); // 发送信号,通知主线程接收到数据
|
|
//OutputLog("收到结果");
|
|
}
|
|
|
|
string ExecCommand(string cmd)
|
|
{
|
|
if (!IsConnect)
|
|
{
|
|
return "";
|
|
}
|
|
DecodeStr = "";
|
|
LogHelper.Instance.Info($"开始扫码:{Name}");
|
|
resetEvent.Reset();
|
|
_system?.SendCommand(cmd);
|
|
|
|
// 等待扫码枪回调返回数据
|
|
resetEvent.WaitOne(3000); // 阻塞等待,直到回调触发 .WaitOne(timeoutMs);
|
|
//_system.SendCommand("TRIGGER OFF");// 无论是否成功,都停止触发
|
|
LogHelper.Instance.Info($"结束扫码:{Name},{DecodeStr}");
|
|
return DecodeStr;
|
|
}
|
|
private void MySystem_SystemDisconnected(object sender, System.EventArgs args)
|
|
{
|
|
isConnect = false;
|
|
resetEvent.Set(); // 发送信号,通知主线程接收到数据
|
|
//OutputLog("断开连接");
|
|
}
|
|
|
|
private void MySystem_SystemConnected(object sender, System.EventArgs args)
|
|
{
|
|
isConnect = true;
|
|
}
|
|
private void MySystem_ImageGraphicsArrived(object sender, ImageGraphicsArrivedEventArgs args)
|
|
{
|
|
}
|
|
public void RegisterSystemEvent()
|
|
{
|
|
_system.SystemConnected += MySystem_SystemConnected;
|
|
_system.SystemDisconnected += MySystem_SystemDisconnected;
|
|
_system.ReadStringArrived += MySystem_ReadStringArrived;
|
|
_system.ImageArrived += MySystem_ImageArrived;
|
|
_system.ImageGraphicsArrived += MySystem_ImageGraphicsArrived;
|
|
}
|
|
public override bool Connect()
|
|
{
|
|
if (IsConnect)
|
|
{
|
|
_system?.Disconnect();
|
|
}
|
|
|
|
|
|
EthSystemConnector conn = new EthSystemConnector(IPAddress.Parse(Ip), Port);
|
|
conn.UserName = "admin";
|
|
conn.Password = "";
|
|
_system = new DataManSystem(conn);
|
|
_system.DefaultTimeout = 500;
|
|
|
|
//注册事件
|
|
RegisterSystemEvent();
|
|
_system.SetResultTypes(ResultTypes.ReadString);
|
|
_system.Connect();
|
|
// 设置扫码枪模式(同步)
|
|
|
|
if (_system.Connector.State != ConnectionState.Connected)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public override OperateResult<string> ReadCode()
|
|
{
|
|
string barcode = "";
|
|
OperateResult<string> result = new OperateResult<string>()
|
|
{
|
|
IsSuccess = false,
|
|
};
|
|
|
|
for (int i = 0; i < Global.SCANCODE_COUNT; i++)
|
|
{
|
|
barcode = ExecCommand(Command);
|
|
if (!string.IsNullOrEmpty(FilterRegex))
|
|
{
|
|
barcode = Regex.Replace(barcode, FilterRegex, "");
|
|
}
|
|
|
|
if (!barcode.Contains("ERROR")
|
|
&& !string.IsNullOrEmpty(barcode))
|
|
{
|
|
result.Content = barcode;
|
|
result.IsSuccess = true;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
//ExecCommand(CloseCommand);
|
|
_system?.SendCommand(CloseCommand);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
public override void Close()
|
|
{
|
|
_system?.Disconnect();
|
|
}
|
|
public override void GetJsonParam(string param)
|
|
{
|
|
try
|
|
{
|
|
dynamic d = JsonConvert.DeserializeObject<dynamic>(param);
|
|
Ip = d.Ip;
|
|
Port = d.Port;
|
|
Command = d.Command;
|
|
FilterRegex = d.FilterCodes; //去掉返回不要的码
|
|
CloseCommand = d.CloseCommand;
|
|
|
|
if (string.IsNullOrEmpty(Ip)
|
|
|| string.IsNullOrEmpty(Command))
|
|
{
|
|
LogHelper.Instance.GetCurrentClassError("扫码枪解析出错:" + param);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Instance.GetCurrentClassError($"扫码枪解析出错:{param},{ex}");
|
|
}
|
|
}
|
|
}
|
|
}
|