20230201_145_upperpc/InSituLaboratory/Common/JZEncapsulation.cs

56 lines
1.3 KiB
C#
Raw Permalink Normal View History

using InSituLaboratory.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InSituLaboratory.Common
{
/// <summary>
/// 基站协议封装
/// </summary>
public class JZEncapsulation
{
public List<byte> EncapAction(List<byte> bytes)
{
//定义封装好的报文List
List<byte> byteaq = new List<byte>();
//添加包头
byteaq.Add(0xAA);
byteaq.Add(0xBB);
//添加版本号
byteaq.Add(0x01);
//添加包长度
byteaq.Add(0x00);
byteaq.Add(0x00);
//添加包ID
byteaq.Add(0x00);
byteaq.Add(0x00);
byteaq.Add(0x00);
byteaq.Add(0x00);
//循环添加下位机报文
foreach (byte b in bytes)
{
byteaq.Add(b);
}
byte[] bytes_length = BitConverter.GetBytes(byteaq.Count() + 3);
byteaq[3] = bytes_length[1];
byteaq[4] = bytes_length[0];
//添加crc校验
byteaq.Add(tools.GetXor(byteaq.ToArray()));
byteaq.Add(0xEE);
byteaq.Add(0xFF);
return byteaq;
}
}
}