首次提交:添加src文件夹代码
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
using StandardDomeNewApp.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static StandardDomeNewApp.Core.SysEnumInfon;
|
||||
|
||||
namespace StandardDomeNewApp.Communication.SweepCodeGun
|
||||
{
|
||||
public abstract class IScanCode
|
||||
{
|
||||
/// <summary>
|
||||
/// plc的唯一标识
|
||||
/// </summary>
|
||||
public string KeyName { set; get; }
|
||||
/// <summary>
|
||||
/// 缓存
|
||||
/// </summary>
|
||||
protected ContentCache contentCache { set; get; } = ContentCache.CreateObj();
|
||||
/// <summary>
|
||||
/// 是否使用虚拟码 true使用
|
||||
/// </summary>
|
||||
protected bool IsUserVCode { set; get; } = true;
|
||||
/// <summary>
|
||||
/// 这个对象最大额虚拟码
|
||||
/// </summary>
|
||||
protected int MaxVCode { set; get; }
|
||||
/// <summary>
|
||||
/// 这个对象最小的虚拟码
|
||||
/// </summary>
|
||||
protected int MinVCode { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否忙(用在手动触发时判断一下)
|
||||
/// </summary>
|
||||
public bool IsBusy { set; get; }
|
||||
/// <summary>
|
||||
/// 当前的虚拟码
|
||||
/// </summary>
|
||||
public virtual int Vcode { set; get; }
|
||||
/// <summary>
|
||||
/// 当前字符解码方式
|
||||
/// </summary>
|
||||
public Encoding oneEncoding { set; get; } = Encoding.Default;
|
||||
/// <summary>
|
||||
/// 最大试错次数
|
||||
/// </summary>
|
||||
public int MaxReadCount { set; get; } = 3;
|
||||
/// <summary>
|
||||
/// 是否连接
|
||||
/// </summary>
|
||||
public virtual bool IsConnect { protected set; get; }
|
||||
/// <summary>
|
||||
/// 消息显示委托
|
||||
/// </summary>
|
||||
public Action<string, bool, MessageLogType> ShowMessageAction { set; get; }
|
||||
/// <summary>
|
||||
/// 错误代码合集
|
||||
/// </summary>
|
||||
public List<string> ErrorCodes { set; get; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 构建这个对象
|
||||
/// </summary>
|
||||
public abstract void Build(object config);
|
||||
/// <summary>
|
||||
/// 连接扫码枪
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public abstract bool Connect();
|
||||
/// <summary>
|
||||
/// 断开扫码枪
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public abstract bool UnConnect();
|
||||
/// <summary>
|
||||
/// 读取条码
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public abstract Tuple<string, int, bool> ReadCode();
|
||||
/// <summary>
|
||||
/// 重连
|
||||
/// </summary>
|
||||
public abstract void ReConnect();
|
||||
/// <summary>
|
||||
/// 设定是否使用虚拟码
|
||||
/// </summary>
|
||||
/// <param name="state"></param>
|
||||
public virtual void SetIsUserVCode(bool state)
|
||||
{
|
||||
IsUserVCode = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
using StandardDomeNewApp.Communication.Sockets;
|
||||
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;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StandardDomeNewApp.Communication.SweepCodeGun
|
||||
{
|
||||
public class ScanCode_ClientSocket : 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.tbl_SweepCodeGunConfigs.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.tbl_SweepCodeGunConfigs.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 object readopLock { set; get; } = new object();
|
||||
/// <summary>
|
||||
/// 扫码枪配置参数
|
||||
/// </summary>
|
||||
private SweepCodeGunConfigModel configModel { set; get; }
|
||||
/// <summary>
|
||||
/// 网口客户端对象
|
||||
/// </summary>
|
||||
private ProvidOrdinaryClient providOrdinaryClient { set; get; }
|
||||
/// <summary>
|
||||
/// 正则对象
|
||||
/// </summary>
|
||||
private Regex regex { set; get; }
|
||||
/// <summary>
|
||||
/// 一个客户端接收数据也是一个个来的
|
||||
/// </summary>
|
||||
private string dataPack { set; get; }
|
||||
/// <summary>
|
||||
/// 是否允许开始接收了
|
||||
/// </summary>
|
||||
private bool IsAllowReceive { set; get; } = false;
|
||||
/// <summary>
|
||||
/// 建立对象
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
public override void Build(object config)
|
||||
{
|
||||
if (config is SweepCodeGunConfigModel model)
|
||||
{
|
||||
configModel = model;
|
||||
KeyName = configModel.PrimaryKey;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override bool Connect()
|
||||
{
|
||||
IsConnect = false;
|
||||
if (configModel == null)
|
||||
{
|
||||
ShowMessageAction?.Invoke("请先Build后再尝试连接!", false, Core.SysEnumInfon.MessageLogType.Info);
|
||||
return false;
|
||||
}
|
||||
providOrdinaryClient = contentCache.GetContent<ProvidOrdinaryClient>(configModel.ProtocolKey);
|
||||
if (providOrdinaryClient == null)
|
||||
{
|
||||
ShowMessageAction?.Invoke($"无法找到[{configModel.ProtocolKey}]对应的网口客户端对象!", false, Core.SysEnumInfon.MessageLogType.Info);
|
||||
}
|
||||
providOrdinaryClient.IsconnectserverAction = b =>
|
||||
{
|
||||
IsConnect = b;
|
||||
ShowMessageAction?.Invoke($"扫码枪对象[{KeyName}]连接状态:{IsConnect}!", false, Core.SysEnumInfon.MessageLogType.Info);
|
||||
};
|
||||
providOrdinaryClient.GetMessages = RawData_Receive;
|
||||
IsConnect = providOrdinaryClient.IsconnectServer;
|
||||
oneEncoding = providOrdinaryClient.encoding;
|
||||
ShowMessageAction?.Invoke($"扫码枪对象[{KeyName}]连接状态:{IsConnect}!", false, Core.SysEnumInfon.MessageLogType.Info);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 读取条码 (同步完成的)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override Tuple<string, int, bool> ReadCode()
|
||||
{
|
||||
lock (readopLock)
|
||||
{
|
||||
IsBusy = true;
|
||||
|
||||
string datapack = "";
|
||||
int vcode = -1;
|
||||
bool isok = false;
|
||||
for (int i = 0; i < MaxReadCount; i++)
|
||||
{
|
||||
bool flag = false;
|
||||
//发送命令
|
||||
if (!string.IsNullOrEmpty(configModel.Command))
|
||||
{
|
||||
var array = configModel.Command.Split('+');
|
||||
var command = array[0];
|
||||
if (array.Length > 1 && array[1] == "NewLine")
|
||||
{
|
||||
providOrdinaryClient.SendCommand(command + Environment.NewLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
providOrdinaryClient.SendCommand(command);
|
||||
}
|
||||
}
|
||||
|
||||
//开始允许接收
|
||||
IsAllowReceive = true;
|
||||
//记录开始的时间
|
||||
DateTime dateTime = DateTime.Now;
|
||||
while (true)
|
||||
{
|
||||
if ((DateTime.Now - dateTime).TotalMilliseconds > configModel.OutTime)
|
||||
{
|
||||
//超过预定的时间跳出等待
|
||||
break;
|
||||
}
|
||||
if (regex == null)
|
||||
{
|
||||
//直接拿过来
|
||||
if (!string.IsNullOrEmpty(dataPack))
|
||||
{
|
||||
//只要有数据来就进来解析
|
||||
datapack = dataPack;
|
||||
var find = ErrorCodes.Find(o => o == datapack);
|
||||
if (find == null)
|
||||
{
|
||||
isok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = regex.Match(dataPack);
|
||||
if (result.Success)
|
||||
{
|
||||
//去头去尾
|
||||
string str = result.Value;
|
||||
var arrayconnand = configModel.Command.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;
|
||||
isok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isok)
|
||||
{
|
||||
//收到数据了
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
//这个是为了破除最大线程数 防止死循环卡死的
|
||||
Thread.Sleep(5);
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
//如果接收到了数据就跳出尝试循环
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (IsUserVCode)
|
||||
{
|
||||
Vcode++;
|
||||
if (Vcode > MaxVCode)
|
||||
{
|
||||
Vcode = MinVCode;
|
||||
}
|
||||
vcode = Vcode;
|
||||
}
|
||||
//读取完成无论成功与否都结束接收
|
||||
IsAllowReceive = false;
|
||||
//清除缓存
|
||||
dataPack = string.Empty;
|
||||
|
||||
IsBusy = false;
|
||||
//这个时候拿出(返回)数据包
|
||||
return Tuple.Create(datapack, vcode, isok);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReConnect()
|
||||
{
|
||||
providOrdinaryClient?.ReConnect();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override bool UnConnect()
|
||||
{
|
||||
if (providOrdinaryClient == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
providOrdinaryClient.CloseConnect();
|
||||
IsConnect = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 原始数据接收
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="encoding"></param>
|
||||
private void RawData_Receive(byte[] data, Encoding encoding)
|
||||
{
|
||||
if (!IsAllowReceive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
dataPack += encoding.GetString(data);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
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.tbl_SweepCodeGunConfigs.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.tbl_SweepCodeGunConfigs.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;
|
||||
|
||||
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.Command.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
using StandardDomeNewApp.Communication.SerialPorts;
|
||||
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;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StandardDomeNewApp.Communication.SweepCodeGun
|
||||
{
|
||||
public class ScanCode_SeriaPort : 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.tbl_SweepCodeGunConfigs.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.tbl_SweepCodeGunConfigs.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 object readopLock { set; get; } = new object();
|
||||
/// <summary>
|
||||
/// 扫码枪配置参数
|
||||
/// </summary>
|
||||
private SweepCodeGunConfigModel configModel { set; get; }
|
||||
/// <summary>
|
||||
/// 串口对象
|
||||
/// </summary>
|
||||
private SeriaPortController seriaPortController { set; get; }
|
||||
/// <summary>
|
||||
/// 一个串口的数据包 一个串口write有多个线程写都是会按照顺序写完一个包再写一个包的
|
||||
/// </summary>
|
||||
private string dataPack { set; get; }
|
||||
/// <summary>
|
||||
/// 是否允许开始接收了
|
||||
/// </summary>
|
||||
private bool IsAllowReceive { set; get; } = false;
|
||||
/// <summary>
|
||||
/// 正则对象
|
||||
/// </summary>
|
||||
private Regex regex { set; get; }
|
||||
/// <summary>
|
||||
/// 建立对象
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
public override void Build(object config)
|
||||
{
|
||||
if (config is SweepCodeGunConfigModel model)
|
||||
{
|
||||
configModel = model;
|
||||
KeyName = configModel.PrimaryKey;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override bool Connect()
|
||||
{
|
||||
IsConnect = false;
|
||||
if (configModel == null)
|
||||
{
|
||||
ShowMessageAction?.Invoke("请先Build后再尝试连接!", false, Core.SysEnumInfon.MessageLogType.Info);
|
||||
return false;
|
||||
}
|
||||
seriaPortController = contentCache.GetContent<SeriaPortController>(configModel.ProtocolKey);
|
||||
if (seriaPortController == null)
|
||||
{
|
||||
ShowMessageAction?.Invoke($"无法找到[{configModel.ProtocolKey}]对应的串口对象!", false, Core.SysEnumInfon.MessageLogType.Info);
|
||||
return false;
|
||||
}
|
||||
seriaPortController.RawDataAction = RawData_Receive;
|
||||
IsConnect = true;
|
||||
oneEncoding = seriaPortController.encoding;
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 读取条码 (同步完成的)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override Tuple<string, int, bool> ReadCode()
|
||||
{
|
||||
lock (readopLock)
|
||||
{
|
||||
IsBusy = true;
|
||||
|
||||
string datapack = "";
|
||||
int vcode = -1;
|
||||
bool isok = false;
|
||||
for (int i = 0; i < MaxReadCount; i++)
|
||||
{
|
||||
bool flag = false;
|
||||
//发送命令
|
||||
if (!string.IsNullOrEmpty(configModel.Command))
|
||||
{
|
||||
var array = configModel.Command.Split('+');
|
||||
var command = array[0];
|
||||
if (array.Length > 1 && array[1] == "NewLine")
|
||||
{
|
||||
seriaPortController.Send(command + Environment.NewLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
seriaPortController.Send(command);
|
||||
}
|
||||
}
|
||||
//开始允许接收
|
||||
IsAllowReceive = true;
|
||||
//记录开始的时间
|
||||
DateTime dateTime = DateTime.Now;
|
||||
while (true)
|
||||
{
|
||||
if ((DateTime.Now - dateTime).TotalMilliseconds > configModel.OutTime)
|
||||
{
|
||||
//超过预定的时间跳出等待
|
||||
break;
|
||||
}
|
||||
if (regex == null)
|
||||
{
|
||||
//直接拿过来
|
||||
if (!string.IsNullOrEmpty(dataPack))
|
||||
{
|
||||
//只要有数据来就进来解析
|
||||
datapack = dataPack;
|
||||
var find = ErrorCodes.Find(o => o == datapack);
|
||||
if (find == null)
|
||||
{
|
||||
isok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = regex.Match(dataPack);
|
||||
if (result.Success)
|
||||
{
|
||||
//去头去尾
|
||||
string str = result.Value;
|
||||
var arrayconnand = configModel.Command.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;
|
||||
isok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isok)
|
||||
{
|
||||
//收到数据了
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
//这个是为了破除最大线程数 防止死循环卡死的
|
||||
Thread.Sleep(5);
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
//如果接收到了数据就跳出尝试循环
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (IsUserVCode)
|
||||
{
|
||||
Vcode++;
|
||||
if (Vcode > MaxVCode)
|
||||
{
|
||||
Vcode = MinVCode;
|
||||
}
|
||||
vcode = Vcode;
|
||||
}
|
||||
//读取完成无论成功与否都结束接收
|
||||
IsAllowReceive = false;
|
||||
//清除缓存
|
||||
dataPack = string.Empty;
|
||||
|
||||
IsBusy = false;
|
||||
//这个时候拿出(返回)数据包
|
||||
return Tuple.Create(datapack, vcode, isok);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReConnect()
|
||||
{
|
||||
seriaPortController?.ReTryOpen();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override bool UnConnect()
|
||||
{
|
||||
if (seriaPortController == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
seriaPortController.Close();
|
||||
IsConnect = false;
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 原始数据接收
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="encoding"></param>
|
||||
private void RawData_Receive(byte[] data, Encoding encoding)
|
||||
{
|
||||
if (!IsAllowReceive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
dataPack += encoding.GetString(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
using StandardDomeNewApp.Communication.Sockets;
|
||||
using StandardDomeNewApp.Mappering;
|
||||
using StandardDomeNewApp.Model;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StandardDomeNewApp.Communication.SweepCodeGun
|
||||
{
|
||||
public class ScanCode_ServerSocket : 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.tbl_SweepCodeGunConfigs.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.tbl_SweepCodeGunConfigs.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 object readopLock { set; get; } = new object();
|
||||
/// <summary>
|
||||
/// 扫码枪配置参数
|
||||
/// </summary>
|
||||
private SweepCodeGunConfigModel configModel { set; get; }
|
||||
/// <summary>
|
||||
/// 服务对象
|
||||
/// </summary>
|
||||
private SingleSocketServer singleSocketServer { 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;
|
||||
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;
|
||||
}
|
||||
singleSocketServer = contentCache.GetContent<SingleSocketServer>(configModel.ProtocolKey);
|
||||
if (singleSocketServer == null)
|
||||
{
|
||||
ShowMessageAction?.Invoke($"无法找到[{configModel.ProtocolKey}]对应的串口对象!", false, Core.SysEnumInfon.MessageLogType.Info);
|
||||
return false;
|
||||
}
|
||||
if (singleSocketServer.RequestReceivedAction == null)
|
||||
{
|
||||
singleSocketServer.RequestReceivedAction = RequestReceived;
|
||||
}
|
||||
if (singleSocketServer.OnSessionAccept == null)
|
||||
{
|
||||
singleSocketServer.OnSessionAccept = SessionAccept;
|
||||
}
|
||||
if (singleSocketServer.OnSessionClose == null)
|
||||
{
|
||||
singleSocketServer.OnSessionClose = SessionClose;
|
||||
}
|
||||
oneEncoding = singleSocketServer.encoding;
|
||||
IsConnect = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
public override Tuple<string, int, bool> ReadCode()
|
||||
{
|
||||
lock (readopLock)
|
||||
{
|
||||
IsBusy = true;
|
||||
|
||||
string datapack = "";
|
||||
int vcode = -1;
|
||||
bool isok = false;
|
||||
var id = singleSocketServer.GetSessionID(configModel.Reserve);
|
||||
for (int i = 0; i < MaxReadCount; i++)
|
||||
{
|
||||
bool flag = false;
|
||||
//发送命令
|
||||
if (!string.IsNullOrEmpty(configModel.Command))
|
||||
{
|
||||
var array = configModel.Command.Split('+');
|
||||
var command = array[0];
|
||||
if (array.Length > 1 && array[1] == "NewLine")
|
||||
{
|
||||
singleSocketServer.SessionSend(command + Environment.NewLine, id);
|
||||
}
|
||||
else
|
||||
{
|
||||
singleSocketServer.SessionSend(command, id);
|
||||
}
|
||||
}
|
||||
//开始允许接收
|
||||
singleSocketServer.IsAllowReceiveDic[id] = true;
|
||||
//记录开始的时间
|
||||
DateTime dateTime = DateTime.Now;
|
||||
while (true)
|
||||
{
|
||||
if ((DateTime.Now - dateTime).TotalMilliseconds > configModel.OutTime)
|
||||
{
|
||||
//超过预定的时间跳出等待
|
||||
break;
|
||||
}
|
||||
if (regex == null)
|
||||
{
|
||||
//直接拿过来
|
||||
if (!string.IsNullOrEmpty(singleSocketServer.SessionDataDic[id]))
|
||||
{
|
||||
//只要有数据来就进来解析
|
||||
datapack = singleSocketServer.SessionDataDic[id];
|
||||
var find = ErrorCodes.Find(o => o == datapack);
|
||||
if (find == null)
|
||||
{
|
||||
isok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = regex.Match(singleSocketServer.SessionDataDic[id]);
|
||||
if (result.Success)
|
||||
{
|
||||
//去头去尾
|
||||
string str = result.Value;
|
||||
var arrayconnand = configModel.Command.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;
|
||||
isok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isok)
|
||||
{
|
||||
//收到数据了
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
//这个是为了破除最大线程数 防止死循环卡死的
|
||||
Thread.Sleep(5);
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
//如果接收到了数据就跳出尝试循环
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (IsUserVCode)
|
||||
{
|
||||
Vcode++;
|
||||
if (Vcode > MaxVCode)
|
||||
{
|
||||
Vcode = MinVCode;
|
||||
}
|
||||
vcode = Vcode;
|
||||
}
|
||||
//读取完成无论成功与否都结束接收
|
||||
singleSocketServer.IsAllowReceiveDic[id] = false;
|
||||
//清除缓存
|
||||
singleSocketServer.SessionDataDic[id] = string.Empty;
|
||||
|
||||
IsBusy = false;
|
||||
//这个时候拿出(返回)数据包
|
||||
return Tuple.Create(datapack, vcode, isok);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReConnect()
|
||||
{
|
||||
//服务不需要
|
||||
}
|
||||
|
||||
public override bool UnConnect()
|
||||
{
|
||||
if (singleSocketServer == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
singleSocketServer.StopServer();
|
||||
IsConnect = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void RequestReceived(string id, string data)
|
||||
{
|
||||
if (singleSocketServer.IsAllowReceiveDic.ContainsKey(id))
|
||||
{
|
||||
if (!singleSocketServer.IsAllowReceiveDic[id])
|
||||
{
|
||||
//其中一个会话返回
|
||||
return;
|
||||
}
|
||||
if (singleSocketServer.SessionDataDic.ContainsKey(id))
|
||||
{
|
||||
singleSocketServer.SessionDataDic[id] += data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SessionAccept(string id)
|
||||
{
|
||||
singleSocketServer.IsAllowReceiveDic[id] = false;
|
||||
singleSocketServer.SessionDataDic[id] = string.Empty;
|
||||
}
|
||||
|
||||
private void SessionClose(string id)
|
||||
{
|
||||
if (singleSocketServer.SessionDataDic.ContainsKey(id))
|
||||
{
|
||||
singleSocketServer.SessionDataDic.TryRemove(id, out string str);
|
||||
}
|
||||
if (singleSocketServer.IsAllowReceiveDic.ContainsKey(id))
|
||||
{
|
||||
singleSocketServer.IsAllowReceiveDic.TryRemove(id, out bool state);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user