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 { /// /// 是否连接 /// 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(); } } } } } } /// /// 虚拟码 /// 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(); } } } } } } /// /// 扫码枪配置参数 /// private SweepCodeGunConfigModel configModel { set; get; } /// /// 基恩士扫码枪的读取器 /// private ReaderAccessor reader { set; get; } /// /// 正则对象 /// 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(configModel.ProtocolKey); if (keyence != null) { reader.CommandPort = reader.DataPort = keyence.Port; } oneEncoding = Encoding.ASCII;//sdk里写死的 return IsConnect = reader.Connect(); } public override Tuple 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; } } } }