You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.2 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2025 WenJY 保留所有权利。
* CLR版本4.0.30319.42000
* 机器名称Mr.Wen's MacBook Pro
* 命名空间Sln.Iot.Socket.Adapter
* 唯一标识50003A25-42CE-44A7-9940-FFDE3BD0A52A
*
* 创建者WenJY
* 电子邮箱:
* 创建时间2025-04-11 13:56:24
* 版本V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System;
using System.Text;
using TouchSocket.Core;
namespace Sln.Iot.Socket.Adapter
{
public class CustomDataHandlingAdapter:CustomDataHandlingAdapter<BufferRequestInfo>
{
protected override FilterResult Filter(in ByteBlock byteBlock, bool beCached, ref BufferRequestInfo request, ref int tempCapacity)
{
CacheTimeoutEnable = true;
CacheTimeout = new TimeSpan(0, 0, 0, 0, 5000);
int pos = byteBlock.Pos;
try
{
if (byteBlock.CanReadLen < 5)
{
return FilterResult.Cache;
}
byteBlock.Read(out byte[] header, 1);
byteBlock.Read(out byte[] deviceType, 1);
byteBlock.Read(out byte[] deviceId, 2);
string DeviceType = Encoding.ASCII.GetString(deviceType);
string collectEquipCode = DeviceType + this.ConverToString(deviceId);
byteBlock.Pos += 1;
byteBlock.Read(out byte[] dataType, 1);
// byteBlock.Read(out byte[] lengthByte, 2);
//
// string hexString = BitConverter.ToString(lengthByte).Replace("-", "");
// int bodyLength = Convert.ToInt32(hexString, 16);
GetBodyLength(dataType[0], out int bodyLength);
if (bodyLength > byteBlock.CanReadLen)
{
byteBlock.Pos = pos; //body数据不足。回退游标
return FilterResult.Cache;
}
else
{
byteBlock.Read(out byte[] body, bodyLength);
byteBlock.Read(out byte[] check, 1);
byteBlock.Read(out byte[] tail, 1);
request = new BufferRequestInfo()
{
header = header,
ColletEquipCode = collectEquipCode,
DataType = dataType[0],
BufferLength = bodyLength,
Body = body,
CheckBit = check[0],
Tail = tail,
buffer = byteBlock,
};
return FilterResult.Success;
}
}catch (Exception ex)
{
Logger.Error("FilterResult"+ex.Message);
byteBlock.Pos = pos; //body数据不足。回退游标
return FilterResult.Cache;
}
}
private string ConverToString(byte[] data)
{
string str;
StringBuilder stb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
if ((int)data[i] > 15)
{
stb.Append(Convert.ToString(data[i], 16).ToUpper()); //添加字符串
}
else //如果是小于0F需要加个零
{
stb.Append("0" + Convert.ToString(data[i], 16).ToUpper());
}
}
str = stb.ToString();
return str;
}
public string bytesToHexStr(byte[] bytes, int iLen)
{
StringBuilder sb = new StringBuilder();
if (bytes != null)
{
for (int i = 0; i < iLen; i++)
{
sb.Append(bytes[i].ToString("X2"));
}
}
return sb.ToString();
}
/// <summary>
/// 数据长度,只计算数据区域长度,控制码向后到校验位,不包含校验
/// </summary>
/// <param name="dataType"></param>
/// <param name="length"></param>
private void GetBodyLength(byte dataType, out int length)
{
switch (dataType)
{
case 0x32:
length = 1;
break;
case 0x33:
length = 4;
break;
case 0x36:
length = 1;
break;
case 0x34:
length = 26;
break;
case 0x35:
length = 4;
break;
case 0x37:
length = 5;
break;
case 0x39:
length = 8;
break;
default:
length = 1;
break;
}
}
}
}