public static byte[] ConvertToByte(SendMessage message) { List <byte> list = new List <byte>(); list.AddRange(Head); list.AddRange(message.ClientCode); list.Add(message.CenterCode); list.AddRange(TimeToByte(message.SendTime)); list.AddRange(BCDUtil.ConvertFrom(message.Serial.ToString(), 2)); list.AddRange(BytesUtil.ToHexArray(message.FunctionCode)); int DataLength = (message.Body == null) ? 0 : message.Body.Length; string DataLengthStr = "8" + DataLength.ToString("X3"); list.AddRange(BytesUtil.ToHexArray(DataLengthStr)); list.Add(BodyStart); if (message.Body != null) { list.AddRange(message.Body); } byte[] CRC = CRCUtil.CRC16(list.ToArray()); list.Add(Tail); list.AddRange(CRC); return(list.ToArray()); }
/// <summary> /// 湿度 01H N(5,2) 1AH 百分比 /// 温度 02H N(5,2) 1AH 摄氏度 /// 湿度阈值 04H N(5,2) 1AH 百分比 /// 温度阈值 05H N(5,2) 1AH 摄氏度 /// 温湿度/温湿度阈值,BCD码,3字节 /// 示例:01 1A 00 88 66 02 1A 00 12 34 /// 04 1A 00 55 66 05 1A 00 66 55 /// </summary> /// <param name="data"></param> /// <returns></returns> public static double Humiture(byte[] data) { byte[] temp = BytesUtil.SubBytes(data, 2, 3); string bcdStr = BCDUtil.ConvertTo(temp); return(Convert.ToInt64(bcdStr) / 100.0); }
/// <summary> /// F3H N(4) 10H /// min 上报时间间隔,BCD码,2字节 /// 示例:F3 10 00 60 /// </summary> /// <param name="data"></param> /// <returns></returns> public static int Interval(byte[] data) { byte[] interval = new byte[2]; Array.Copy(data, 2, interval, 0, 2); return(Convert.ToInt16(BCDUtil.ConvertTo(interval))); }
/// <summary> /// F2H N(18) 48H /// 中心站1主信道类型及地址TCP协议,IPV4,BCD码,9字节 255255255255 000000,分别表示IP和端口 /// 示例:F2 48 06 01 23 45 67 89 00 60 12 结果 IP:060.123.456.789,端口:006012 /// </summary> /// <returns></returns> public static string IPPort(byte[] data) { byte[] ipPort = new byte[9]; Array.Copy(data, 2, ipPort, 0, 9); return(BCDUtil.ConvertTo(ipPort)); }
/// <summary> /// F1H N(12) 30H /// 遥测站地址,BCD码,6字节 /// 示例:F1 28 00 12 34 56 78 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string ClientCode(byte[] data) { byte[] client = new byte[6]; Array.Copy(data, 2, client, 0, 6); return(BCDUtil.ConvertTo(client)); }
//获取上下行标识及报文长度 //分两个字节,代表数据长度 19-20位 public int DataLength(int position) { byte[] length = new byte[2]; Array.Copy(Data, position, length, 0, 2); string lengthStr = BCDUtil.ConvertToHex(length); return(Convert.ToInt32(lengthStr.Substring(1), 16)); }
public static int GetDataLength(byte[] data, out int headLength) { headLength = GetStartPosition(data); if (data.Length < headLength + 19) { return(headLength + 43); } byte[] length = new byte[2]; Array.Copy(data, headLength + 18, length, 0, 2); string lengthStr = BCDUtil.ConvertToHex(length); return(Convert.ToInt32(lengthStr.Substring(1), 16) + 24); }
public byte[] ToByte() { List <byte> list = new List <byte>(); list.AddRange(new byte[] { 0x08, 0x08 }); list.AddRange(BytesUtil.ToHexArray(Code)); list.AddRange(new byte[] { 0x04, 0x1A }); string _humLimit = (Math.Round(HumidityLimit, 2) * 100).ToString().PadLeft(6, '0'); list.AddRange(BCDUtil.ConvertFrom(_humLimit, 3)); list.AddRange(new byte[] { 0x05, 0x1A }); string _temLimit = (Math.Round(TemperatureLimit, 2) * 100).ToString().PadLeft(6, '0'); list.AddRange(BCDUtil.ConvertFrom(_temLimit, 3)); return(list.ToArray()); }
private static byte[] TimeToByte(DateTime time) { string timeStr = string.Format("{0}{1}{2}{3}{4}{5}", time.Year.ToString().Substring(2), ZeroFill(time.Month), ZeroFill(time.Day), ZeroFill(time.Hour), ZeroFill(time.Minute), ZeroFill(time.Second)); return(BCDUtil.ConvertFrom(timeStr, 6)); }
/// <summary> /// 08H N(2) 08H /// 主从机地址,1字节(其中地址FF表示主机) /// 示例:08 08 FF /// </summary> /// <param name="data"></param> /// <returns></returns> public static string Code(byte[] data) { return(BCDUtil.ConvertToHex(data[2])); }