|
|
using Dm.filter.log;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using Serilog;
|
|
|
using SlnMesnac.Common;
|
|
|
using SlnMesnac.Model.dto;
|
|
|
using SlnMesnac.Rfid.Dto;
|
|
|
using SlnMesnac.Rfid.Enum;
|
|
|
using SlnMesnac.Rfid.NewRFIDConnect;
|
|
|
using SlnMesnac.Rfid.NewRFIDConnect.entity;
|
|
|
using SlnMesnac.Serilog;
|
|
|
using SqlSugar;
|
|
|
using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Drawing;
|
|
|
using System.Linq;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using TouchSocket.Core;
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
#region << 版 本 注 释 >>
|
|
|
/*--------------------------------------------------------------------
|
|
|
* 版权所有 (c) 2024 WenJY 保留所有权利。
|
|
|
* CLR版本:4.0.30319.42000
|
|
|
* 机器名称:LAPTOP-E0N2L34V
|
|
|
* 命名空间:SlnMesnac.Rfid.Factory
|
|
|
* 唯一标识:496f8d2b-70e3-4a05-ae18-a9b0fcd06b82
|
|
|
*
|
|
|
* 创建者:WenJY
|
|
|
* 电子邮箱:wenjy@mesnac.com
|
|
|
* 创建时间:2024-03-27 21:58:35
|
|
|
* 版本:V1.0.0
|
|
|
* 描述:
|
|
|
*
|
|
|
*--------------------------------------------------------------------
|
|
|
* 修改人:
|
|
|
* 时间:
|
|
|
* 修改说明:
|
|
|
*
|
|
|
* 版本:V1.0.0
|
|
|
*--------------------------------------------------------------------*/
|
|
|
#endregion << 版 本 注 释 >>
|
|
|
namespace SlnMesnac.Rfid.Factory
|
|
|
{
|
|
|
public class RflyFactory : RfidAbsractFactory
|
|
|
{
|
|
|
private SerilogHelper _logger;
|
|
|
private readonly TcpClient _tcpClient = new TcpClient();
|
|
|
private readonly StringChange _stringChange;
|
|
|
private string m_strIP;
|
|
|
private string m_deviceID;
|
|
|
|
|
|
public RflyFactory(SerilogHelper logger, StringChange stringChange)
|
|
|
{
|
|
|
_logger = logger;
|
|
|
_stringChange = stringChange;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 建立连接
|
|
|
/// </summary>
|
|
|
/// <param name="ip"></param>
|
|
|
/// <param name="port"></param>
|
|
|
/// <returns></returns>
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
|
public override bool Connect(string ip, int port)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
_tcpClient.SetupAsync(new TouchSocketConfig().SetRemoteIPHost($"{ip}:{port}"));
|
|
|
_tcpClient.ConnectAsync();
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return false;
|
|
|
//throw new InvalidOperationException($"设备连接异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 异步建立连接
|
|
|
/// </summary>
|
|
|
/// <param name="ip"></param>
|
|
|
/// <param name="port"></param>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<bool> ConnectAsync(string ip, int port, string deviceid)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
m_deviceID = deviceid;
|
|
|
m_strIP = ip;
|
|
|
_tcpClient.SetupAsync(new TouchSocketConfig().SetRemoteIPHost($"{ip}:{port}"));
|
|
|
await _tcpClient.ConnectAsync(1500);
|
|
|
_tcpClient.Received = (client, e) =>
|
|
|
{
|
|
|
//从客户端收到信息
|
|
|
//var mes = Encoding.UTF8.GetString(e.Memory.Span.ToArray(), 0, e.Memory.Span.Length);//注意:数据长度是byteBlock.Len
|
|
|
|
|
|
byte[] receivedBuffer = new byte[e.Memory.Span.Length];
|
|
|
Array.Copy(e.Memory.Span.ToArray(), 0, receivedBuffer, 0, e.Memory.Span.Length);
|
|
|
if (receivedBuffer.Length > 3)
|
|
|
{
|
|
|
//心跳
|
|
|
if (e.Memory.Span.ToArray()[3] == 0xBF)
|
|
|
{
|
|
|
if (GloalVar.HeartBeatRecoard.TryGetValue(deviceid, out var value))
|
|
|
{
|
|
|
GloalVar.HeartBeatRecoard[deviceid] = DateTime.Now;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
GloalVar.HeartBeatRecoard.Add(deviceid, DateTime.Now);
|
|
|
}
|
|
|
}
|
|
|
//连续盘点返回
|
|
|
if (e.Memory.Span.ToArray()[3] == 0x01)
|
|
|
{
|
|
|
DealMessageData(receivedBuffer);
|
|
|
}
|
|
|
//按时间段盘点返回
|
|
|
if (e.Memory.Span.ToArray()[3] == 0x02)
|
|
|
{
|
|
|
//粘包处理
|
|
|
//BBDD01900029B80D BBDD1202000132CD043000333131303432360000000000E90D
|
|
|
//BBDD1202000138CF043000333131303035360000000000E20DBBDD01900026B70D
|
|
|
if (e.Memory.Span.ToArray()[e.Memory.Span.ToArray().Length - 5] == 0x90)
|
|
|
{
|
|
|
var messagebuffer = new byte[receivedBuffer.Length - 8];
|
|
|
Array.Copy(receivedBuffer, 0, messagebuffer, 0, receivedBuffer.Length - 8);
|
|
|
DealMessageData(messagebuffer);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DealMessageData(receivedBuffer);
|
|
|
}
|
|
|
//Log.Information($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}");
|
|
|
//byte[] resultBuffer = PareReceiveBufferData(receivedBuffer, receivedBuffer.Length);
|
|
|
//List<TagInfo> tagInfoList = Device_DealTagInfoList(resultBuffer);
|
|
|
//string info = "";
|
|
|
//if (tagInfoList != null && tagInfoList.Count > 0)
|
|
|
//{
|
|
|
// tagInfoList = tagInfoList.Where((x, i) => tagInfoList.FindIndex(z => z.EPCstring == x.EPCstring) == i).ToList();
|
|
|
// info = string.Join(",", tagInfoList.Select(item => item.EPCstring));
|
|
|
//}
|
|
|
//else
|
|
|
//{
|
|
|
// info = "Nodata";
|
|
|
//}
|
|
|
//_Action?.Invoke(m_deviceID, tagInfoList);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
//Log.Information($"{m_deviceID},IP:{m_strIP}接收原始报文{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
};
|
|
|
Log.Information($"{m_deviceID},IP:{m_strIP}连接成功");
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Log.Information($"{m_deviceID},IP:{m_strIP}数据接收异常" + e.Message);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 真心跳设置
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<bool> Set_HeartBeat(byte second)
|
|
|
{
|
|
|
byte[] data = new byte[3];
|
|
|
data[0] = 0x00;
|
|
|
data[1] = second;
|
|
|
data[2] = 0x01;
|
|
|
|
|
|
BaseSendDataEntity entity = new BaseSendDataEntity()
|
|
|
{
|
|
|
Code = 0xBF,
|
|
|
Data = data
|
|
|
};
|
|
|
|
|
|
byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity);
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(result, 2000))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
|
|
|
byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length);
|
|
|
Log.Information($"{m_deviceID}接收读取指令{_stringChange.bytesToHexStr(resultBuffer, resultBuffer.Length)}");
|
|
|
|
|
|
if (resultBuffer[3] == 0xBF || resultBuffer[4] == 0x00)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 异步按时间段盘点
|
|
|
/// </summary>
|
|
|
/// <param name="timeout"></param>
|
|
|
/// <returns></returns>
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
|
public override async Task<List<TagInfo>> GetRFIDAsync(int timeout = 1500)
|
|
|
{
|
|
|
List<TagInfo> tagInfoList = new List<TagInfo>();
|
|
|
byte[] u16byte = new byte[2];
|
|
|
byte[] bCRC = new byte[4];
|
|
|
try
|
|
|
{
|
|
|
#region 指令封装
|
|
|
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[8];
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x02;
|
|
|
pMessagePack.m_pData[3] = 0x02;
|
|
|
//1s
|
|
|
pMessagePack.m_pData[4] = 0x03;
|
|
|
pMessagePack.m_pData[5] = 0xE8;
|
|
|
//2s
|
|
|
//pMessagePack.m_pData[4] = 0x07;
|
|
|
//pMessagePack.m_pData[5] = 0xD0;
|
|
|
//3s
|
|
|
//pMessagePack.m_pData[4] = 0x0B;
|
|
|
//pMessagePack.m_pData[5] = 0xA0;
|
|
|
|
|
|
Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4);
|
|
|
pMessagePack.m_pData[6] = _stringChange.CalculateVerify(bCRC, bCRC.Length);
|
|
|
pMessagePack.m_pData[7] = 0x0D;
|
|
|
|
|
|
#endregion 指令封装
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, timeout))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
|
|
|
Log.Information($"{m_deviceID}发送读取指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
|
|
|
byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length);
|
|
|
Log.Information($"{m_deviceID}接收读取指令{_stringChange.bytesToHexStr(resultBuffer, resultBuffer.Length)}");
|
|
|
|
|
|
tagInfoList = Device_DealTagInfoList(resultBuffer);
|
|
|
|
|
|
return tagInfoList;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Log.Information($"{m_deviceID}数据接收异常" + e);
|
|
|
return tagInfoList;
|
|
|
//throw new InvalidOperationException($"{m_strIP}按时间段盘点异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 设置GPIO
|
|
|
/// </summary>
|
|
|
/// <param name="port"></param>
|
|
|
/// <param name="Timedout"></param>
|
|
|
/// <returns></returns>
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
|
public override async Task<bool> WriteAlarmLight(int port, UInt16 Timedout)
|
|
|
{
|
|
|
byte[] u16byte = new byte[2];
|
|
|
byte[] bCRC = new byte[6];
|
|
|
try
|
|
|
{
|
|
|
#region 指令封装
|
|
|
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[10];
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x04;
|
|
|
pMessagePack.m_pData[3] = 0x51;
|
|
|
pMessagePack.m_pData[4] = (byte)port;
|
|
|
pMessagePack.m_pData[5] = 0x00;
|
|
|
u16byte = BitConverter.GetBytes(Timedout); //超时时间
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 6, 2);
|
|
|
Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 6);
|
|
|
pMessagePack.m_pData[8] = _stringChange.CalculateVerify(bCRC, bCRC.Length);
|
|
|
pMessagePack.m_pData[9] = 0x0D;
|
|
|
|
|
|
#endregion 指令封装
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
if (reciveBuffer[3] == 0x51)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
throw new InvalidOperationException($"{m_strIP}设置GPIO异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 设置功率
|
|
|
/// </summary>
|
|
|
/// <param name="iDbi"></param>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<bool> Set_Rf(int iDbi)
|
|
|
{
|
|
|
byte[] u16byte = new byte[2];
|
|
|
byte[] bCRC = new byte[22];
|
|
|
try
|
|
|
{
|
|
|
#region 指令封装
|
|
|
iDbi = iDbi * 100;
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[26];
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x14;
|
|
|
pMessagePack.m_pData[3] = 0x42;
|
|
|
pMessagePack.m_pData[4] = 0x01;
|
|
|
u16byte = BitConverter.GetBytes(iDbi); //功率
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 5, 2);
|
|
|
u16byte = BitConverter.GetBytes(iDbi); //功率
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 7, 2);
|
|
|
|
|
|
pMessagePack.m_pData[9] = 0x02;
|
|
|
u16byte = BitConverter.GetBytes(iDbi); //功率
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 10, 2);
|
|
|
u16byte = BitConverter.GetBytes(iDbi); //功率
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 12, 2);
|
|
|
pMessagePack.m_pData[14] = 0x03;
|
|
|
u16byte = BitConverter.GetBytes(iDbi); //功率
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 15, 2);
|
|
|
u16byte = BitConverter.GetBytes(iDbi); //功率
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 17, 2);
|
|
|
pMessagePack.m_pData[19] = 0x04;
|
|
|
u16byte = BitConverter.GetBytes(iDbi); //功率
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 20, 2);
|
|
|
u16byte = BitConverter.GetBytes(iDbi); //功率
|
|
|
u16byte = _stringChange.Swap16Bytes(u16byte); //协议里为大端在前
|
|
|
Array.Copy(u16byte, 0, pMessagePack.m_pData, 22, 2);
|
|
|
Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 22);
|
|
|
pMessagePack.m_pData[24] = _stringChange.CalculateVerify(bCRC, bCRC.Length);
|
|
|
pMessagePack.m_pData[25] = 0x0D;
|
|
|
#endregion 指令封装
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
Log.Information($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
Log.Information($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
if (reciveBuffer[3] == 0x42)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return false;
|
|
|
//throw new InvalidOperationException($"{m_strIP}设置功率异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public override async Task<string> Get_Rf()
|
|
|
{
|
|
|
int DB = 0;
|
|
|
byte[] u16byte = new byte[2];
|
|
|
byte[] bCRC = new byte[2];
|
|
|
try
|
|
|
{
|
|
|
#region 指令封装
|
|
|
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[6];
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x00;
|
|
|
pMessagePack.m_pData[3] = 0x72;
|
|
|
Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 2);
|
|
|
pMessagePack.m_pData[4] = _stringChange.CalculateVerify(bCRC, bCRC.Length);
|
|
|
pMessagePack.m_pData[5] = 0x0D;
|
|
|
#endregion 指令封装
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
Log.Information($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
if (reciveBuffer[3] == 0x72)
|
|
|
{
|
|
|
byte[] bDB = new byte[2];
|
|
|
bDB[0] = reciveBuffer[6];
|
|
|
bDB[1] = reciveBuffer[7];
|
|
|
DB = Convert.ToInt32(_stringChange.bytesToHexStr(bDB, 2), 16) / 100;
|
|
|
return DB.ToString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
return DB.ToString();
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return DB.ToString();
|
|
|
//throw new InvalidOperationException($"{m_strIP}设置功率异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public override bool GetOnlineStatus()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return _tcpClient.Online;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public override void Close()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
_tcpClient.CloseAsync();
|
|
|
//_tcpClient.Dispose();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Log.Information(ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 停止连续盘点
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<bool> Set_StopIdentify()
|
|
|
{
|
|
|
byte[] bCRC = new byte[4];
|
|
|
try
|
|
|
{
|
|
|
#region 指令封装
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[8];
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x02;
|
|
|
pMessagePack.m_pData[3] = 0x12;
|
|
|
pMessagePack.m_pData[4] = 0x00;
|
|
|
pMessagePack.m_pData[5] = 0x00;
|
|
|
Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4);
|
|
|
pMessagePack.m_pData[6] = _stringChange.CalculateVerify(bCRC, bCRC.Length);
|
|
|
pMessagePack.m_pData[7] = 0x0D;
|
|
|
#endregion 指令封装
|
|
|
|
|
|
|
|
|
_tcpClient.SendAsync(pMessagePack.m_pData).GetAwaiter().GetResult();
|
|
|
//var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
//{
|
|
|
// FilterFunc = response =>
|
|
|
// {
|
|
|
// // 检查响应数据是否符合预期
|
|
|
// if (response.Memory.Length > 0)
|
|
|
// {
|
|
|
// // 可以根据实际情况添加更多的检查逻辑
|
|
|
// return true;
|
|
|
// }
|
|
|
// return false;
|
|
|
// }
|
|
|
//});
|
|
|
//Log.Information($"{m_deviceID}发送停止连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
//using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000))
|
|
|
//{
|
|
|
// var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
// Log.Information($"{m_deviceID}接收停止连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
// if (reciveBuffer[3] == 0x12)
|
|
|
// {
|
|
|
// return true;
|
|
|
// }
|
|
|
//}
|
|
|
//return false;
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
//throw new InvalidOperationException($"{m_strIP}暂停盘点异常:{e.Message}");
|
|
|
Log.Error($"{m_deviceID},IP:{m_strIP}暂停盘点异常:{e.Message}");
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 开始连续盘点
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<bool> Set_BeginIdentify()
|
|
|
{
|
|
|
byte[] bCRC = new byte[4];
|
|
|
try
|
|
|
{
|
|
|
#region 指令封装
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[8];
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x02;
|
|
|
pMessagePack.m_pData[3] = 0x11;
|
|
|
pMessagePack.m_pData[4] = 0x00;
|
|
|
pMessagePack.m_pData[5] = 0x00;
|
|
|
Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4);
|
|
|
pMessagePack.m_pData[6] = _stringChange.CalculateVerify(bCRC, bCRC.Length);
|
|
|
pMessagePack.m_pData[7] = 0x0D;
|
|
|
#endregion 指令封装
|
|
|
|
|
|
_tcpClient.SendAsync(pMessagePack.m_pData).GetAwaiter().GetResult();
|
|
|
|
|
|
//var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
//{
|
|
|
// FilterFunc = response =>
|
|
|
// {
|
|
|
// // 检查响应数据是否符合预期
|
|
|
// if (response.Memory.Length > 0)
|
|
|
// {
|
|
|
// // 可以根据实际情况添加更多的检查逻辑
|
|
|
// return true;
|
|
|
// }
|
|
|
// return false;
|
|
|
// }
|
|
|
//});
|
|
|
//Log.Information($"{m_deviceID}发送连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
//using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000))
|
|
|
//{
|
|
|
// var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
// Log.Information($"{m_deviceID}接收连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
// if (reciveBuffer[3] == 0x11)
|
|
|
// {
|
|
|
// return true;
|
|
|
// }
|
|
|
//}
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return false;
|
|
|
//throw new InvalidOperationException($"{m_strIP}设置功率异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 写入
|
|
|
/// </summary>
|
|
|
/// <param name="pSelectData"></param>
|
|
|
/// <param name="pWriteData"></param>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<bool> Set_Write(byte[] pSelectData, string pWriteData)
|
|
|
{
|
|
|
byte[] bWriteData = null;
|
|
|
if (Convert.ToByte(pWriteData.Length % 2) != 0)
|
|
|
{
|
|
|
bWriteData = new byte[pWriteData.Length + 1];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
bWriteData = new byte[pWriteData.Length];
|
|
|
}
|
|
|
|
|
|
if (pSelectData == null || pSelectData.Count() == 0)
|
|
|
{
|
|
|
pSelectData = Encoding.ASCII.GetBytes("JG26522604101");
|
|
|
}
|
|
|
|
|
|
if (string.IsNullOrEmpty(pWriteData))
|
|
|
{
|
|
|
bWriteData = Encoding.ASCII.GetBytes("JG26522604102");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
byte[] bytes = Encoding.ASCII.GetBytes(pWriteData);
|
|
|
Array.Copy(bytes, 0, bWriteData, 0, bytes.Length);
|
|
|
//bWriteData = Encoding.ASCII.GetBytes(pWriteData);
|
|
|
}
|
|
|
|
|
|
//计算PC并衔接
|
|
|
byte[] finalWriteData = new byte[bWriteData.Length + 2];
|
|
|
byte[] pc = _stringChange.IntToBytes(2, ((bWriteData.Length / 2) * 8)).Reverse().ToArray();
|
|
|
Array.Copy(pc, 0, finalWriteData, 0, 2);
|
|
|
Array.Copy(bWriteData, 0, finalWriteData, 2, bWriteData.Length);
|
|
|
|
|
|
try
|
|
|
{
|
|
|
#region 指令封装
|
|
|
//自动根据写入数据封装
|
|
|
Base03HENtity rawData = new Base03HENtity()
|
|
|
{
|
|
|
TimeOut = _stringChange.IntToBytes(2, 1000),
|
|
|
AccessPassword = _stringChange.IntToBytes(4, 0),
|
|
|
SelectBank = 01,
|
|
|
SelectAddress = _stringChange.IntToBytes(4, 32),
|
|
|
SelectLength = (byte)(pSelectData.Count() * 8),
|
|
|
SelectData = pSelectData,
|
|
|
WriteBank = 01,
|
|
|
WriteAddress = _stringChange.IntToBytes(4, 1),
|
|
|
WordCount = (byte)((finalWriteData.Count() / 2)),
|
|
|
WriteData = finalWriteData
|
|
|
};
|
|
|
|
|
|
int selectLength = rawData.SelectLength / 8;
|
|
|
int wordCount = rawData.WordCount * 2;
|
|
|
byte[] bytes = new byte[2 + 4 + 1 + 4 + 1 + selectLength + 1 + 4 + 1 + wordCount];
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
Array.Copy(rawData.TimeOut, 0, bytes, index, 2);
|
|
|
index += 2;
|
|
|
|
|
|
Array.Copy(rawData.AccessPassword, 0, bytes, index, 4);
|
|
|
index += 4;
|
|
|
|
|
|
bytes[index] = rawData.SelectBank;
|
|
|
index++;
|
|
|
|
|
|
Array.Copy(rawData.SelectAddress, 0, bytes, index, 4);
|
|
|
index += 4;
|
|
|
|
|
|
bytes[index] = rawData.SelectLength;
|
|
|
index++;
|
|
|
|
|
|
Array.Copy(rawData.SelectData, 0, bytes, index, selectLength);
|
|
|
index += selectLength;
|
|
|
|
|
|
bytes[index] = rawData.WriteBank;
|
|
|
index++;
|
|
|
|
|
|
Array.Copy(rawData.WriteAddress, 0, bytes, index, 4);
|
|
|
index += 4;
|
|
|
|
|
|
bytes[index] = rawData.WordCount;
|
|
|
index++;
|
|
|
|
|
|
Array.Copy(rawData.WriteData, 0, bytes, index, wordCount);
|
|
|
index += wordCount;
|
|
|
|
|
|
BaseSendDataEntity entity = new BaseSendDataEntity()
|
|
|
{
|
|
|
Code = 0x03,
|
|
|
Data = bytes
|
|
|
};
|
|
|
byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity);
|
|
|
#endregion 指令封装
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
Log.Information($"{m_deviceID}发送写入指令{_stringChange.bytesToHexStr(result, result.Length)}");
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(result, 2000))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
Log.Information($"{m_deviceID}接收写入指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
if (reciveBuffer[3] == 0x03 && reciveBuffer[4] == 0x00)
|
|
|
{
|
|
|
Log.Information($"写入成功,写入数值{pWriteData}");
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
throw new InvalidOperationException($"{m_strIP}写入异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#region 标签解析
|
|
|
|
|
|
/// <summary>
|
|
|
/// 状态机函数
|
|
|
/// </summary>
|
|
|
/// <param name="buffer"></param>
|
|
|
/// <param name="iLen"></param>
|
|
|
/// <returns></returns>
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
|
private byte[] PareReceiveBufferData(byte[] buffer, int iLen)
|
|
|
{
|
|
|
RecvState enumRecvState = RecvState.WaitingBeginChar1_State;
|
|
|
int m_iPosition = 0;
|
|
|
UInt16 m_iFullMessageLength = 0;
|
|
|
int iBarcodeLength = 0;//条码长度
|
|
|
ArrayList m_FrecvData = new ArrayList();
|
|
|
byte m_iVerify = 0;
|
|
|
try
|
|
|
{
|
|
|
var bufferStr = _stringChange.bytesToHexStr(buffer, iLen);
|
|
|
byte[] m_szFullMessage = new byte[iLen];
|
|
|
for (int i = 0; i < iLen; i++)
|
|
|
{
|
|
|
switch (enumRecvState)
|
|
|
{
|
|
|
case RecvState.WaitingBeginChar1_State: //开始接受数据帧1 0xBB
|
|
|
Array.Clear(m_szFullMessage, 0, iLen);//清空为0
|
|
|
if (buffer[i] == 0xBB)
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingBeginChar2_State;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
m_iFullMessageLength = 0;
|
|
|
m_iPosition = 0;
|
|
|
enumRecvState = RecvState.WaitingBeginChar1_State;
|
|
|
}
|
|
|
break;
|
|
|
case RecvState.WaitingBeginChar2_State: //开始接受数据帧1 0xDD
|
|
|
if (buffer[i] == 0xDD)
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForBarcodeLength_State;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
m_iFullMessageLength = 0;
|
|
|
m_iPosition = 0;
|
|
|
enumRecvState = RecvState.WaitingBeginChar1_State;
|
|
|
}
|
|
|
break;
|
|
|
case RecvState.WaitingForBarcodeLength_State: //开始接受数据长度(TagCount - EPC)
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
iBarcodeLength = buffer[i]; //单组标签:18;两组标签:35
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForCode_State;
|
|
|
break;
|
|
|
|
|
|
case RecvState.WaitingForCode_State: //开始接受指令编号
|
|
|
if (buffer[i] == 0x01)
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForStus_State;
|
|
|
}
|
|
|
else if (buffer[i] == 0x02)
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForStus_State;
|
|
|
}
|
|
|
else if (buffer[i] == 0x42)
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForStus_State;
|
|
|
}
|
|
|
else if (buffer[i] == 0x72)
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForStus_State;
|
|
|
}
|
|
|
else if (buffer[i] == 0x90) // 如果是心跳BB DD 01 90 00 1F 8E 0D
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForEndChar_State;
|
|
|
}
|
|
|
else if (buffer[i] == 0xBF) // 如果是心跳BB DD 04 BF 00 00 00 F9 0B 49 0D
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForEndChar_State;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
m_iFullMessageLength = 0;
|
|
|
m_iPosition = 0;
|
|
|
enumRecvState = RecvState.WaitingBeginChar1_State;
|
|
|
}
|
|
|
break;
|
|
|
case RecvState.WaitingForStus_State: //开始接受状态码
|
|
|
if (buffer[i] == 0x00)
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForTagCount_State;
|
|
|
}
|
|
|
else if (buffer[i] == 0x40)
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
//LogService.Instance.Debug("RFU620等待接受WaitingForEndChar_State:Noread");
|
|
|
lock (m_FrecvData)
|
|
|
{
|
|
|
m_FrecvData.Add(m_szFullMessage);
|
|
|
}
|
|
|
m_iPosition = 0;
|
|
|
i = iLen;
|
|
|
enumRecvState = RecvState.WaitingBeginChar1_State;
|
|
|
//LogService.Instance.Debug("RFly-I160状态机结束。");
|
|
|
}
|
|
|
break;
|
|
|
case RecvState.WaitingForTagCount_State: //开始接受标签组数
|
|
|
Array.Copy(buffer, i, m_szFullMessage, m_iPosition, iBarcodeLength);//m_iPosition = 5
|
|
|
byte[] tempData = new byte[iBarcodeLength];
|
|
|
Array.Clear(tempData, 0, iBarcodeLength);
|
|
|
Array.Copy(buffer, i, tempData, 0, iBarcodeLength);
|
|
|
m_iPosition = m_iPosition + iBarcodeLength; //m_iPosition = 39
|
|
|
i = i + iBarcodeLength - 1; //i = 39
|
|
|
enumRecvState = RecvState.WaitingForXor_State;
|
|
|
break;
|
|
|
case RecvState.WaitingForXor_State: //开始比对校验位 Rfly160
|
|
|
byte[] m_CRCVerify = new byte[1024]; //此数组用于校验位计算
|
|
|
Array.Clear(m_CRCVerify, 0, m_CRCVerify.Length);
|
|
|
Array.Copy(m_szFullMessage, 2, m_CRCVerify, 0, iBarcodeLength + 3); //校验位计算是从Length - EPC 结束
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iVerify = m_szFullMessage[m_iPosition];
|
|
|
if (m_iVerify == _stringChange.CalculateVerify(m_CRCVerify, m_CRCVerify.Length))
|
|
|
{
|
|
|
m_iPosition++;
|
|
|
enumRecvState = RecvState.WaitingForEndChar_State;
|
|
|
}
|
|
|
else //如果校验不成功
|
|
|
{
|
|
|
m_iFullMessageLength = 0;
|
|
|
m_iPosition = 0;
|
|
|
enumRecvState = RecvState.WaitingBeginChar1_State;
|
|
|
}
|
|
|
break;
|
|
|
case RecvState.WaitingForEndChar_State:
|
|
|
if (buffer[0] == 0xBB && buffer[1] == 0xDD && buffer[2] == 0x00 && buffer[3] != 0x90) //此处为Noread数据显示
|
|
|
{
|
|
|
m_szFullMessage[0] = 0xBB;
|
|
|
m_szFullMessage[1] = 0xDD;
|
|
|
m_szFullMessage[2] = 0x00;
|
|
|
lock (m_FrecvData)
|
|
|
{
|
|
|
m_FrecvData.Add(m_szFullMessage);
|
|
|
}
|
|
|
m_iPosition = 0;
|
|
|
i = iLen;
|
|
|
enumRecvState = RecvState.WaitingBeginChar1_State;
|
|
|
}
|
|
|
else if (buffer[0] == 0xBB && buffer[1] == 0xDD && buffer[2] == 0x04 && buffer[3] == 0xBF)
|
|
|
{
|
|
|
Array.Copy(buffer, 0, m_szFullMessage, 0, 11);
|
|
|
i = 11;
|
|
|
lock (m_FrecvData)
|
|
|
{
|
|
|
m_FrecvData.Add(m_szFullMessage);
|
|
|
}
|
|
|
i = iLen;
|
|
|
}
|
|
|
else if (buffer[i] == 0x00) //获取温度
|
|
|
{
|
|
|
Array.Copy(buffer, 0, m_szFullMessage, 0, 8);
|
|
|
i = 8;
|
|
|
lock (m_FrecvData)
|
|
|
{
|
|
|
m_FrecvData.Add(m_szFullMessage);
|
|
|
}
|
|
|
i = iLen;
|
|
|
}
|
|
|
else if (buffer[i] == 0x11)
|
|
|
{
|
|
|
Array.Copy(buffer, 0, m_szFullMessage, 0, 7);
|
|
|
i = 7;
|
|
|
lock (m_FrecvData)
|
|
|
{
|
|
|
m_FrecvData.Add(m_szFullMessage);
|
|
|
}
|
|
|
}
|
|
|
else if (buffer[i] == 0x01)
|
|
|
{
|
|
|
Array.Copy(buffer, 0, m_szFullMessage, 0, 8);
|
|
|
i = 8;
|
|
|
lock (m_FrecvData)
|
|
|
{
|
|
|
m_FrecvData.Add(m_szFullMessage);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
m_szFullMessage[m_iPosition] = buffer[i];
|
|
|
m_iPosition++;
|
|
|
if (buffer[i] == 0x0D)
|
|
|
{
|
|
|
lock (m_FrecvData)
|
|
|
{
|
|
|
m_FrecvData.Add(m_szFullMessage);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
m_iPosition = 0;
|
|
|
enumRecvState = RecvState.WaitingBeginChar1_State;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return m_szFullMessage;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
throw new InvalidOperationException($"状态机逻辑处理异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
public List<TagInfo> Device_LXDealTagInfoList(byte[] AutoDealReportData)
|
|
|
{
|
|
|
List<TagInfo> tagInfoList = new List<TagInfo>();
|
|
|
byte[] bResultEPC_Data = new byte[14];
|
|
|
byte[] m_AutoReadEPC = null;
|
|
|
int m_readEPCDataLen = 0;
|
|
|
try
|
|
|
{
|
|
|
//mutauto.WaitOne();
|
|
|
int iFirstCountPos = 5; //第一次读取标签次数位置
|
|
|
int iFirstRSSIPos = 6; //第一次读取标签强度位置
|
|
|
int iFirstAnt = 7;
|
|
|
int iFirstPC = 8; //第一次读取标签天线位置
|
|
|
int iFirstLeftBarcketPos = 10;//EPC数据起始位置
|
|
|
UInt16 tempDataCount = 0;
|
|
|
int tempDataRSSI = 0;
|
|
|
UInt16 tempDataANT = 0;
|
|
|
int iBarcodeGroupCount = Convert.ToInt32(AutoDealReportData[5].ToString()); //标签组数
|
|
|
int iBarcodeLength = Convert.ToInt32(AutoDealReportData[2].ToString()) - 5; //标签长度
|
|
|
int iCommonSecondFlag = 0;
|
|
|
for (int j = 0; j < iBarcodeGroupCount; j++)
|
|
|
{
|
|
|
TagInfo tag = new TagInfo();
|
|
|
byte[] tempPCByte = new byte[2]; //取出PC
|
|
|
Array.Clear(tempPCByte, 0, 2);
|
|
|
Array.Copy(AutoDealReportData, iFirstPC, tempPCByte, 0, 2);
|
|
|
|
|
|
//int pc = Convert.ToInt32(tempPCByte[0].ToString("X"));
|
|
|
//PC转二进制取前五位转十进制
|
|
|
int epcLength = Convert.ToInt32(Convert.ToString(Convert.ToInt64(tempPCByte[0].ToString("X"), 16), 2).PadLeft(8, '0').Substring(0, 5), 2) * 2;
|
|
|
|
|
|
//int epcLength = EPCLengthByPC(pc);
|
|
|
iBarcodeLength = epcLength;
|
|
|
|
|
|
byte[] tempDataByte = new byte[epcLength];
|
|
|
Array.Clear(tempDataByte, 0, iBarcodeLength);
|
|
|
Array.Copy(AutoDealReportData, iFirstLeftBarcketPos, tempDataByte, 0, iBarcodeLength);
|
|
|
|
|
|
byte[] tempCountByte = new byte[1]; //取出标签次数
|
|
|
Array.Clear(tempCountByte, 0, 1);
|
|
|
Array.Copy(AutoDealReportData, iFirstCountPos, tempCountByte, 0, 1);
|
|
|
tempDataCount = tempCountByte[0];
|
|
|
|
|
|
byte[] tempRSSIByte = new byte[1]; //取出标签强度
|
|
|
Array.Clear(tempRSSIByte, 0, 1);
|
|
|
Array.Copy(AutoDealReportData, iFirstRSSIPos, tempRSSIByte, 0, 1);
|
|
|
|
|
|
tempDataRSSI = _stringChange.HexStringToNegative(_stringChange.bytesToHexStr(tempRSSIByte, 1));
|
|
|
|
|
|
#region add by wenjy 20220829 取出天线号
|
|
|
byte[] tempAntByte = new byte[1]; //取出天线号
|
|
|
Array.Clear(tempAntByte, 0, 1);
|
|
|
Array.Copy(AutoDealReportData, iFirstAnt, tempAntByte, 0, 1);
|
|
|
tempDataANT = tempAntByte[0];
|
|
|
#endregion
|
|
|
|
|
|
tag.Count = tempDataCount;
|
|
|
tag.RSSI = tempDataRSSI;
|
|
|
tag.EPC = tempDataByte;
|
|
|
//tag.EPCstring = _stringChange.bytesToHexStr(tempDataByte, tempDataByte.Length).Substring(0, 8);
|
|
|
tag.EPCstring = _stringChange.bytesToHexStr(tempDataByte, tempDataByte.Length);
|
|
|
tag.PC = tempPCByte;
|
|
|
tag.Antana = tempDataANT;
|
|
|
tagInfoList.Add(tag);
|
|
|
int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据
|
|
|
|
|
|
iFirstCountPos = iFirstCountPos + iBarcodeLength + 5; //次数
|
|
|
iFirstRSSIPos = iFirstCountPos + 1; //强度
|
|
|
iFirstAnt = iFirstRSSIPos + 1; //天线
|
|
|
iFirstPC = iFirstAnt + 1;
|
|
|
iFirstLeftBarcketPos = iFirstLeftBarcketPos + iBarcodeLength + 5;
|
|
|
_RefreshLogMessageAction?.Invoke($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",\r\n读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]");
|
|
|
Log.Information($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]");
|
|
|
iCommonSecondFlag++;
|
|
|
if (iCommonSecondFlag == iBarcodeGroupCount)
|
|
|
{
|
|
|
TagInfo bestTag = tagInfoList
|
|
|
.OrderByDescending(t => t.Count) // 第一条件:Count从大到小
|
|
|
.ThenByDescending(t => t.RSSI) // 第二条件:RSSI从大到小
|
|
|
.FirstOrDefault();
|
|
|
List<TagInfo> bestTagList = new List<TagInfo>();
|
|
|
bestTagList.Add(bestTag);
|
|
|
//mutauto.ReleaseMutex();
|
|
|
//_logger.LogInformation("《《《返回标签数据!");
|
|
|
return bestTagList;
|
|
|
}
|
|
|
}
|
|
|
return tagInfoList;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
//mutauto.ReleaseMutex();
|
|
|
Log.Information($"{m_deviceID},IP:{m_strIP}Device_AutoDealContent异常" + ex.Message);
|
|
|
return tagInfoList;
|
|
|
//throw new InvalidOperationException($"Device_AutoDealContent 自动处理函数异常:{ex.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//private Mutex mutauto = new Mutex();
|
|
|
/// <summary>
|
|
|
/// 解析函数
|
|
|
/// </summary>
|
|
|
/// <param name="AutoDealReportData"></param>
|
|
|
/// <returns></returns>
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
|
public List<TagInfo> Device_DealTagInfoList(byte[] AutoDealReportData)
|
|
|
{
|
|
|
List<TagInfo> tagInfoList = new List<TagInfo>();
|
|
|
byte[] bResultEPC_Data = new byte[14];
|
|
|
byte[] m_AutoReadEPC = null;
|
|
|
int m_readEPCDataLen = 0;
|
|
|
try
|
|
|
{
|
|
|
//mutauto.WaitOne();
|
|
|
int iFirstCountPos = 6; //第一次读取标签次数位置
|
|
|
int iFirstRSSIPos = 7; //第一次读取标签强度位置
|
|
|
int iFirstAnt = 8;
|
|
|
int iFirstPC = 9; //第一次读取标签天线位置
|
|
|
int iFirstLeftBarcketPos = 11;//EPC数据起始位置
|
|
|
UInt16 tempDataCount = 0;
|
|
|
int tempDataRSSI = 0;
|
|
|
UInt16 tempDataANT = 0;
|
|
|
int iBarcodeGroupCount = Convert.ToInt32(AutoDealReportData[5].ToString()); //标签组数
|
|
|
int iBarcodeLength = 12; //标签长度
|
|
|
int iCommonSecondFlag = 0;
|
|
|
for (int j = 0; j < iBarcodeGroupCount; j++)
|
|
|
{
|
|
|
TagInfo tag = new TagInfo();
|
|
|
byte[] tempPCByte = new byte[2]; //取出PC
|
|
|
Array.Clear(tempPCByte, 0, 2);
|
|
|
Array.Copy(AutoDealReportData, iFirstPC, tempPCByte, 0, 2);
|
|
|
|
|
|
//int pc = Convert.ToInt32(tempPCByte[0].ToString("X"));
|
|
|
//PC转二进制取前五位转十进制
|
|
|
int epcLength = Convert.ToInt32(Convert.ToString(Convert.ToInt64(tempPCByte[0].ToString("X"), 16), 2).PadLeft(8, '0').Substring(0, 5), 2) * 2;
|
|
|
|
|
|
//int epcLength = EPCLengthByPC(pc);
|
|
|
iBarcodeLength = epcLength;
|
|
|
|
|
|
byte[] tempDataByte = new byte[epcLength];
|
|
|
Array.Clear(tempDataByte, 0, iBarcodeLength);
|
|
|
Array.Copy(AutoDealReportData, iFirstLeftBarcketPos, tempDataByte, 0, iBarcodeLength);
|
|
|
|
|
|
byte[] tempCountByte = new byte[1]; //取出标签次数
|
|
|
Array.Clear(tempCountByte, 0, 1);
|
|
|
Array.Copy(AutoDealReportData, iFirstCountPos, tempCountByte, 0, 1);
|
|
|
tempDataCount = tempCountByte[0];
|
|
|
|
|
|
byte[] tempRSSIByte = new byte[1]; //取出标签强度
|
|
|
Array.Clear(tempRSSIByte, 0, 1);
|
|
|
Array.Copy(AutoDealReportData, iFirstRSSIPos, tempRSSIByte, 0, 1);
|
|
|
|
|
|
tempDataRSSI = _stringChange.HexStringToNegative(_stringChange.bytesToHexStr(tempRSSIByte, 1));
|
|
|
|
|
|
#region add by wenjy 20220829 取出天线号
|
|
|
byte[] tempAntByte = new byte[1]; //取出天线号
|
|
|
Array.Clear(tempAntByte, 0, 1);
|
|
|
Array.Copy(AutoDealReportData, iFirstAnt, tempAntByte, 0, 1);
|
|
|
tempDataANT = tempAntByte[0];
|
|
|
#endregion
|
|
|
|
|
|
tag.Count = tempDataCount;
|
|
|
tag.RSSI = tempDataRSSI;
|
|
|
tag.EPC = tempDataByte;
|
|
|
//tag.EPCstring = _stringChange.bytesToHexStr(tempDataByte, tempDataByte.Length).Substring(0, 8);
|
|
|
tag.EPCstring = _stringChange.bytesToHexStr(tempDataByte, tempDataByte.Length);
|
|
|
tag.PC = tempPCByte;
|
|
|
tag.Antana = tempDataANT;
|
|
|
tagInfoList.Add(tag);
|
|
|
int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据
|
|
|
|
|
|
iFirstCountPos = iFirstCountPos + iBarcodeLength + 5; //次数
|
|
|
iFirstRSSIPos = iFirstCountPos + 1; //强度
|
|
|
iFirstAnt = iFirstRSSIPos + 1; //天线
|
|
|
iFirstPC = iFirstAnt + 1;
|
|
|
iFirstLeftBarcketPos = iFirstLeftBarcketPos + iBarcodeLength + 5;
|
|
|
_RefreshLogMessageAction?.Invoke($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",\r\n读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]");
|
|
|
Log.Information($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]");
|
|
|
iCommonSecondFlag++;
|
|
|
if (iCommonSecondFlag == iBarcodeGroupCount)
|
|
|
{
|
|
|
TagInfo bestTag = tagInfoList
|
|
|
.OrderByDescending(t => t.Count) // 第一条件:Count从大到小
|
|
|
.ThenByDescending(t => t.RSSI) // 第二条件:RSSI从大到小
|
|
|
.FirstOrDefault();
|
|
|
List<TagInfo> bestTagList = new List<TagInfo>();
|
|
|
bestTagList.Add(bestTag);
|
|
|
//mutauto.ReleaseMutex();
|
|
|
//_logger.LogInformation("《《《返回标签数据!");
|
|
|
return bestTagList;
|
|
|
}
|
|
|
}
|
|
|
return tagInfoList;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
//mutauto.ReleaseMutex();
|
|
|
Log.Information($"{m_deviceID},IP:{m_strIP}Device_AutoDealContent异常" + ex.Message);
|
|
|
return tagInfoList;
|
|
|
//throw new InvalidOperationException($"Device_AutoDealContent 自动处理函数异常:{ex.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// 设置过滤参数
|
|
|
/// </summary>
|
|
|
/// <param name="Filter"></param>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<bool> Set_FilterData(string Filter)
|
|
|
{
|
|
|
bool iflag = false;
|
|
|
string pre = "filter ";
|
|
|
//Filter = "FC 08";
|
|
|
try
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(Filter))
|
|
|
{
|
|
|
if (Filter != "0")
|
|
|
{
|
|
|
Filter = pre + Filter.Trim().Replace(" ", "").Length / 2 + " " + Filter;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Filter = pre + Filter.Trim();
|
|
|
}
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
//Log.Information($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
Filter = Filter + "\r\n";
|
|
|
//byte[] reciveBuffer = await waitClient.SendThenReturnAsync(Filter + "\r\n", 2000);
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(Filter, 2000))
|
|
|
{
|
|
|
var memory = responsedData.Memory;
|
|
|
string result = Encoding.GetEncoding("GBK").GetString(memory.ToArray(), 0, memory.Length);
|
|
|
Log.Information($"{m_deviceID}接收设置过滤指令{result}");
|
|
|
return true;
|
|
|
}
|
|
|
//Log.Information($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
}
|
|
|
return iflag;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
|
|
|
return iflag;
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取过滤参数
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<string> Get_FilterData()
|
|
|
{
|
|
|
string iflag = "";
|
|
|
try
|
|
|
{
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
//Log.Information($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
|
|
|
//byte[] reciveBuffer = await waitClient.SendThenReturnAsync("filter?\r\n", 2000);
|
|
|
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync("filter?\r\n", 2000))
|
|
|
{
|
|
|
var memory = responsedData.Memory;
|
|
|
string result = Encoding.GetEncoding("GBK").GetString(memory.ToArray(), 0, memory.Length);
|
|
|
Log.Information($"{m_deviceID}接收设置过滤指令{result}");
|
|
|
return result;
|
|
|
}
|
|
|
return iflag;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
|
|
|
return iflag;
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 设置频段
|
|
|
/// </summary>
|
|
|
/// <param name="Frequencyband"></param>
|
|
|
/// <returns></returns>
|
|
|
public override async Task<bool> Set_Frequencyband(string Frequencyband)
|
|
|
{
|
|
|
bool iflag = false;
|
|
|
byte[] bCRC = new byte[3];
|
|
|
try
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(Frequencyband))
|
|
|
{
|
|
|
return iflag;
|
|
|
}
|
|
|
Frequencyband = StringChange.GetRegionNumber(Frequencyband);
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[7];
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x01;
|
|
|
pMessagePack.m_pData[3] = 0x44;
|
|
|
pMessagePack.m_pData[4] = byte.Parse(Frequencyband);
|
|
|
Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 3);
|
|
|
pMessagePack.m_pData[5] = _stringChange.CalculateVerify(bCRC, bCRC.Length);
|
|
|
pMessagePack.m_pData[6] = 0x0D;
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
Log.Information($"{m_deviceID}发送设置频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
Log.Information($"{m_deviceID}接收设置频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
if (reciveBuffer[3] == 0x44)
|
|
|
{
|
|
|
iflag = true;
|
|
|
return iflag;
|
|
|
}
|
|
|
return iflag;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
|
|
|
return iflag;
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取频段
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
public override async Task<string> Get_Frequencyband()
|
|
|
{
|
|
|
string Frequencyband = "";
|
|
|
byte[] u16byte = new byte[2];
|
|
|
byte[] bCRC = new byte[2];
|
|
|
try
|
|
|
{
|
|
|
#region 指令封装
|
|
|
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[6];
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x00;
|
|
|
pMessagePack.m_pData[3] = 0x74;
|
|
|
Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 2);
|
|
|
pMessagePack.m_pData[4] = _stringChange.CalculateVerify(bCRC, bCRC.Length);
|
|
|
pMessagePack.m_pData[5] = 0x0D;
|
|
|
#endregion 指令封装
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
Log.Information($"{m_deviceID}发送获取频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
Log.Information($"{m_deviceID}接收获取频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
if (reciveBuffer[3] == 0x74)
|
|
|
{
|
|
|
byte bDB = reciveBuffer[5];
|
|
|
Frequencyband = StringChange.GetRegionByNumber(bDB.ToString());
|
|
|
return Frequencyband;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
return Frequencyband;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return Frequencyband;
|
|
|
//throw new InvalidOperationException($"{m_strIP}设置功率异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void DealMessageData(byte[] reciveBuffer)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
//连续盘点返回
|
|
|
if (reciveBuffer[3] == 0x01)
|
|
|
{
|
|
|
Log.Information($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length);
|
|
|
List<TagInfo> tagInfoList = Device_LXDealTagInfoList(resultBuffer);
|
|
|
string info = "";
|
|
|
if (tagInfoList != null && tagInfoList.Count > 0)
|
|
|
{
|
|
|
tagInfoList = tagInfoList.Where((x, i) => tagInfoList.FindIndex(z => z.EPCstring == x.EPCstring) == i).ToList();
|
|
|
info = string.Join(",", tagInfoList.Select(item => item.EPCstring));
|
|
|
_Action?.Invoke(m_deviceID, tagInfoList);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
info = "Nodata";
|
|
|
}
|
|
|
|
|
|
}
|
|
|
//按时间段盘点返回
|
|
|
if (reciveBuffer[3] == 0x02)
|
|
|
{
|
|
|
Log.Information($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length);
|
|
|
List<TagInfo> tagInfoList = Device_DealTagInfoList(resultBuffer);
|
|
|
string info = "";
|
|
|
if (tagInfoList != null && tagInfoList.Count > 0)
|
|
|
{
|
|
|
tagInfoList = tagInfoList.Where((x, i) => tagInfoList.FindIndex(z => z.EPCstring == x.EPCstring) == i).ToList();
|
|
|
info = string.Join(",", tagInfoList.Select(item => item.EPCstring));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
info = "Nodata";
|
|
|
}
|
|
|
_Action?.Invoke(m_deviceID, tagInfoList);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Log.Information($"》》》设备:{m_deviceID},IP:{m_strIP},处理数据异常:{ex}");
|
|
|
}
|
|
|
}
|
|
|
public override async Task<bool> SendHeartPack()
|
|
|
{
|
|
|
bool iflag = false;
|
|
|
byte[] bCRC = new byte[2];
|
|
|
try
|
|
|
{
|
|
|
|
|
|
MessagePack pMessagePack = new MessagePack();
|
|
|
pMessagePack.m_pData = new byte[6];
|
|
|
//获取温度
|
|
|
pMessagePack.m_pData[0] = 0xAA;
|
|
|
pMessagePack.m_pData[1] = 0x55;
|
|
|
pMessagePack.m_pData[2] = 0x00;
|
|
|
pMessagePack.m_pData[3] = 0x90;
|
|
|
pMessagePack.m_pData[4] = 0x90;
|
|
|
pMessagePack.m_pData[5] = 0x0D;
|
|
|
|
|
|
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
|
|
|
{
|
|
|
FilterFunc = response =>
|
|
|
{
|
|
|
// 检查响应数据是否符合预期
|
|
|
if (response.Memory.Length > 0)
|
|
|
{
|
|
|
// 可以根据实际情况添加更多的检查逻辑
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
_logger.Data($"{m_deviceID},IP:{m_strIP}发送心跳包{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}");
|
|
|
using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000))
|
|
|
{
|
|
|
var reciveBuffer = responsedData.Memory.ToArray();
|
|
|
|
|
|
if (reciveBuffer[3] == 0x90)
|
|
|
{
|
|
|
if (reciveBuffer.Length == 8)
|
|
|
{
|
|
|
_logger.Data($"{m_deviceID},IP:{m_strIP}接收心跳包{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}");
|
|
|
iflag = true;
|
|
|
return iflag;
|
|
|
}
|
|
|
//粘包处理
|
|
|
//BBDD01900029B80D BBDD1202000132CD043000333131303432360000000000E90D
|
|
|
//BBDD1202000138CF043000333131303035360000000000E20DBBDD01900026B70D
|
|
|
else
|
|
|
{
|
|
|
var messagebuffer = new byte[reciveBuffer.Length - 8];
|
|
|
Array.Copy(reciveBuffer, 8, messagebuffer, 0, reciveBuffer.Length - 8);
|
|
|
DealMessageData(messagebuffer);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
if (reciveBuffer[3] == 0x02)
|
|
|
{
|
|
|
DealMessageData(reciveBuffer);
|
|
|
}
|
|
|
return iflag;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.Data($"》》》设备:{m_deviceID},IP:{m_strIP},心跳包发送超时:{ex}");
|
|
|
return iflag;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 将byte数组转换为十六进制字符串(格式:00 0A 0B)
|
|
|
/// </summary>
|
|
|
public string BytesToHexString(byte[] bytes)
|
|
|
{
|
|
|
if (bytes == null || bytes.Length == 0)
|
|
|
return string.Empty;
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
|
{
|
|
|
sb.Append(bytes[i].ToString("X2"));
|
|
|
if (i < bytes.Length - 1)
|
|
|
sb.Append(" ");
|
|
|
}
|
|
|
|
|
|
return sb.ToString();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 将十六进制字符串转换回byte数组
|
|
|
/// </summary>
|
|
|
public byte[] HexStringToBytes(string hexString)
|
|
|
{
|
|
|
if (string.IsNullOrWhiteSpace(hexString))
|
|
|
{
|
|
|
Console.WriteLine("传入数据为空");
|
|
|
return new byte[0];
|
|
|
}
|
|
|
|
|
|
// 移除所有空格
|
|
|
hexString = hexString.Replace(" ", "");
|
|
|
|
|
|
// 检查长度是否为偶数
|
|
|
if (hexString.Length % 2 != 0)
|
|
|
Console.WriteLine("格式不正确16进制字符串必须为偶数");
|
|
|
|
|
|
byte[] bytes = new byte[hexString.Length / 2];
|
|
|
|
|
|
try
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
|
{
|
|
|
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
|
|
}
|
|
|
}
|
|
|
catch { Console.WriteLine("格式不正确"); }
|
|
|
|
|
|
|
|
|
return bytes;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
}
|
|
|
} |