/// <summary> /// Writes UInt32 data into internal buffer. /// </summary> /// <param name="data">UInt32 data to write.</param> public void Write(UInt32 data) { var bytes = data.GetBytes(); _data.Add(Integer); var length = GetLength(bytes.Length); WriteBytes(length); WriteBytes(bytes); }
/// <summary> /// Writes UInt32 data into internal buffer. /// </summary> /// <param name="data">UInt32 data to write.</param> public void Write(UInt32 data) { var bytes = data.GetBytes(); this._data.Add(INTEGER); var length = this.GetLength(bytes.Length); this.WriteBytes(length); this.WriteBytes(bytes); }
/// <summary> /// Writes uint32 data into internal buffer. /// </summary> /// <param name="data">uint32 data to write.</param> protected void Write(UInt32 data) { this.Write(data.GetBytes()); }
private List <byte[]> CreatePackage(UInt16 addr, UInt32 header, UInt32 footer, UInt16 id, UInt16 commandId, byte[] data) { List <byte[]> packageItems = new List <byte[]>(); if (data != null) { List <byte[]> dataItems = data.Split(SINGLE_PACKAGE_LENGTH, false, 0x00); UInt16 packageNum = 1; foreach (var item in dataItems) { byte[] packageItem = new byte[25 + item.Length]; int position = 0; // 数据包头[0 - 2] 0xAA8E42 3字节,高字节在前 Array.Copy(header.GetBytes(), 1, packageItem, 0, 3); position += 3; //数据包总长度(LEN)[3-4] 2字节,高字节在前 Array.Copy(((UInt16)packageItem.Length).GetBytes(), 0, packageItem, position, 2); position += 2; //设备地址[5 - 6] 2字节 高字节为地址X,低字节为地址Y Array.Copy(addr.GetBytes(), 0, packageItem, position, 2); position += 2; //数据包识别号[7-8] 2字节,用于识别是哪个设备发出的数据包 Array.Copy(id.GetBytes(), 0, packageItem, position, 2); position += 2; //命令号[9 - 10] 2字节 高字节在前 区分不同的命令 Array.Copy(commandId.GetBytes(), 0, packageItem, position, 2); position += 2; //预留字段[11 - 14] 4字节 默认为0 Array.Copy(((UInt32)0).GetBytes(), 0, packageItem, position, 4); position += 4; //总包数[15-16] 2字节 取值范围1到65535 单包命令默认为1 Array.Copy(((UInt16)dataItems.Count).GetBytes(), 0, packageItem, position, 2); position += 2; //当前包序号[17-18] 2字节 取值范围1到65535 单包命令默认为1,序号从1开始计数。 Array.Copy(packageNum.GetBytes(), 0, packageItem, position, 2); position += 2; //命令执行结果[19] 1字节 //1 =成功 //2 = 忙碌中 //3 = 未知命令 //4 = 校验错误 //5 = 长度错误 //6 = 地址错误 MCU反馈命令执行情况 packageItem[position] = 0; position += 1; //数据段[20到LEN - 6] //数据段以外的长度为25字节。 //可用来保存上位机发给下位机的参数,也可以用来保存下位机返回给上位机的数据。当数据段长度为0时,表示该命令不需要传输数据 Array.Copy(item, 0, packageItem, position, item.Length); position += item.Length; //校验码[LEN-5 到LEN-4] 0-65535,高字节在前 采用累加和校验法,将字节序号3到序号(LEN-6)的所有数据相加,取低16位,高8位在前 UInt16 checkSum = (UInt16)(packageItem.CheckSumForUInt32(3, packageItem.Length - 6) & 0xFFFF); Array.Copy(checkSum.GetBytes(), 0, packageItem, position, 2); position += 2; //数据包尾[LEN-3到LEN-1] 0x5571BD 3字节,高字节在前 Array.Copy(footer.GetBytes(), 1, packageItem, position, 3); position += 2; packageItems.Add(packageItem); packageNum++; } } else { byte[] packageItem = new byte[25]; int position = 0; // 数据包头[0 - 2] 0xAA8E42 3字节,高字节在前 Array.Copy(header.GetBytes(), 1, packageItem, 0, 3); position += 3; //数据包总长度(LEN)[3-4] 2字节,高字节在前 Array.Copy(((UInt16)packageItem.Length).GetBytes(), 0, packageItem, position, 2); position += 2; //设备地址[5 - 6] 2字节 高字节为地址X,低字节为地址Y Array.Copy(addr.GetBytes(), 0, packageItem, position, 2); position += 2; //数据包识别号[7-8] 2字节,用于识别是哪个设备发出的数据包 Array.Copy(id.GetBytes(), 0, packageItem, position, 2); position += 2; //命令号[9 - 10] 2字节 高字节在前 区分不同的命令 Array.Copy(commandId.GetBytes(), 0, packageItem, position, 2); position += 2; //预留字段[11 - 14] 4字节 默认为0 Array.Copy(((UInt32)0).GetBytes(), 0, packageItem, position, 4); position += 4; //总包数[15-16] 2字节 取值范围1到65535 单包命令默认为1 Array.Copy(((UInt16)1).GetBytes(), 0, packageItem, position, 2); position += 2; //当前包序号[17-18] 2字节 取值范围1到65535 单包命令默认为1,序号从1开始计数。 Array.Copy(((UInt16)(1)).GetBytes(), 0, packageItem, position, 2); position += 2; //命令执行结果[19] 1字节 //1 =成功 //2 = 忙碌中 //3 = 未知命令 //4 = 校验错误 //5 = 长度错误 //6 = 地址错误 MCU反馈命令执行情况 packageItem[position] = 1; position += 1; //校验码[LEN-5 到LEN-4] 0-65535,高字节在前 采用累加和校验法,将字节序号3到序号(LEN-6)的所有数据相加,取低16位,高8位在前 UInt16 checkSum = packageItem.CheckSumForUInt16(3, packageItem.Length - 6); Array.Copy(checkSum.GetBytes(), 0, packageItem, position, 2); position += 2; //数据包尾[LEN-3到LEN-1] 0x5571BD 3字节,高字节在前 Array.Copy(footer.GetBytes(), 1, packageItem, position, 3); position += 2; packageItems.Add(packageItem); } return(packageItems); }
public void IntegerX_GetBytesUInt32_Test(byte[] data, UInt32 value) { Assert.True(data.CompareWith(0, value.GetBytes(isLittleEndian: true), 0, data.Length) == 0); }
public static IEnumerable <byte> Concat(this IEnumerable <byte> sequence, UInt32 value) { return(sequence.Concat(value.GetBytes())); }
/// <summary> /// <para>Изменить порядок байт</para> /// </summary> /// <param name="value">Исходное значение</param> /// <returns>Результируюущее значение</returns> public static UInt32 Swap(this UInt32 value) { return(LoadUInt32(value.GetBytes(true), 0, false)); }
public BinaryWriter Write(UInt32 value) { byteList.AddRange(value.GetBytes()); return(this); }