首次提交:添加src文件夹代码

This commit is contained in:
2026-02-27 14:02:43 +08:00
commit d330cfbca7
4184 changed files with 5546478 additions and 0 deletions

View File

@@ -0,0 +1,177 @@
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}");
}
}
}
}

View File

@@ -0,0 +1,133 @@
using Cowain.Bake.Common;
using Cowain.Bake.Common.Core;
using Cowain.Bake.Communication.Interface;
using Cowain.Bake.Communication.Models;
using Cowain.Bake.Communication.Sokects;
using HslCommunication;
using Newtonsoft.Json;
using Prism.Services.Dialogs;
using System;
using System.Threading;
using Unity;
namespace Cowain.Bake.Communication.Scan
{
public class Honeywell : IScanCodeBase
{
string DataPack = "";
TcpSocketClient tcpClient = null;
IUnityContainer _unityContainer;
MessageEventWaitHandle<string> messageEvent { get; set; }
public Honeywell(IUnityContainer unityContainer, IDialogService dialogService) : base(unityContainer, dialogService)
{
_unityContainer = unityContainer;
messageEvent = new MessageEventWaitHandle<string>(false, EventResetMode.ManualReset);
}
public override bool Connect()
{
tcpClient = new TcpSocketClient(Ip, Port);
tcpClient.ValueChanged += (sender, e) =>
{
IsConnect = ((TcpSocketClient)sender).IsConnected;
SetStatus(Id, IsConnect);
};
tcpClient.ReceivedString += TcpClient_ReceivedString; ;
tcpClient.ConnectServer();
return true;
}
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} ");
}
}
private void TcpClient_ReceivedString(string obj)
{
messageEvent.Set(obj);
}
public override void Close()
{
tcpClient.ConnectClose();
}
private bool isConnect;
public override bool IsConnect
{
get
{
isConnect = tcpClient.IsConnected;
return isConnect;
}
set
{
isConnect = value;
}
}
public override OperateResult<string> ReadCode()
{
OperateResult plcResult;
ExOperateResult<string> result = new ExOperateResult<string>()
{
IsSuccess = false
};
for (int i = 0; i < Global.MAX_READS; i++)
{
var array = Command.Split('+');
var command = array[0];
if (array.Length > 1 && array[1] == "NewLine")
{
plcResult = tcpClient.SendString(command + Environment.NewLine);
}
else
{
plcResult = tcpClient.SendString(command);
}
result.IsSuccess = plcResult.IsSuccess;
result.Message = plcResult.Message;
result.ErrorCode = plcResult.ErrorCode;
if (!result.IsSuccess)
{
result.Message = "发送数据失";
LogHelper.Instance.Error($"发送数据失败,发送数据:{Command}");
continue;
}
DataPack = messageEvent.GetMessage(1 * Global.SECONDS_TO_MILLISCONDS);//"0\r\n"
if (string.IsNullOrEmpty(DataPack)) //超时才返回为空
{
result.Message = "失败";
result.IsSuccess = false;
result.Content = "操作超时!";
}
result.Content = DataPack?.Replace("\r\n","");
break;
}
return result;
}
}
}

View File

