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

78 lines
2.4 KiB

using System;
using GPSBusiness.Helper;
namespace GPSBusiness.BBGPSStandard
{
public class DataHead
{
public static int intSerialNo = 0;
/// <summary>
/// ��ȡ��ˮ��
/// </summary>
/// <returns></returns>
public int GetSerialNo()
{
if (intSerialNo >= 255)
{
intSerialNo = 0;
}
intSerialNo++;
return intSerialNo;
}
/// <summary>
/// ��ϢID
/// </summary>
public UInt16 DataID;
/// <summary>
/// ��Ϣ������
/// </summary>
public UInt16 DataAttribute;
/// <summary>
/// �ն��ֻ���=centerid+mdtid
/// </summary>
public UInt16 CenterID;
public byte[] MdtID;
/// <summary>
/// ��Ϣ��ˮ��
/// </summary>
public UInt16 DataSerialNo;
/// <summary>
/// ��Ϣ��
/// </summary>
public byte[] DataContent;
public byte[] GetBytes()
{
this.DataAttribute = (UInt16)this.DataContent.Length;
ByteBuilder bb = new ByteBuilder();
bb.Append(ByteConvert.ToBytes(this.DataID));
bb.Append(ByteConvert.ToBytes(this.DataAttribute));
bb.Append(ByteConvert.ToBytes(this.CenterID));
bb.Append(this.MdtID);
bb.Append(ByteConvert.ToBytes(this.DataSerialNo));
if (this.DataContent.Length > 0)
{
bb.Append(this.DataContent);
}
byte Crc = ByteConvert.GetCRC(bb.GetBytes(), 0, bb.GetBytes().Length);
ByteBuilder bb2 = new ByteBuilder();
bb2.Append(0x7e);
bb2.Append(bb.GetBytes());
bb2.Append(Crc);
bb2.Append(0x7e);
return bb2.GetBytes();
}
public void Frombytes(DataHead model, byte[] buffer)
{
model.DataID = ByteConvert.ToUInt16(buffer, 1);
model.DataAttribute = ByteConvert.ToUInt16(buffer, 3);//buffer.Length - 15
model.CenterID = ByteConvert.ToUInt16(buffer, 5);
model.MdtID = ByteConvert.ToBytes(buffer, 7, 4);
model.DataSerialNo = ByteConvert.ToUInt16(buffer, 11);
if (buffer.Length > 15)
{
//��Ϣ������ �ݶ�Ϊ��Ϣ�峤��
model.DataContent = ByteConvert.ToBytes(buffer, 13, this.DataAttribute);
}
}
}
}