//心跳 //7A 7A 01 00 02 00 0E 00 31 32 33 34 35 36 37 38 17 10 31 17 34 00 C9 6A 7B 7B //7a 7a 01 00 02 00 07 00 01 17 10 31 17 38 29 27 cb 7b 7b static public void ReceiveDispose_Heartbeat(byte[] data, TcpSocketClient client) { try { //设备编号 byte[] sn = new byte[8]; //设备编号 for (int i = 0; i < 8; i++) { sn[i] = data[i]; } string DeviceNo = Encoding.ASCII.GetString(sn); //获取设备编号ASCII DateTime dateTime = DateTime.Now; //RTC string tStr = ConvertData.ToHexString(data, 8, 6); try { dateTime = DateTime.ParseExact(tStr, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture); } catch (Exception) { dateTime = DateTime.Now; } //拼接应答 byte[] result = SendJoint_Heartbeat(); if (result != null) { client.SendBuffer(result); } //更新redis DPC.Discharge_operation.Update_equminet_last_online_time(DeviceNo, DPC_Tool.GetTimeStamp(dateTime)); } catch (Exception) { } }
/// <summary> /// 实时数据 /// </summary> /// <param name="b"></param> /// 发送7e 7e 0e 01 00 12 34 56 00 00 00 00 17 09 27 10 12 10 01 00 00 00 02 00 00 00 03 00 04 00 05 00 06 00 01 00 02 00 03 00 04 00 05 00 02 00 00 00 00 00 00 01 00 02 00 03 00 00 00 04 00 00 00 00 7d 7d public static Byte[] OnResolveRealData(byte[] b, int bCount, TcpSocketClient client) { string tStr = ConvertData.ToHexString(b, 0, 2); if (tStr != "7E7E") { return(null); } Zhgd_iot_tower_current data = new Zhgd_iot_tower_current(); #region 原协议 //设备号 data.sn = ConvertData.ToHexString(b, 5, 3); //司机卡号 data.driver_id_code = ConvertData.ToHexString(b, 8, 4); //日期 tStr = ConvertData.ToHexString(b, 12, 6); try { data.@timestamp = DPC_Tool.GetTimeStamp(DateTime.ParseExact(tStr, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture)); } catch { data.@timestamp = DPC_Tool.GetTimeStamp(); } IntValue iv = new IntValue(); //经度 iv.bValue1 = b[18]; iv.bValue2 = b[19]; iv.bValue3 = b[20]; iv.bValue4 = b[21]; //data.Current.Longitude = (iv.iValue / 100000.0).ToString("0.00"); //纬度 iv.bValue1 = b[22]; iv.bValue2 = b[23]; iv.bValue3 = b[24]; iv.bValue4 = b[25]; //data.Current.Latitude = (iv.iValue / 100000.0).ToString("0.00"); UShortValue s = new UShortValue(); //高度 s.bValue1 = b[26]; s.bValue2 = b[27]; data.height = double.Parse((s.sValue / 100.00).ToString("0.00")); //幅度 s.bValue1 = b[28]; s.bValue2 = b[29]; data.range = double.Parse((s.sValue / 100.00).ToString("0.00")); ShortValue sv = new ShortValue(); //转角 sv.bValue1 = b[30]; sv.bValue2 = b[31]; data.rotation = double.Parse((sv.sValue / 10.00).ToString("0.00")); //重量 s.bValue1 = b[32]; s.bValue2 = b[33]; data.weight = double.Parse((s.sValue / 100.00).ToString("0.00")); //风速 s.bValue1 = b[34]; s.bValue2 = b[35]; data.wind_speed = double.Parse((s.sValue / 100.00).ToString("0.00")); data.wind_grade = ConvertWind.WindToLeve(s.sValue / 100.0f); if (data.wind_grade >= 13) { data.wind_grade = 12; } //倾角X sv.bValue1 = b[36]; sv.bValue2 = b[37]; data.dip_x = double.Parse((sv.sValue / 100.00).ToString("0.00")); //倾角Y sv.bValue1 = b[38]; sv.bValue2 = b[39]; data.dip_y = double.Parse((sv.sValue / 100.00).ToString("0.00")); //安全力矩 s.bValue1 = b[40]; s.bValue2 = b[41]; //data.Current.Safetorque = (s.sValue / 10.00).ToString("0.00"); //安全起重量 s.bValue1 = b[42]; s.bValue2 = b[43]; //data.Current.SafeWeight = (s.sValue / 100.00).ToString("0.00"); //倍率 //data.Current.Times = Convert.ToInt32(b[44]).ToString(); //限位控制器状态 //s.bValue1 = b[51]; //s.bValue2 = b[52]; //data.Current.LimitStatus = Convert.ToString(s.sValue, 2).PadLeft(16, '0'); //LimitFlag(data, data.Current.LimitStatus); ////传感器状态 //s.bValue1 = b[53]; //s.bValue2 = b[54]; //data.Current.SensorStatus = Convert.ToString(s.sValue, 2).PadLeft(16, '0'); //预警告码 IntValue i = new IntValue(); //i.bValue1 = b[55]; //i.bValue2 = b[56]; //i.bValue3 = b[57]; //i.bValue4 = b[58]; //string WarnType = Convert.ToString(i.iValue, 2).PadLeft(32, '0'); //WarnFlag(data, WarnType); //报警告码 i.bValue1 = b[59]; i.bValue2 = b[60]; i.bValue3 = b[61]; i.bValue4 = b[62]; string AlarmType = Convert.ToString(i.iValue, 2).PadLeft(32, '0'); //总共是32位右对齐 AlarmFlag(data, AlarmType); //报警码解析 tStr = ConvertData.ToHexString(b, 64, 2); if (tStr != "7D7D") { return(null); } #endregion //进行数据put Tower_operation.Send_tower_Current(data); return(null); }
/// <summary> /// 心跳 /// </summary> /// <param name="b"></param> /// 发送7e 7e 0e 00 00 12 34 56 17 09 27 10 10 10 00 7d 7d /// 接收7e 7e 0e 00 07 01 17 09 27 10 38 42 00 7d 7d private static byte[] OnResolveHeabert(byte[] b, int bCount) { DateTime dt = DateTime.Now; if (bCount != 17) { return(null); } string tStr = ConvertData.ToHexString(b, 0, 2); if (tStr != "7E7E") { return(null); } //设备号 string SN = ConvertData.ToHexString(b, 5, 3); long dttemp = DPC_Tool.GetTimeStamp(DateTime.Now); byte[] rb = new byte[15]; //时间 tStr = ConvertData.ToHexString(b, 8, 6); try { rb = new byte[9]; rb[4] = 0x01; //应用数据区数据长度 rb[5] = 0x00; //1:校准时间 rb[7] = 0x7D; rb[8] = 0x7D; DateTime getdate = DateTime.ParseExact(tStr, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture); double compare = (dt - getdate).TotalMinutes; if (compare > 1 || compare < 0) { throw new Exception(); } string time = DateTime.ParseExact(tStr, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd HH:mm:ss"); dttemp = DPC_Tool.GetTimeStamp(DateTime.Parse(time)); } catch { rb = new byte[15]; rb[4] = 0x07; //应用数据区数据长度 rb[5] = 0x01; //1:校准时间 rb[6] = byte.Parse(dt.Year.ToString().Substring(2, 2), System.Globalization.NumberStyles.HexNumber); rb[7] = byte.Parse(dt.Month.ToString(), System.Globalization.NumberStyles.HexNumber); rb[8] = byte.Parse(dt.Day.ToString(), System.Globalization.NumberStyles.HexNumber); rb[9] = byte.Parse(dt.Hour.ToString(), System.Globalization.NumberStyles.HexNumber); rb[10] = byte.Parse(dt.Minute.ToString(), System.Globalization.NumberStyles.HexNumber); rb[11] = byte.Parse(dt.Second.ToString(), System.Globalization.NumberStyles.HexNumber); rb[13] = 0x7D; rb[14] = 0x7D; string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); dttemp = DPC_Tool.GetTimeStamp(DateTime.Parse(time)); } rb[0] = 0x7E; rb[1] = 0x7E; rb[2] = 0x0E; rb[3] = 0x00;//功能码 //更新redis DPC.Tower_operation.Update_equminet_last_online_time(SN, dttemp); return(rb); }
//实时数据 //7A 7A 01 00 02 01 15 00 31 32 33 34 35 36 37 38 17 10 31 17 34 00 01 00 02 00 03 00 01 15 03 7B 7B //有报警7A 7A 01 00 02 01 15 00 31 32 33 34 35 36 37 38 17 10 31 17 34 00 01 00 02 00 03 00 0f DB E2 7B 7B static public void ReceiveDispose_Current(byte[] data, TcpSocketClient client, bool isCurrent) { try { Zhgd_iot_discharge_current dataFrame = new Zhgd_iot_discharge_current(); //设备编号 byte[] sn = new byte[8]; //设备编号 for (int i = 0; i < 8; i++) { sn[i] = data[i]; } dataFrame.sn = Encoding.ASCII.GetString(sn); //获取设备编号ASCII //RTC string tStr = ConvertData.ToHexString(data, 8, 6); try { dataFrame.timestamp = DPC_Tool.GetTimeStamp(DateTime.ParseExact(tStr, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture)); } catch (Exception) { dataFrame.timestamp = DPC_Tool.GetTimeStamp(DateTime.Now); } //当前载重 UShortValue us = new UShortValue(); us.bValue1 = data[14]; us.bValue2 = data[15]; dataFrame.weight = us.sValue; //倾角X ShortValue s = new ShortValue(); s.bValue1 = data[16]; s.bValue2 = data[17]; dataFrame.dip_x = (double)((double)(s.sValue) / 100d); //倾角Y s.bValue1 = data[18]; s.bValue2 = data[19]; dataFrame.dip_y = (double)((double)(s.sValue) / 100d); //报警状态 string state = Convert.ToString(data[20], 2).PadLeft(8, '0'); List <string> vs = new List <string>(); dataFrame.is_warning = "N"; // dataFrame.WeightWarning = (byte)(state[7] - 48); if ((byte)(state[6] - 48) == 1) { vs.Add(Warning_type.重量告警); dataFrame.is_warning = "Y"; } //dataFrame.WeightAlarm = (byte)(state[6] - 48); // dataFrame.AngleWarning = (byte)(state[5] - 48); if ((byte)(state[4] - 48) == 1) { vs.Add(Warning_type.重量告警); dataFrame.is_warning = "Y"; } //dataFrame.AngleAlarm = (byte)(state[4] - 48); //离线数据应答 if (!isCurrent) { byte[] result = SendJoint_OffLine(); if (result != null) { client.SendBuffer(result); } } //进行数据put Discharge_operation.Send_discharge_Current(dataFrame); } catch (Exception) { } }
/// <summary> /// 心跳接收数据 /// </summary> /// <param name="b"></param> public static byte[] OnResolveHeabert(byte[] b, int bCount, TcpSocketClient client)//1.2.1 { if (bCount != 0x1A) { return(null); } if (BitConverter.ToUInt16(b, 22) != ConvertData.CRC16(b, 8, 14))//检验和 { return(null); } string tStr = ConvertData.ToHexString(b, 0, 2);//获取帧头 if (tStr != "7A7A") { return(null); } byte[] t = new byte[8]; for (int i = 8, j = 0; i < 16; i++, j++) { t[j] = b[i]; } string SN = Encoding.ASCII.GetString(t); tStr = ConvertData.ToHexString(b, 16, 6);//获取时间 DateTime getdate = new DateTime(); byte[] bytes = new byte[19]; try { getdate = DateTime.ParseExact(tStr, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture); } catch (Exception ex) { bytes[0] = 0x7A; bytes[1] = 0x7A; //协议版本 bytes[2] = 0x01; bytes[3] = 0x04; bytes[4] = 0x00; //命令字 bytes[5] = 0x00; //数据长度 bytes[6] = 0x07; bytes[7] = 0x00; //////////时间校准标示//// bytes[8] = 0x01; DateTime nowTime = DateTime.Now; //时间 bytes[9] = byte.Parse(nowTime.Year.ToString().Substring(2, 2), System.Globalization.NumberStyles.HexNumber); bytes[10] = byte.Parse(nowTime.Month.ToString(), System.Globalization.NumberStyles.HexNumber); bytes[11] = byte.Parse(nowTime.Day.ToString(), System.Globalization.NumberStyles.HexNumber); bytes[12] = byte.Parse(nowTime.Hour.ToString(), System.Globalization.NumberStyles.HexNumber); bytes[13] = byte.Parse(nowTime.Minute.ToString(), System.Globalization.NumberStyles.HexNumber); bytes[14] = byte.Parse(nowTime.Second.ToString(), System.Globalization.NumberStyles.HexNumber); //校验和 byte[] crc16 = BitConverter.GetBytes(ConvertData.CRC16(bytes, 8, 7)); bytes[15] = crc16[0];//0x00;//算校验和 bytes[16] = crc16[1]; //结束 bytes[17] = 0x7B; bytes[18] = 0x7B; //更新redis DPC.Lift_operation.Update_equminet_last_online_time(SN, DPC_Tool.GetTimeStamp(nowTime)); return(bytes); } DateTime now = System.DateTime.Now; double compare = (now - getdate).TotalMinutes; if (compare > 1 || compare < 0) //需要 { bytes = new byte[19]; //数据长度 bytes[6] = 0x07; bytes[7] = 0x00; //////////时间校准标示//// bytes[8] = 0x01; //时间 bytes[9] = byte.Parse(now.Year.ToString().Substring(2, 2), System.Globalization.NumberStyles.HexNumber); bytes[10] = byte.Parse(now.Month.ToString(), System.Globalization.NumberStyles.HexNumber); bytes[11] = byte.Parse(now.Day.ToString(), System.Globalization.NumberStyles.HexNumber); bytes[12] = byte.Parse(now.Hour.ToString(), System.Globalization.NumberStyles.HexNumber); bytes[13] = byte.Parse(now.Minute.ToString(), System.Globalization.NumberStyles.HexNumber); bytes[14] = byte.Parse(now.Second.ToString(), System.Globalization.NumberStyles.HexNumber); //校验和 byte[] crc16 = BitConverter.GetBytes(ConvertData.CRC16(bytes, 8, 7)); bytes[15] = crc16[0];//0x00;//算校验和 bytes[16] = crc16[1]; //结束 bytes[17] = 0x7B; bytes[18] = 0x7B; getdate = DateTime.Now; ToolAPI.XMLOperation.WriteLogXmlNoTail("时间校验V1.4.0", "包内解析时间:" + getdate + "当前时间:" + now); } else //不需要 { bytes = new byte[13]; //长度 bytes[6] = 0x01; bytes[7] = 0x00; //数据区 bytes[8] = 0x00; //校验和 byte[] crc16 = BitConverter.GetBytes(ConvertData.CRC16(bytes, 8, 1)); bytes[9] = crc16[0];//0x00;//算校验和 bytes[10] = crc16[1]; //结束 bytes[11] = 0x7B; bytes[12] = 0x7B; } bytes[0] = 0x7A; bytes[1] = 0x7A; //协议版本 bytes[2] = 0x01; bytes[3] = 0x04; bytes[4] = 0x00; //命令字 bytes[5] = 0x00; //更新redis DPC.Lift_operation.Update_equminet_last_online_time(SN, DPC_Tool.GetTimeStamp(getdate)); return(bytes); }
/// <summary> /// 实时数据 /// </summary> /// <param name="b"></param> /// <param name="bCount"></param> /// <param name="client"></param> /// <param name="df"></param> public static void OnResolveCurrent(byte[] b, int bCount, TcpSocketClient client) { string tStr = ConvertData.ToHexString(b, 0, 2); if (tStr != "7A7A") { return; } Zhgd_iot_dust_noise_current current = new Zhgd_iot_dust_noise_current(); byte[] t = new byte[8]; for (int i = 8, j = 0; i < 16; i++, j++) { t[j] = b[i]; } current.sn = Encoding.ASCII.GetString(t); tStr = ConvertData.ToHexString(b, 16, 6); try { current.timestamp = DPC_Tool.GetTimeStamp(DateTime.ParseExact(tStr, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture)); } catch { current.timestamp = DPC_Tool.GetTimeStamp(DateTime.Now); } UShortValue s = new UShortValue(); s.bValue1 = b[22]; s.bValue2 = b[23]; if (s.sValue.ToString() == "0") { current.pm2_5 = 5; } else { current.pm2_5 = s.sValue; } s.bValue1 = b[24]; s.bValue2 = b[25]; if (s.sValue.ToString() == "0") { current.pm10 = 5; } else { current.pm10 = s.sValue; } s.bValue1 = b[26]; s.bValue2 = b[27]; current.noise = double.Parse((float.Parse(s.sValue.ToString()) / 10).ToString("0.0")); s.bValue1 = b[28]; s.bValue2 = b[29]; current.temperature = double.Parse((float.Parse(s.sValue.ToString()) / 10).ToString("0.0")); s.bValue1 = b[30]; s.bValue2 = b[31]; current.humidity = double.Parse((float.Parse(s.sValue.ToString()) / 10).ToString("0.0")); s.bValue1 = b[32]; s.bValue2 = b[33]; current.wind_speed = double.Parse((float.Parse(s.sValue.ToString()) / 10).ToString("0.0")); s.bValue1 = b[34]; s.bValue2 = b[35]; current.wind_direction = s.sValue; // current.GprsSignal = ((sbyte)b[36]).ToString(); // current.automatic = b[37].ToString(); //current.Manual = b[38].ToString(); s.bValue1 = b[39]; s.bValue2 = b[40]; current.air_pressure = double.Parse((float.Parse(s.sValue.ToString()) / 100).ToString("0.00")); s.bValue1 = b[41]; s.bValue2 = b[42]; current.tsp = s.sValue; Dust_noise_operation.Send_dust_noise_Current(current); }