首次提交:添加src文件夹代码
This commit is contained in:
167
Cowain.Bake.Communication/Scan/Keyence.cs
Normal file
167
Cowain.Bake.Communication/Scan/Keyence.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user