private void cMenu_Check_Click(object sender, EventArgs e) //查看设备状态 { if (dgv_fanList.RowCount > 0) { DeviceForm.gw_SN = lab_SNID.Text; DeviceForm.addr_Short = dgv_fanList.CurrentRow.Cells[8].Value.ToString(); DeviceForm.endpoint = dgv_fanList.CurrentRow.Cells[10].Value.ToString(); DeviceForm.dev_Name = dgv_fanList.CurrentRow.Cells[1].Value.ToString(); FormState = true; device.ShowDialog(); if (device.DialogResult == DialogResult.OK) { string aShort = DeviceForm.addr_Short; if (FanList.Keys.Contains(aShort)) { Fan fan = new Fan(); FanList.TryGetValue(aShort, out fan); fan.DeviceName = DeviceForm.dev_Name; FanList[aShort] = fan; Refresh_FanList(); //在修改风机设备的名称之后将数据保存到xml文件中 XmlElement list = document.DocumentElement; XmlNodeList group = list.ChildNodes; foreach (XmlElement gp in group) { foreach (XmlElement nfan in gp) { string addr = nfan.FirstChild.InnerText; string name = nfan.LastChild.InnerText; if (addr.Equals(DeviceForm.addr_Short)) { nfan.LastChild.InnerText = DeviceForm.dev_Name; document.Save(Application.StartupPath + "\\DeviceName.xml"); } } } MessageBox.Show("修改成功!", "操作提示"); } } } }
private void tree_devices_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode current_node = tree_devices.SelectedNode; this.GroupFanList.Clear(); int level = current_node.Level; switch (level) { case 0: lab_groupName.Text = "所有设备"; lab_groupName.Enabled = false; btn_gn_save.Enabled = false; btn_del_group.Enabled = false; this.GroupFanList = new Dictionary <string, Fan>(this.FanList); Refresh_FanList_Group(this.GroupFanList); break; case 1: lab_groupName.Text = current_node.Text; lab_previous.Text = current_node.Text; if ("未分组".Equals(current_node.Text)) { btn_gn_save.Enabled = false; lab_groupName.Enabled = false; btn_del_group.Enabled = false; } else { btn_gn_save.Enabled = true; lab_groupName.Enabled = true; btn_del_group.Enabled = true; } foreach (TreeNode child in current_node.Nodes) { CustomNode node = (CustomNode)child.Tag; string addr = node.Addr; Fan fan = new Fan(); this.FanList.TryGetValue(addr, out fan); if (fan != null) { this.GroupFanList.Add(addr, fan); } } Refresh_FanList_Group(this.GroupFanList); break; case 2: lab_groupName.Text = current_node.Parent.Text; lab_groupName.Enabled = false; btn_gn_save.Enabled = false; btn_del_group.Enabled = false; CustomNode node_spe = (CustomNode)current_node.Tag; string addr_spe = node_spe.Addr; Fan fan_spe = new Fan(); this.FanList.TryGetValue(addr_spe, out fan_spe); if (fan_spe != null) { this.GroupFanList.Add(addr_spe, fan_spe); } Refresh_FanList_Group(this.GroupFanList); break; } }
private void Parse_DeviceState(byte[] data) //处理返回的设备状态数据 { if (data.Length != 35) { return; } else if (data.Length != data[1] + 2) { TCPHandler.State = "更新设备状态失败!"; return; } //短地址 byte[] tmp = data.Skip(2).Take(2).ToArray(); string addr_short = tmp[0].ToString("X2") + tmp[1].ToString("X2"); //string addr_short = System.Text.Encoding.Default.GetString(tmp); //EndPoint tmp = data.Skip(4).Take(1).ToArray(); string endpoint = tmp[0].ToString("X2"); //string endpoint = System.Text.Encoding.Default.GetString(tmp); //RealData byte[] data_real; if (data[12] == 0x68 && data[15] == 0x80 && data[16] == 0x2F) //设备状态数据 { data_real = data.Skip(17).Take(17).ToArray(); //版本信息 string ver_hard = "" + data_real[0]; string ver_soft = "" + data_real[1]; //运行时间 tmp = data_real.Skip(3).Take(2).ToArray(); string work_time = "" + BitConverter.ToInt16(tmp, 0); //风机运行状态 string work_mode = (data_real[9] & 1) == 1?"自动":"手动"; string fan_state = (data_real[9] & 64) == 64 ? "打开" : "关闭"; string pressure_state = (data_real[9] & 128) == 128 ? "打开" : "关闭"; //工作电压 tmp = data_real.Skip(10).Take(2).ToArray(); string work_voltage = ""; if ((tmp[1] & 128) == 128) { tmp[1] -= 128; work_voltage = "-" + BitConverter.ToInt16(tmp, 0); } else { work_voltage = "" + BitConverter.ToInt16(tmp, 0); } work_voltage = Convert.ToString(int.Parse(work_voltage) / 1000); //离子风机风速 string fan_speed = "" + data_real[12]; //设备ID tmp = data_real.Skip(13).Take(2).ToArray(); string device_id = tmp[0].ToString("X2") + tmp[1].ToString("X2"); //自动清洁间隔 string autoclean_interval = "" + data_real[15]; //报警电压 string alarm_voltage = "" + data_real[16]; Update_XmlDataBase(addr_short, device_id); if (FanList.Keys.Contains <string>(addr_short) == true) { Fan fan = new Fan(); FanList.TryGetValue(addr_short, out fan); fan.FanState = fan_state; fan.PressureState = pressure_state; fan.BalanceVoltage = work_voltage; fan.DeviceID = device_id; fan.AutoCleanInterval = autoclean_interval; fan.AlarmVoltage = alarm_voltage; FanList[addr_short] = fan; Refresh_FanList(); } if (FormState == true && addr_short == DeviceForm.addr_Short) { DeviceForm.hard_Version = ver_hard; DeviceForm.soft_Version = ver_soft; DeviceForm.work_Time = work_time; DeviceForm.bal_Voltage = work_voltage; DeviceForm.fan_Speed = fan_speed; DeviceForm.autoClean_Interval = autoclean_interval; DeviceForm.alarm_Voltage = alarm_voltage; DeviceForm.work_Mode = work_mode; DeviceForm.fan_State = fan_state; DeviceForm.pressure_State = pressure_state; } } else if (data[12] == 0x68 && data[15] == 0xC8 && data[16] == 0x00) //设备异常状态数据 { string device_voltage = (data[17] & 1) == 1? "异常" : "正常"; string device_electric = (data[17] & 2) == 2 ? "异常" : "正常"; string balance_voltage = (data[17] & 4) == 4 ? "异常" : "正常"; string fan_error = (data[17] & 8) == 8 ? "异常" : "正常"; string device_id = data[19].ToString("X2") + data[20].ToString("X2"); Update_XmlDataBase(addr_short, device_id); if (FanList.Keys.Contains(addr_short)) { Fan fan = new Fan(); FanList.TryGetValue(addr_short, out fan); fan.PressureError = device_voltage; fan.FanError = fan_error; fan.DeviceID = device_id; FanList[addr_short] = fan; Refresh_FanList(); } if (FormState == true && addr_short == DeviceForm.addr_Short) { DeviceForm.error_Voltage = device_voltage; DeviceForm.error_Electric = device_electric; DeviceForm.error_Bal = balance_voltage; DeviceForm.error_Fan = fan_error; } if (handler != null && handler.isConnected()) //回复接收到异常上报数据 { handler.SendData(DataSent.GetSendData(lab_SNID.Text, addr_short, endpoint, 0x30)); } } else { return; } TCPHandler.State = "更新设备状态成功!"; }
private void Parse_DeviceInfo(byte[] data) //处理返回的设备信息 { if (data.Length < 30) { TCPHandler.State = "获取设备失败!"; return; } Fan newFan = new Fan(); newFan.DeviceName = "新风机设备"; newFan.NetState = "打开"; newFan.PressureError = "无"; newFan.FanError = "无"; newFan.PressureState = "未知"; newFan.FanState = "未知"; newFan.AutoCleanInterval = "未知"; newFan.AlarmVoltage = "未知"; //短地址 byte[] tmp = data.Skip(2).Take(2).ToArray(); //string addr_short = System.Text.Encoding.Default.GetString(tmp); string addr_short = tmp[0].ToString("X2") + tmp[1].ToString("X2"); newFan.ShorAddress = addr_short; //EndPoint tmp = data.Skip(4).Take(1).ToArray(); //string endpoint = System.Text.Encoding.Default.GetString(tmp); string endpoint = tmp[0].ToString("X2"); newFan.EndPoint = endpoint; //设备ID tmp = data.Skip(7).Take(2).ToArray(); //string device_id = System.Text.Encoding.Default.GetString(tmp); string device_id = tmp[1].ToString("X2") + tmp[0].ToString("X2"); device_id = "未获取"; newFan.DeviceID = device_id; //8字节IEEE地址 tmp = data.Skip(11 + data[10] + 1).Take(8).ToArray(); //string addr_ieee = System.Text.Encoding.Default.GetString(tmp); string addr_ieee = tmp[0].ToString("X2") + tmp[1].ToString("X2") + tmp[2].ToString("X2") + tmp[3].ToString("X2") + tmp[4].ToString("X2") + tmp[5].ToString("X2") + tmp[6].ToString("X2") + tmp[7].ToString("X2"); newFan.IEEEAddress = addr_ieee; //匹配已保存的设备名 XmlElement list = document.DocumentElement; XmlNodeList group = list.ChildNodes; bool flag = true; foreach (XmlElement gp in group) { foreach (XmlNode fan in gp.ChildNodes) { string addr = fan.FirstChild.InnerText; string devid = fan.ChildNodes[1].InnerText; string name = fan.LastChild.InnerText; if (addr.Equals(addr_short)) { newFan.DeviceName = name; newFan.DeviceID = devid; flag = false; } } } //若未在数据库中保存过设备,则添加 if (flag) { foreach (XmlElement xe in group) { if (xe.GetAttribute("name").Equals("未分组")) { XmlElement fan = document.CreateElement("fan"); XmlElement addr = document.CreateElement("addr"); addr.InnerText = addr_short; XmlElement devid = document.CreateElement("deviceid"); devid.InnerText = device_id; XmlElement name = document.CreateElement("name"); name.InnerText = "新风机设备"; fan.AppendChild(addr); fan.AppendChild(devid); fan.AppendChild(name); xe.AppendChild(fan); document.Save(Application.StartupPath + "\\DeviceName.xml"); } } } if (!FanList.Keys.Contains(addr_short)) { FanList.Add(addr_short, newFan); } TCPHandler.State = "获取设备成功!"; Refresh_FanList(); }