using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.Model
|
{
|
/// <summary>
|
/// 报文头
|
/// </summary>
|
public class Header
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public Header() { }
|
private Header(byte[] bts)
|
{
|
Type = (eDatagramType)BitConverter.ToInt32(bts, 4);
|
Array.Copy(bts, 8, Memo, 0, 4);
|
KeyNum = BitConverter.ToInt32(bts, 12);
|
PackLen = BitConverter.ToInt32(bts, 16);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
public byte[] Head
|
{
|
get { return _head.ToArray(); }
|
}
|
private byte[] _head = new byte[4] { 0xa5, 0xa5, 0xa5, 0xa5, };
|
|
/// <summary>
|
///
|
/// </summary>
|
public eDatagramType Type { get; set; }
|
|
/// <summary>
|
///
|
/// </summary>
|
public byte[] Memo { get; set; } = new byte[4];
|
|
/// <summary>
|
///
|
/// </summary>
|
public int KeyNum { get; set; }
|
|
/// <summary>
|
///
|
/// </summary>
|
public int PackLen { get; set; }
|
|
/// <summary>
|
///
|
/// </summary>
|
public static Header Get(byte[] bts)
|
{
|
if (bts == null)
|
return null;
|
if (bts.Count() != 20)
|
return null;
|
if (bts[0] != 0xa5)
|
return null;
|
if (bts[1] != 0xa5)
|
return null;
|
if (bts[2] != 0xa5)
|
return null;
|
if (bts[3] != 0xa5)
|
return null;
|
return new Header(bts);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
public byte[] GetBuffer()
|
{
|
var bts = new byte[20];
|
Array.Copy(Head, 0, bts, 0, 4);
|
var typeBuffer = BitConverter.GetBytes((int)Type);
|
Array.Copy(typeBuffer, 0, bts, 4, 4);
|
Array.Copy(Memo, 0, bts, 8, 4);
|
var keyNumBuffer = BitConverter.GetBytes(KeyNum);
|
Array.Copy(keyNumBuffer, 0, bts, 12, 4);
|
var packLenBuffer = BitConverter.GetBytes(PackLen);
|
Array.Copy(packLenBuffer, 0, bts, 16, 4);
|
return bts;
|
}
|
|
|
|
|
}
|
}
|