246 lines
8.2 KiB
C#
246 lines
8.2 KiB
C#
using Keyence.AutoID.SDK;
|
||
using StandardDomeNewApp.Mappering;
|
||
using StandardDomeNewApp.Model;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace StandardDomeNewApp.Communication.SweepCodeGun
|
||
{
|
||
public class ScanCode_Keyence : IScanCode
|
||
{
|
||
/// <summary>
|
||
/// 是否连接
|
||
/// </summary>
|
||
public override bool IsConnect
|
||
{
|
||
get => base.IsConnect;
|
||
protected set
|
||
{
|
||
if (base.IsConnect != value)
|
||
{
|
||
base.IsConnect = value;
|
||
if (configModel == null)
|
||
{
|
||
return;
|
||
}
|
||
using (var dbContext = new DataBaseMappering())
|
||
{
|
||
var find = dbContext.TSweepCodeGunConfigs.Where(o => o.PrimaryKey == configModel.PrimaryKey);
|
||
if (find != null)
|
||
{
|
||
var list = find.ToList();
|
||
if (list.Count > 0)
|
||
{
|
||
var one = list.ElementAt(0);
|
||
//修改这个one
|
||
one.IsConnect = value ? 1 : 0;
|
||
dbContext.SaveChanges();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 虚拟码
|
||
/// </summary>
|
||
public override int Vcode
|
||
{
|
||
get => base.Vcode;
|
||
set
|
||
{
|
||
if (base.Vcode != value)
|
||
{
|
||
base.Vcode = value;
|
||
if (configModel == null)
|
||
{
|
||
return;
|
||
}
|
||
using (var dbContext = new DataBaseMappering())
|
||
{
|
||
var find = dbContext.TSweepCodeGunConfigs.Where(o => o.PrimaryKey == configModel.PrimaryKey);
|
||
if (find != null)
|
||
{
|
||
var list = find.ToList();
|
||
if (list.Count > 0)
|
||
{
|
||
var one = list.ElementAt(0);
|
||
//修改这个one
|
||
one.VCode = value;
|
||
dbContext.SaveChanges();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 扫码枪配置参数
|
||
/// </summary>
|
||
private SweepCodeGunConfigModel configModel { set; get; }
|
||
/// <summary>
|
||
/// 基恩士扫码枪的读取器
|
||
/// </summary>
|
||
private ReaderAccessor reader { set; get; }
|
||
/// <summary>
|
||
/// 正则对象
|
||
/// </summary>
|
||
private Regex regex { set; get; }
|
||
public override void Build(object config)
|
||
{
|
||
if (config is SweepCodeGunConfigModel model)
|
||
{
|
||
configModel = model;
|
||
KeyName = configModel.PrimaryKey;
|
||
reader = new ReaderAccessor();
|
||
|
||
if (configModel.MaxVCode == -1 || configModel.MinVCode == -1)
|
||
{
|
||
IsUserVCode = false;
|
||
}
|
||
MaxVCode = configModel.MaxVCode;
|
||
MinVCode = configModel.MinVCode;
|
||
Vcode = configModel.VCode;
|
||
if (configModel.ErrorCode != null)
|
||
{
|
||
var errorcodes = configModel.ErrorCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||
foreach (var item in errorcodes)
|
||
{
|
||
ErrorCodes.Add(item);
|
||
}
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(configModel.CodeRegex))
|
||
{
|
||
regex = new Regex(configModel.CodeRegex);
|
||
}
|
||
}
|
||
}
|
||
|
||
public override bool Connect()
|
||
{
|
||
IsConnect = false;
|
||
if (configModel == null)
|
||
{
|
||
ShowMessageAction?.Invoke("请先Build后再尝试连接!", false, Core.SysEnumInfon.MessageLogType.Info);
|
||
return false;
|
||
}
|
||
KeyenceConnectModel keyence = contentCache.GetContent<KeyenceConnectModel>(configModel.ProtocolKey);
|
||
if (keyence != null)
|
||
{
|
||
reader.CommandPort = reader.DataPort = keyence.Port;
|
||
}
|
||
oneEncoding = Encoding.ASCII;//sdk里写死的
|
||
return IsConnect = reader.Connect();
|
||
}
|
||
|
||
public override Tuple<string, int, bool> ReadCode()
|
||
{
|
||
IsBusy = true;
|
||
string datapack = "";
|
||
int vcode = -1;
|
||
bool isok = false;
|
||
if (IsSimulation)
|
||
{
|
||
//模拟
|
||
isok = true;
|
||
datapack = Guid.NewGuid().ToString();
|
||
if (IsUserVCode)
|
||
{
|
||
Vcode++;
|
||
if (Vcode > MaxVCode)
|
||
{
|
||
Vcode = MinVCode;
|
||
}
|
||
vcode = Vcode;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
for (int i = 0; i < MaxReadCount; i++)
|
||
{
|
||
var cmd = configModel.Command.Split('+')[0];
|
||
var code = reader.ExecCommand(cmd, configModel.OutTime);
|
||
if (regex == null)
|
||
{
|
||
var find = ErrorCodes.Find(o => o == code);
|
||
if (find == null)
|
||
{
|
||
datapack = code;
|
||
if (IsUserVCode)
|
||
{
|
||
Vcode++;
|
||
if (Vcode > MaxVCode)
|
||
{
|
||
Vcode = MinVCode;
|
||
}
|
||
vcode = Vcode;
|
||
}
|
||
isok = true;
|
||
break;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
var result = regex.Match(code);
|
||
if (result.Success)
|
||
{
|
||
//去头去尾
|
||
string str = result.Value;
|
||
var arrayconnand = configModel.CodeRegex.Split(new string[] { ".*" }, StringSplitOptions.RemoveEmptyEntries);
|
||
foreach (var item in arrayconnand)
|
||
{
|
||
str = str.Replace(item, "");
|
||
}
|
||
var find = ErrorCodes.Find(o => o == str);
|
||
if (find == null)
|
||
{
|
||
datapack = str;
|
||
if (IsUserVCode)
|
||
{
|
||
Vcode++;
|
||
if (Vcode > MaxVCode)
|
||
{
|
||
Vcode = MinVCode;
|
||
}
|
||
vcode = Vcode;
|
||
}
|
||
isok = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
IsBusy = false;
|
||
return Tuple.Create(datapack, vcode, isok);
|
||
}
|
||
|
||
public override void ReConnect()
|
||
{
|
||
if (!IsConnect)
|
||
{
|
||
reader.Connect();
|
||
}
|
||
}
|
||
|
||
public override bool UnConnect()
|
||
{
|
||
try
|
||
{
|
||
reader.Disconnect();
|
||
reader.Dispose();
|
||
return true;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|