@@ -0,0 +1,167 @@
using Cowain.Bake.Common;
using Cowain.Bake.Common.Core;
using Cowain.Bake.Communication.Interface;
using Cowain.Bake.Communication.Models;
using HslCommunication;
using Keyence.AutoID.SDK;
using Newtonsoft.Json;
using Prism.Services.Dialogs;
using System;
using System.Text.RegularExpressions;
using Unity;
namespace Cowain.Bake.Communication.Scan
{
public class Keyence : IScanCodeBase
{
IUnityContainer _unityContainer;
public Keyence(IUnityContainer unityContainer, IDialogService dialogService) : base(unityContainer, dialogService)
{
_unityContainer = unityContainer;
}
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}");
}
}
private bool? isConnect = null;
//private bool? lastConnect = null;
//public event EventHandler<ValueChangedEventArgs> ValueChanged;
public override bool IsConnect
{
get
{
bool connectStatus = false;
if (null == reader)
{
isConnect = false;
}
else
{
string vervion =reader.ExecCommand("KEYENCE"); //OK,KEYENCE,SR-1000,1.42,4.604
if (vervion.Contains("OK"))
{
connectStatus = true;
}
else
{
LogHelper.Instance.GetCurrentClassError($"扫码枪连接失败,{this.Name},错误码:{reader.LastErrorInfo},返回版本:{vervion}");
}
//(int)reader.LastErrorInfo=0拔网络是0//((0 == (int)reader.LastErrorInfo || 5 == (int)reader.LastErrorInfo) ? true : false);
if (connectStatus != isConnect)
{
isConnect = connectStatus;
SetStatus(Id, connectStatus);
}
}
return connectStatus;
}
set
{
isConnect = value;
SetStatus(Id, value);
}
}
//protected virtual void OnValueChanged(bool newValue)
//{
// SetStatus(Id, lastConnect.Value);
// //ValueChanged?.Invoke(this, new ValueChangedEventArgs { NewValue = newValue });
//}
/// <summary>
/// 基恩士扫码枪的读取器
/// </summary>
private ReaderAccessor reader { set; get; }
public override bool Connect()
{
Close();
reader = new ReaderAccessor(Ip);
reader.CommandPort = Port;
reader.DataPort = Port;
IsConnect = reader.Connect();
return IsConnect;
}
public override OperateResult<string> ReadCode()
{
string barcode = "";
ExOperateResult<string> result = new ExOperateResult<string>()
{
IsSuccess = false,
};
for (int i = 0; i < Global.SCANCODE_COUNT; i++)
{
barcode = reader.ExecCommand(Command, Global.MAX_TCP_READ_OUTTIME);
if (!string.IsNullOrEmpty(FilterRegex))
{
barcode = Regex.Replace(barcode, FilterRegex, "");
}
if (!barcode.Contains("ERROR")
&& !barcode.Contains("KEYENCE")
&& !string.IsNullOrEmpty(barcode))
{
result.Content = barcode;
result.IsSuccess = true;
break;
}
else
{
reader.ExecCommand(CloseCommand);
}
}
return result;
}
public override void Close()
{
try
{
if (reader != null)
{
reader.Disconnect();
reader.Dispose();
reader = null;
}
}
catch (Exception ex)
{
LogHelper.Instance.GetCurrentClassError("扫码枪关闭失败:" + ex.Message);
}
finally
{
reader = null;
}
}
~Keyence()
{
Close();
}
}
}

View File

@@ -0,0 +1,69 @@
using Cowain.Bake.BLL;
using Cowain.Bake.Common;
using Cowain.Bake.Common.Core;
using Cowain.Bake.Common.Enums;
using Cowain.Bake.Communication.Interface;
using Cowain.Bake.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Unity;
namespace Cowain.Bake.Communication.Scan
{
public class ScanCodeManage
{
private List<TDeviceConfig> scanCodeList;
IUnityContainer _unityContainer;
public ScanCodeManage(IUnityContainer unityContainer)
{
_unityContainer = unityContainer;
scanCodeList = unityContainer.Resolve<DeviceConfigService>().GetConfig(EDeviceType.SCANNER);
List<Task> listTask = new List<Task>();
foreach (var item in scanCodeList)
{
Assembly asm = Assembly.GetExecutingAssembly(); //获得当前程序的绝对路径
Type type = asm.GetType(MyPath.SCAN + item.DriverName);
unityContainer.RegisterSingleton(typeof(IScanCodeBase), type, item.Name);
var scanCode = _unityContainer.Resolve<IScanCodeBase>(item.Name); //几个PLC就实例化几个
scanCode.Id = item.Id;
scanCode.Name = item.Name;
scanCode.DeviceName = item.DriverName;
scanCode.GetJsonParam(item.Json);
if (item.Enable)
{
listTask.Add(Task.Run(() =>
{
scanCode.Connect();
}));
}
}
Task.WaitAll(listTask.ToArray());
Task.Run(async () =>
{
while(true)
{
await Task.Delay(5000);
CheckConnect();
}
});
}
void CheckConnect()
{
var scanCodes = _unityContainer.ResolveAll<IScanCodeBase>();
foreach(var item in scanCodes)
{
bool b = item.IsConnect;
}
}
}
}