|
|
|
@ -32,10 +32,15 @@ namespace SlnMesnac.TouchSocket
|
|
|
|
|
public delegate void RefreshMaterialCodeStr(string materialCodeStr, string ip);
|
|
|
|
|
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
|
|
|
|
|
|
|
|
|
|
//相机拍照识别结果事件
|
|
|
|
|
//相机拍照识别结果
|
|
|
|
|
public delegate void CameraResult(string result);
|
|
|
|
|
public static event CameraResult CameraResultEvent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//光电信号推送
|
|
|
|
|
public delegate void SerialSignalPush(string ip, int flag);
|
|
|
|
|
public static event SerialSignalPush SerialSignalPushEvent;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public TcpServer(ILogger<TcpServer> logger,TcpService tcpService)
|
|
|
|
@ -67,42 +72,66 @@ namespace SlnMesnac.TouchSocket
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//从客户端收到信息
|
|
|
|
|
var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len
|
|
|
|
|
|
|
|
|
|
//心跳包
|
|
|
|
|
if (mes == "heartbeat")
|
|
|
|
|
{
|
|
|
|
|
//扫码器心跳连接
|
|
|
|
|
client.Logger.Info($"心跳{client.IP}:{client.Port}》接收到心跳信息:{mes}");
|
|
|
|
|
RefreshStateEvent?.Invoke(client.IP, true);
|
|
|
|
|
}
|
|
|
|
|
else if (mes == "NoRead")
|
|
|
|
|
|
|
|
|
|
// 串口模块光电信号
|
|
|
|
|
if (client.IP == config.SerialIP)
|
|
|
|
|
{
|
|
|
|
|
client.Logger.Info($"客户端{client.IP}:{client.Port}》NoRead事件{mes}");
|
|
|
|
|
// TODO
|
|
|
|
|
int result = ChangeSerialSignal(e.ByteBlock.Buffer,e.ByteBlock.Len);
|
|
|
|
|
if(result == -1)
|
|
|
|
|
{
|
|
|
|
|
client.Logger.Info($"{client.IP}:{client.Port}》接收到光电信息异常");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string message = result == 1 ? "触发" : "释放";
|
|
|
|
|
client.Logger.Info($"{client.IP}:{client.Port}》接收到光电信息:{message}");
|
|
|
|
|
SerialSignalPushEvent?.Invoke(client.IP, result);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
else //扫码器及相机
|
|
|
|
|
{
|
|
|
|
|
string code = mes.Trim().TrimEnd('\0');
|
|
|
|
|
client.Logger.Info($"客户端{client.IP}:{client.Port}》接收到信息:{mes}");
|
|
|
|
|
// 区分相机扫码器数据
|
|
|
|
|
if (client.IP == config.ScannerIP)
|
|
|
|
|
//从客户端收到信息
|
|
|
|
|
var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len
|
|
|
|
|
|
|
|
|
|
//心跳包
|
|
|
|
|
if (mes == "heartbeat")
|
|
|
|
|
{
|
|
|
|
|
if (code.Contains("heart"))
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
RefreshMaterialCodeStrEvent?.Invoke(code, client.IP);
|
|
|
|
|
//扫码器心跳连接
|
|
|
|
|
client.Logger.Info($"心跳{client.IP}:{client.Port}》接收到心跳信息:{mes}");
|
|
|
|
|
RefreshStateEvent?.Invoke(client.IP, true);
|
|
|
|
|
}
|
|
|
|
|
else if (client.IP == config.CameraIP)
|
|
|
|
|
else if (mes == "NoRead")
|
|
|
|
|
{
|
|
|
|
|
// 相机返回数据
|
|
|
|
|
CameraResultEvent?.Invoke(code);
|
|
|
|
|
client.Logger.Info($"客户端{client.IP}:{client.Port}》NoRead事件{mes}");
|
|
|
|
|
// TODO扫码器扫码失败报警
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string code = mes.Trim().TrimEnd('\0');
|
|
|
|
|
client.Logger.Info($"客户端{client.IP}:{client.Port}》接收到信息:{mes}");
|
|
|
|
|
// 区分相机扫码器数据
|
|
|
|
|
if (client.IP == config.ScannerIP)
|
|
|
|
|
{
|
|
|
|
|
if (code.Contains("heart"))
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
RefreshMaterialCodeStrEvent?.Invoke(code, client.IP);
|
|
|
|
|
}
|
|
|
|
|
else if (client.IP == config.CameraIP)
|
|
|
|
|
{
|
|
|
|
|
// 相机返回数据
|
|
|
|
|
CameraResultEvent?.Invoke(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
@ -158,5 +187,39 @@ namespace SlnMesnac.TouchSocket
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 转换光电信号
|
|
|
|
|
/// 释放 01 03 02 01 00 B9 D4
|
|
|
|
|
/// 触发 01 03 02 01 FF F9 94
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int ChangeSerialSignal(byte[] data,int len)
|
|
|
|
|
{
|
|
|
|
|
if (data == null || len < 0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
len = Math.Min(len, data.Length);
|
|
|
|
|
byte[] CheckByte = new byte[len];
|
|
|
|
|
Array.Copy(data, 0, CheckByte, 0, len);
|
|
|
|
|
|
|
|
|
|
byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0xB9, (byte)0xD4 };
|
|
|
|
|
byte[] trigger = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 };
|
|
|
|
|
|
|
|
|
|
if (CheckByte.Length == release.Length && CheckByte.SequenceEqual(release))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if (CheckByte.Length == trigger.Length && CheckByte.SequenceEqual(trigger))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 异常
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|