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.
194 lines
5.9 KiB
C#
194 lines
5.9 KiB
C#
using HighWayIot.Log4net;
|
|
using HighWayIot.Repository.domain;
|
|
using HighWayIot.Repository.service.Impl;
|
|
using HighWayIot.TouchSocket.Entity;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using TouchSocket.Core;
|
|
|
|
namespace HighWayIot.TouchSocket
|
|
{
|
|
/// <summary>
|
|
/// 服务端接收报文分析
|
|
/// </summary>
|
|
public class ServerBufferAnalysis
|
|
{
|
|
private static readonly Lazy<ServerBufferAnalysis> lazy = new Lazy<ServerBufferAnalysis>(() => new ServerBufferAnalysis());
|
|
|
|
public static ServerBufferAnalysis Instance => lazy.Value;
|
|
|
|
private LogHelper logHelper = LogHelper.Instance;
|
|
|
|
List<byte> tempData = new List<byte>();
|
|
|
|
/// <summary>
|
|
/// 基础数据转包
|
|
/// </summary>
|
|
public BaseMessagePack BaseUnpackServerBufferAnalysis(byte[] bytes)
|
|
{
|
|
BaseMessagePack unpackEntity = new BaseMessagePack();
|
|
int index = 1;
|
|
//报文完整性验证
|
|
if (!MessageCheck(bytes))
|
|
{
|
|
tempData.AddRange(bytes.ToList());
|
|
bytes = tempData.ToArray();
|
|
if (!MessageCheck(bytes))
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
tempData.Clear();
|
|
}
|
|
}
|
|
|
|
//功能码
|
|
Array.Copy(bytes, index, unpackEntity.FunctionCode, 0, 2);
|
|
Array.Reverse(unpackEntity.FunctionCode);
|
|
index += 2;
|
|
|
|
//报文字节长度
|
|
Array.Copy(bytes, index, unpackEntity.Length, 0, 2);
|
|
Array.Reverse(unpackEntity.Length);
|
|
index += 2;
|
|
|
|
//版本号
|
|
unpackEntity.VersionCode = bytes[index];
|
|
index++;
|
|
|
|
//报文序列号
|
|
Array.Copy(bytes, index, unpackEntity.SerialCode, 0, 4);
|
|
Array.Reverse(unpackEntity.SerialCode);
|
|
index += 4;
|
|
|
|
//时间戳
|
|
Array.Copy(bytes, index, unpackEntity.TimeStamp, 0, 8);
|
|
Array.Reverse(unpackEntity.TimeStamp);
|
|
index += 8;
|
|
|
|
//数据内容
|
|
int dataLength = BitConverter.ToUInt16(unpackEntity.Length, 0) - 13;
|
|
unpackEntity.DataContent = new byte[dataLength];
|
|
Array.Copy(bytes, index, unpackEntity.DataContent, 0, dataLength);
|
|
index += dataLength;
|
|
|
|
//校验值
|
|
unpackEntity.CheckSum = bytes[index];
|
|
|
|
//核对校验值
|
|
byte[] checkBytes = new byte[bytes.Length - 4];
|
|
Array.Copy(bytes, 1, checkBytes, 0, bytes.Length - 4);
|
|
if (unpackEntity.CheckSum != CalculateCheckSum(checkBytes))
|
|
{
|
|
logHelper.Error("报文校验失败!");
|
|
return null;
|
|
}
|
|
|
|
//到工厂分类解析
|
|
return unpackEntity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 基础数据封包
|
|
/// </summary>
|
|
public byte[] BasePackedServerBufferAnalysis(BaseMessagePack message)
|
|
{
|
|
int length = BitConverter.ToUInt16(message.Length, 0) + 8;
|
|
byte[] bytes = new byte[length];
|
|
int index = 0;
|
|
//起始字符
|
|
bytes[index] = 0x3A;
|
|
index++;
|
|
|
|
//功能码
|
|
Array.Reverse(message.FunctionCode);
|
|
Array.Copy(message.FunctionCode, 0, bytes, index, 2);
|
|
index += 2;
|
|
|
|
//报文字节长度
|
|
Array.Reverse(message.Length);
|
|
Array.Copy(message.Length, 0, bytes, index, 2);
|
|
index += 2;
|
|
|
|
//版本号
|
|
bytes[index] = message.VersionCode;
|
|
index++;
|
|
|
|
//报文序列号
|
|
//序列号加一
|
|
//uint serial = BitConverter.ToUInt32(message.SerialCode, 0);
|
|
//serial++;
|
|
//message.SerialCode = BitConverter.GetBytes(serial);
|
|
Array.Reverse(message.SerialCode);
|
|
Array.Copy(message.SerialCode, 0, bytes, index, 4);
|
|
index += 4;
|
|
|
|
//时间戳
|
|
Array.Reverse(message.TimeStamp);
|
|
Array.Copy(message.TimeStamp, 0, bytes, index, 8);
|
|
index += 8;
|
|
|
|
//数据内容
|
|
Array.Reverse(message.Length);
|
|
int dataLength = BitConverter.ToUInt16(message.Length, 0) - 13;
|
|
Array.Copy(message.DataContent, 0, bytes, index, dataLength);
|
|
index += dataLength;
|
|
|
|
//校验值
|
|
byte[] checkBytes = new byte[bytes.Length - 4];
|
|
Array.Copy(bytes, 1, checkBytes, 0, bytes.Length - 4);
|
|
bytes[index] = CalculateCheckSum(checkBytes);
|
|
index++;
|
|
|
|
//结束字符1
|
|
bytes[index] = 0x0D;
|
|
index++;
|
|
|
|
//结束字符2
|
|
bytes[index] = 0x0A;
|
|
|
|
return bytes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 报文完整性验证
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <returns>true为通过 false为失败</returns>
|
|
private bool MessageCheck(byte[] message)
|
|
{
|
|
bool result = true;
|
|
if (message[0] != 0x3A || message[message.Length - 1] != 0x0A || message[message.Length - 2] != 0x0D)
|
|
{
|
|
result = false;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 和校验
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
/// <param name="length"></param>
|
|
/// <returns></returns>
|
|
public byte CalculateCheckSum(byte[] bytes)
|
|
{
|
|
int length = bytes.Length;
|
|
int check = 0;
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
check += bytes[i];
|
|
}
|
|
byte result = (byte)(255 - (check % 255));
|
|
return result;
|
|
}
|
|
|
|
}
|
|
}
|