//任务 #region //xd300发送任务 public static void Polling_XD300Send() { for (int i = 0; i < _XD300Buffer.Length; i++) { for (int j = 0; j < _XD300Buffer[i]._Command.Length; j++) { //判断命令是否开启 if (_XD300Buffer[i]._Command[j]._onoff == false) { continue; } //判断是否已发送 if (_XD300Buffer[i]._Command[j]._send == false) { //检测ISocketRS是否被占用和是否连接 Gprs.GprsList gl = Gprs.Get_GprsList(_XD300Buffer[i]._Info._ip); if (gl._Iscon == false || gl._Isbusy == true || gl._activate == false) { continue; } bool send_flg = Gprs.Gprs_send(gl, _XD300Buffer[i]._Command[j]._cmd); if (send_flg == true) { _XD300Buffer[i]._Command[j]._send = true; _XD300Buffer[i]._Command[j]._back = false; _XD300Buffer[i]._Command[j]._timeoutnow = 0; _XD300Buffer[i]._Command[j]._senddt = DateTime.Now; _XD300Buffer[i]._LastCommandIndex = j; //占用ISocketRS Gprs.Gprs_IsOccupy(_XD300Buffer[i]._Info._ip, true); } } //发送完成 开始计时 计算超时时间 else { _XD300Buffer[i]._Command[j]._timeoutnow++; } //判断超时 if (_XD300Buffer[i]._Command[j]._timeoutnow >= _XD300Buffer[i]._Info._timeout) { _XD300Buffer[i]._Command[j]._timeoutnow = 0; _XD300Buffer[i]._Command[j]._send = false; //解除ISocketRS占用 Gprs.Gprs_IsOccupy(_XD300Buffer[i]._Info._ip, false); _XD300Buffer[i]._Command[j]._retrytimesnow++; } //判断重试次数 超过次数 设备故障 if (_XD300Buffer[i]._Command[j]._retrytimesnow >= _XD300Buffer[i]._Info._retrytimes) { _XD300Buffer[i]._Command[j]._onoff = false; _XD300Buffer[i]._Command[j]._retrytimesnow = 0; _XD300Buffer[i]._Info._state = false; } } } }
//发送心跳包 public static void Polling_HeatbeatSend() { for (int i = 0; i < _GprsList.Length; i++) { //检测是否到达心跳周期 if ((DateTime.Now - _GprsList[i]._lasttime).TotalSeconds >= 300) { //检测ISocketRS是否被占用和是否连接 if (_GprsList[i]._Iscon == false || _GprsList[i]._Isbusy == true || _GprsList[i]._activate == false) { continue; } byte[] buffer = new System.Text.UnicodeEncoding().GetBytes(_GprsList[i]._heatbeat); bool send_flg = Gprs.Gprs_send(_GprsList[i], buffer); if (send_flg == true) { _GprsList[i]._lasttime = DateTime.Now; } } } }
//数据处理逻辑 #region //xd100n数据处理 public static void Deal_XD100nData(byte[] inbyte, string ip) { //检测是否为xd100n数据 //判断依据 1、CRC 2、ip 3、器件地址 if (false == DataInfo.CheckCRC(inbyte)) { return; } int busfferlistID = -1; for (int i = 0; i < xd100x._XD100xBuffer.Length; i++) { if (xd100x._XD100xBuffer[i]._Info._ip == ip && xd100x._XD100xBuffer[i]._Info._addr == inbyte[0]) { busfferlistID = i; break; } } if (busfferlistID == -1) { return; } if (xd100x._XD100xBuffer[busfferlistID]._Info._version != "xd100n") { return; } //实时数据模拟量 功能码0x04 if (inbyte[1] == 0x04 && inbyte[2] == 0x64) { xd100x._XD100xBuffer[busfferlistID]._Data = Read_ai(inbyte); xd100x._XD100xBuffer[busfferlistID]._SaveDatas = true; //2012 if (SocketServer.Program.basedeviceid == xd100x._XD100xBuffer[busfferlistID]._Info._id) { SocketServer.Program.baseouttemp = Tool.xd100x._XD100xBuffer[busfferlistID]._Data._OT; SocketServer.Program.isseted = false; } } //实时数据数字量 功能码0x02 //if (inbyte[1] == 0x02 && inbyte[2] == 0x04) //{ // xd100x._XD100xBuffer[busfferlistID]._Data._alarm = Get_alarmstate(inbyte); // xd100x._XD100xBuffer[busfferlistID]._Data._pump = Get_pumpstate(inbyte); // xd100x._XD100xBuffer[busfferlistID]._SaveDatas = true; //} //03可读返回 if (inbyte[1] == 0x03) { //控制方法114 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 1) { xd100x._XD100xBuffer[busfferlistID]._Set._valvecontrol = Read_valvecontrol(inbyte); } //设定值116 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 2) { xd100x._XD100xBuffer[busfferlistID]._Set._valvevalue = Read_valvevalue(inbyte); } //曲线117 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 3) { xd100x._XD100xBuffer[busfferlistID]._Set._valveline = Read_valveline(inbyte); } //分时调整133 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 4) { xd100x._XD100xBuffer[busfferlistID]._Set._valvetime = Read_valvetime(inbyte); } //开度最大最小值145 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 5) { xd100x._XD100xBuffer[busfferlistID]._Set._valvemm = Read_valvemm(inbyte); } //流量限定153 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 6) { xd100x._XD100xBuffer[busfferlistID]._Set._valvelimit = Read_valvelimit(inbyte); } //压差曲线 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 7) { xd100x._XD100xBuffer[busfferlistID]._Set._cycpumpline = Read_cycpumpline(inbyte); } //压差设定 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 8) { xd100x._XD100xBuffer[busfferlistID]._Set._cycpumpvalue = Read_cycpumpvalue(inbyte); } //补水压力模式 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 9) { xd100x._XD100xBuffer[busfferlistID]._Set._addpumpvalue = Read_addpumpvalue(inbyte); } //补水上下限水位上下限 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 10) { xd100x._XD100xBuffer[busfferlistID]._Set._addpumpmm = Read_addpumpmm(inbyte); } //报警198 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 11) { xd100x._XD100xBuffer[busfferlistID]._Set._alarmp = Read_alarmp(inbyte); } //报警202 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 12) { xd100x._XD100xBuffer[busfferlistID]._Set._alarmt = Read_alarmt(inbyte); } //室外温度206 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 13) { xd100x._XD100xBuffer[busfferlistID]._Set._outmode = Read_outmode(inbyte); } //室外温度修正4 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 32) { xd100x._XD100xBuffer[busfferlistID]._Set._outrevise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._outrevise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._OT; } //一次供温温度修正11 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 33) { xd100x._XD100xBuffer[busfferlistID]._Set._GT1revise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._GT1revise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._GT1; } //一次回温温度修正18 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 34) { xd100x._XD100xBuffer[busfferlistID]._Set._BT1revise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._BT1revise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._BT1; } //二次供温温度修正25 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 35) { xd100x._XD100xBuffer[busfferlistID]._Set._GT2revise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._GT2revise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._GT2; } //二次回温温度修正32 if (xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex == 36) { xd100x._XD100xBuffer[busfferlistID]._Set._BT2revise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._BT2revise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._BT2; } } //10写返回 if (inbyte[1] == 0x10) { } //急停返回 if (inbyte[1] == 0x0F) { } xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._onoff = false; xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._retrytimesnow = 0; xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._backdt = DateTime.Now; xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._back = true; xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._send = false; xd100x._XD100xBuffer[busfferlistID]._Info._state = true; //只要有数据返回即解除ISocketRS占用 Gprs.Gprs_IsOccupy(xd100x._XD100xBuffer[busfferlistID]._Info._ip, false); }
//数据处理逻辑 #region //xd100n数据处理 public static void Deal_XD100Data(byte[] inbyte, string ip) { //检测是否为xd100n数据 //判断依据 1、CRC 2、ip 3、器件地址 if (false == DataInfo.CheckCRC(inbyte)) { return; } if (inbyte[0] != 0x21 || inbyte[1] != 0x58 || inbyte[2] != 0x44 || inbyte[4] != 0xa0) { return; } int busfferlistID = -1; for (int i = 0; i < xd100x._XD100xBuffer.Length; i++) { if (xd100x._XD100xBuffer[i]._Info._ip == ip && xd100x._XD100xBuffer[i]._Info._addr == inbyte[3]) { busfferlistID = i; break; } } if (busfferlistID == -1) { return; } //实时数据 功能码30 if (inbyte[5] == 30) { xd100x._XD100xBuffer[busfferlistID]._Data = Read_ai(inbyte); xd100x._XD100xBuffer[busfferlistID]._SaveDatas = true; TimeSpan ts = DateTime.Now - Convert.ToDateTime(xd100x._XD100xBuffer[busfferlistID]._Data._dt); if (ts.TotalMinutes > 3) { if (DateTime.Now.Date != Convert.ToDateTime(xd100x._XD100xBuffer[busfferlistID]._Data._dt).Date) { Tool.xd100x._XD100xBuffer[busfferlistID]._Command[42]._cmd = Tool.xd100.Set_datetime(Tool.xd100x._XD100xBuffer[busfferlistID]._Info._addr); Tool.xd100x._XD100xBuffer[busfferlistID]._Command[42]._onoff = true; } } //2012 if (SocketServer.Program.basedeviceid == xd100x._XD100xBuffer[busfferlistID]._Info._id) { SocketServer.Program.baseouttemp = Tool.xd100x._XD100xBuffer[busfferlistID]._Data._OT; SocketServer.Program.isseted = false; } } //获取调节阀控制模式调节阀设定值 if (inbyte[5] == 20 && inbyte[7] == 0x47) { xd100x._XD100xBuffer[busfferlistID]._Set._valvecontrol = Read_valvecontrol(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._valvevalue = Read_valvevalue(inbyte); } //获取调节阀温度曲线 if (inbyte[5] == 20 && inbyte[7] == 0x48) { xd100x._XD100xBuffer[busfferlistID]._Set._valveline = Read_valveline(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._valvetime = Read_valvetime(inbyte); } //获取调节阀开度上下限 if (inbyte[5] == 20 && inbyte[7] == 0x49) { xd100x._XD100xBuffer[busfferlistID]._Set._valvemm = Read_valvemm(inbyte); } //获取压差曲线 if (inbyte[5] == 0x3E) { xd100x._XD100xBuffer[busfferlistID]._Set._cycpumpline = Read_cycpumpline(inbyte); } //获取循环泵压差设定 if (inbyte[5] == 0x40) { xd100x._XD100xBuffer[busfferlistID]._Set._cycpumpvalue = Read_cycpumpvalue(inbyte); } //获取补水泵压力设定 if (inbyte[5] == 20 && inbyte[7] == 0x4d) { xd100x._XD100xBuffer[busfferlistID]._Set._addpumpvalue = Read_addpumpvalue(inbyte); } //获取补水压力上下限 水箱水位上下限 if (inbyte[5] == 20 && inbyte[7] == 0x4f) { xd100x._XD100xBuffer[busfferlistID]._Set._addpumpmm = Read_addpumpmm(inbyte); } //获取报警设置压力 if (inbyte[5] == 20 && inbyte[7] == 0x51) { xd100x._XD100xBuffer[busfferlistID]._Set._alarmp = Read_alarmp(inbyte); } //获取报警设置温度液位 if (inbyte[5] == 20 && inbyte[7] == 0x52) { xd100x._XD100xBuffer[busfferlistID]._Set._alarmt = Read_alarmt(inbyte); } //获取室外温度模式 没有此功能 //if (inbyte[5] == 40) //{ // xd100x._XD100xBuffer[busfferlistID]._Set._outmode = Read_outmode(inbyte); //} //获取室外温度值 if (inbyte[5] == 20 && inbyte[7] == 0x1f) { xd100x._XD100xBuffer[busfferlistID]._Set._outtemp = Read_outtemp(inbyte); } //获取室外温度修正 if (inbyte[5] == 20 && inbyte[7] == 31) { xd100x._XD100xBuffer[busfferlistID]._Set._outrevise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._outrevise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._OT; } //获取室外温度修正 if (inbyte[5] == 20 && inbyte[7] == 32) { xd100x._XD100xBuffer[busfferlistID]._Set._GT1revise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._GT1revise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._GT1; } //获取室外温度修正 if (inbyte[5] == 20 && inbyte[7] == 33) { xd100x._XD100xBuffer[busfferlistID]._Set._BT1revise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._BT1revise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._BT1; } //获取室外温度修正 if (inbyte[5] == 20 && inbyte[7] == 34) { xd100x._XD100xBuffer[busfferlistID]._Set._GT2revise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._GT2revise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._GT2; } //获取室外温度修正 if (inbyte[5] == 20 && inbyte[7] == 35) { xd100x._XD100xBuffer[busfferlistID]._Set._BT2revise = Read_temprevise(inbyte); xd100x._XD100xBuffer[busfferlistID]._Set._BT2revise._temp = xd100x._XD100xBuffer[busfferlistID]._Data._BT2; } //设定成功返回 if (inbyte[5] == 10) { } //主动上报报警 if (inbyte[5] == 32) { xd100x._XD100xBuffer[busfferlistID]._Data._alarm = Read_alarmstate(inbyte); return; } xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._onoff = false; xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._retrytimesnow = 0; xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._backdt = DateTime.Now; xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._back = true; xd100x._XD100xBuffer[busfferlistID]._Command[xd100x._XD100xBuffer[busfferlistID]._LastCommandIndex]._send = false; xd100x._XD100xBuffer[busfferlistID]._Info._state = true; //只要有数据返回即解除ISocketRS占用 Gprs.Gprs_IsOccupy(xd100x._XD100xBuffer[busfferlistID]._Info._ip, false); }
//数据处理逻辑 #region //xd300数据处理 public static void Deal_XD300Data(byte[] inbyte, string ip) { //检测是否为xd300数据 //判断依据 1、CRC 2、ip 3、器件地址 //0x21 0x58 0x44 地址 设备类型0xb0 if (false == DataInfo.CheckCRC(inbyte)) { return; } if (inbyte[0] != 0x21 || inbyte[1] != 0x58 || inbyte[2] != 0x44 || inbyte[4] != 0xB0) { return; } int busfferlistID = -1; for (int i = 0; i < _XD300Buffer.Length; i++) { if (_XD300Buffer[i]._Info._ip == ip && _XD300Buffer[i]._Info._addr == inbyte[3]) { busfferlistID = i; break; } } if (busfferlistID == -1) { return; } //功能码 0x0a 主动上报 if (inbyte[5] == 0x0a) { Data da = Read_recordn(inbyte); if (Convert.ToDateTime(da._dt) > Convert.ToDateTime(_XD300Buffer[busfferlistID]._Data._dt)) { _XD300Buffer[busfferlistID]._Data = Read_recordn(inbyte); _XD300Buffer[busfferlistID]._Data._person = Get_person(_XD300Buffer[busfferlistID]._Data._card); } _XD300Buffer[busfferlistID]._Command[2]._cmd = Set_recordcleannow(_XD300Buffer[busfferlistID]._Info._addr); _XD300Buffer[busfferlistID]._Command[2]._onoff = true; _XD300Buffer[busfferlistID]._SaveDatas = true; TimeSpan ts = DateTime.Now - Convert.ToDateTime(_XD300Buffer[busfferlistID]._Data._dt); if (Math.Abs(ts.TotalMinutes) > 3) { if (DateTime.Now.Date != Convert.ToDateTime(_XD300Buffer[busfferlistID]._Data._dt).Date) { Tool.xd300._XD300Buffer[busfferlistID]._Command[3]._cmd = Tool.xd300.Set_date(Tool.xd300._XD300Buffer[busfferlistID]._Info._addr); Tool.xd300._XD300Buffer[busfferlistID]._Command[3]._onoff = true; } Tool.xd300._XD300Buffer[busfferlistID]._Command[4]._cmd = Tool.xd300.Set_time(Tool.xd300._XD300Buffer[busfferlistID]._Info._addr); Tool.xd300._XD300Buffer[busfferlistID]._Command[4]._onoff = true; } return; } //功能码 0x06 读取记录总条数 if (inbyte[5] == 0x06) { _XD300Buffer[busfferlistID]._Data._count = Read_recordcount(inbyte); if (_XD300Buffer[busfferlistID]._Data._count > 0) { _XD300Buffer[busfferlistID]._Command[1]._cmd = Get_recordn(_XD300Buffer[busfferlistID]._Info._addr, 1); _XD300Buffer[busfferlistID]._Command[1]._onoff = true; } } //功能码 0x07 读取第N条记录 if (inbyte[5] == 0x07) { _XD300Buffer[busfferlistID]._Data = Read_recordn(inbyte); _XD300Buffer[busfferlistID]._Data._person = Get_person(_XD300Buffer[busfferlistID]._Data._card); if (_XD300Buffer[busfferlistID]._Data._n < _XD300Buffer[busfferlistID]._Data._count) { //读下条记录 _XD300Buffer[busfferlistID]._Command[1]._cmd = Get_recordn(_XD300Buffer[busfferlistID]._Info._addr, _XD300Buffer[busfferlistID]._Data._n + 1); _XD300Buffer[busfferlistID]._Command[1]._onoff = true; } if (_XD300Buffer[busfferlistID]._Data._n == _XD300Buffer[busfferlistID]._Data._count && _XD300Buffer[busfferlistID]._Data._count > 0) { //清除总记录 _XD300Buffer[busfferlistID]._Command[3]._cmd = Set_recordclean(_XD300Buffer[busfferlistID]._Info._addr); _XD300Buffer[busfferlistID]._Command[3]._onoff = true; _XD300Buffer[busfferlistID]._Data._count = 0; } _XD300Buffer[busfferlistID]._SaveDatas = true; } //功能码 0x08 清除记录 if (inbyte[5] == 0x08) { } //功能码 0x01 修改日期 if (inbyte[5] == 0x01) { } //功能码 0x02 修改时间 if (inbyte[5] == 0x01) { } _XD300Buffer[busfferlistID]._Command[_XD300Buffer[busfferlistID]._LastCommandIndex]._onoff = false; _XD300Buffer[busfferlistID]._Command[_XD300Buffer[busfferlistID]._LastCommandIndex]._retrytimesnow = 0; _XD300Buffer[busfferlistID]._Command[_XD300Buffer[busfferlistID]._LastCommandIndex]._backdt = DateTime.Now; _XD300Buffer[busfferlistID]._Command[_XD300Buffer[busfferlistID]._LastCommandIndex]._back = true; _XD300Buffer[busfferlistID]._Command[_XD300Buffer[busfferlistID]._LastCommandIndex]._send = false; _XD300Buffer[busfferlistID]._Info._state = true; //只要有数据返回即解除ISocketRS占用 Gprs.Gprs_IsOccupy(_XD300Buffer[busfferlistID]._Info._ip, false); }