public void removeChannel(ChannelData data) { modbusChannelList.Remove(data); curDeviceLength = checkDeviceLength(); //删除通道,通道内的地址重新分配 refreshAddr(curDeviceAddr); }
private void button_add_Click(object sender, EventArgs e) { int row = dataGridView1.RowCount; dataGridView1.RowCount += 1; // Set the text for each button. int i = row; ChannelData data = new ChannelData(); dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.ID].Value = row; data.ID = row; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.NAME].Value = "设备" + i.ToString(); data.nameChannel = dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.NAME].Value.ToString(); // dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.MSGTYPE].Value = ""; if (dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.MSGTYPE].Value.ToString() == "") { data.msgType = -1; } else { data.msgType = int.Parse(dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.MSGTYPE].Value.ToString()); } dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.POLLINGTIME].Value = "1000"; //ms data.pollingTime = int.Parse(dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.POLLINGTIME].Value.ToString()); dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.READOFFSET].Value = "0"; string str = dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.READOFFSET].Value.ToString(); data.readOffset = int.Parse(str); dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.READLENGTH].Value = "1"; str = dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.READLENGTH].Value.ToString(); data.readLength = int.Parse(str); dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.WRITEOFFSET].Value = "0"; str = dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.WRITEOFFSET].Value.ToString(); data.writeOffset = int.Parse(str); dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.WRITELENGTH].Value = "1"; str = dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.WRITELENGTH].Value.ToString(); data.writeLength = int.Parse(str); dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.NOTE].Value = ""; data.note = dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.NOTE].Value.ToString(); data_.modbusChannelList.Add(data); }
public void addChannel(ChannelData data) { data.curChannelAddr = curDeviceAddr + 1; foreach (var channel in modbusChannelList) { data.curChannelAddr += channel.curChannelLength; } data.writeOffset = data.curChannelAddr; data.writeLength = data.curChannelAddr + 1; modbusChannelList.Add(data); curDeviceLength = checkDeviceLength(); }
private void button_add_Click(object sender, EventArgs e) { if (utility.masterDeviceChannleCountMax <= dataGridView1.RowCount) { string err = string.Format("通道最大个数是{0}", utility.masterDeviceCountMax); utility.PrintError(err); return; } int row = dataGridView1.RowCount; // Set the text for each button. int i = row; ChannelData data = new ChannelData(); deviceData_.addChannel(data); // data.ID = row; data.nameChannel = "master" + masterData_.ID.ToString() + "_d" + dn + "_c" + i.ToString(); data.msgType = 0x01; //0x01 单bit 默认生成 data.curChannelLength = 1 + 3; data.trig_mode = 0; data.pollingTime = 1000; data.readOffset = 0; data.readLength = 1; data.offsetkey[0] = deviceData_.resetkey[0]; data.offsetkey[1] = deviceData_.resetkey[1]; data.offsetkey[2] = data.ID.ToString(); data.offsetkey1 = "0"; data.offsetkey2 = "1"; data.note = ""; // //deviceData_.modbusChannelList.Add(data); if (!checkMasterLenthValid()) { deviceData_.removeChannel(data); } else { dataGridView1.RowCount += 1; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.ID].Value = data.ID; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.NAME].Value = data.nameChannel; // dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.MSGTYPE].Value = "读多个位(线圈) - Fc01"; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.TRIG_MODE].Value = "自动"; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.POLLINGTIME].Value = data.pollingTime; //ms dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.READOFFSET].Value = data.readOffset; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.READLENGTH].Value = data.readLength; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.WRITEOFFSET].Value = ""; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.WRITELENGTH].Value = ""; dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.NOTE].Value = ""; //data.note = dataGridView1.Rows[i].Cells[(int)COLUMNNAME_CHANNLE.NOTE].Value.ToString(); } for (int j = 0; j < 16; j++) { if (temrow[j] == -1) { temrow[j] = row; break; } } }
public void loadXml(XmlNode xn) { XmlNodeList nodeList = xn.ChildNodes; //创建xn的所有子节点的集合 foreach (XmlNode childNode in nodeList) //遍历集合中所有的节点 { XmlElement e = (XmlElement)childNode; string name = e.Name; string test = e.GetAttribute("name");//获取该节点中所有name属性的值 Console.WriteLine(name); ModbusMasterData data = new ModbusMasterData(); int.TryParse(e.GetAttribute("id"), out data.ID);//将所有id属性的值(字符串),转换成int32类型,输出变量为data.ID data.transformChannel = e.GetAttribute("transformchannel"); int.TryParse(e.GetAttribute("transformmode"), out data.transformMode); int.TryParse(e.GetAttribute("responsetimeout"), out data.responseTimeout); int.TryParse(e.GetAttribute("packet_interval"), out data.packet_interval); bool.TryParse(e.GetAttribute("isready"), out data.isready); //data.transformChannel = int.TryParse(eChild.GetAttribute("transformchannel")); //读取device数据 XmlNodeList nodeDeviceList = childNode.ChildNodes;//创建当前子设备节点下的所有子节点集合 foreach (XmlNode childDeviceNode in nodeDeviceList) { e = (XmlElement)childDeviceNode; //子设备节点 DeviceData deviceData = new DeviceData(); int.TryParse(e.GetAttribute("ID"), out deviceData.ID); //为各子节点赋值 deviceData.nameDev = e.GetAttribute("namedev"); deviceData.slaveAddr = e.GetAttribute("slaveaddr"); int.TryParse(e.GetAttribute("responsetimeout"), out deviceData.reponseTimeout); int.TryParse(e.GetAttribute("permittimeoutcount"), out deviceData.permitTimeoutCount); int.TryParse(e.GetAttribute("reconnectinterval"), out deviceData.reconnectInterval); deviceData.resetVaraible = e.GetAttribute("resetvaraible"); string[] resetkey = e.GetAttribute("resetkey").Split('m'); deviceData.resetkey[0] = resetkey[0]; deviceData.resetkey[1] = resetkey[1]; int.TryParse(e.GetAttribute("devstartaddr"), out deviceData.curDeviceAddr); int.TryParse(e.GetAttribute("devlength"), out deviceData.curDeviceLength); //读取channel数据 XmlNodeList nodeChannelList = childDeviceNode.ChildNodes; foreach (XmlNode childChannelNode in nodeChannelList) { e = (XmlElement)childChannelNode; ChannelData channelData = new ChannelData(); int.TryParse(e.GetAttribute("ID"), out channelData.ID); channelData.nameChannel = e.GetAttribute("namechannel"); int.TryParse(e.GetAttribute("msgtype"), out channelData.msgType); int.TryParse(e.GetAttribute("trig_mode"), out channelData.trig_mode); int.TryParse(e.GetAttribute("pollingtime"), out channelData.pollingTime); int.TryParse(e.GetAttribute("offset"), out channelData.readOffset); int.TryParse(e.GetAttribute("length"), out channelData.readLength); channelData.trigger = e.GetAttribute("trigger_offset"); string[] trigoffset = e.GetAttribute("triggeroffsetkey").Split('m'); channelData.offsetkey[0] = trigoffset[0]; trigoffset[1].Substring(0, 2); channelData.offsetkey[1] = trigoffset[1].Substring(0, 2); channelData.offsetkey[2] = trigoffset[1].Substring(2); channelData.error = e.GetAttribute("error_offset"); int.TryParse(e.GetAttribute("channelstartaddr"), out channelData.curChannelAddr); int.TryParse(e.GetAttribute("channellength"), out channelData.curChannelLength); //int.TryParse(e.GetAttribute("writeoffset"), out channelData.writeOffset); //int.TryParse(e.GetAttribute("writelength"), out channelData.writeLength); channelData.note = e.GetAttribute("note"); int.TryParse(e.GetAttribute("edit"), out channelData.edit); deviceData.modbusChannelList.Add(channelData); } data.modbusDeviceList.Add(deviceData); } masterManage.add(data);//添加进xml mastermanege1 = masterManage; } }
public void saveJson(JsonTextWriter writer) { //添加modbusmaster节点 ModbusMasterManage master = masterManage; if (master.modbusMastrList.Count > 0) { writer.WritePropertyName("mbserial_master"); writer.WriteStartObject(); //添加{ master节点 writer.WritePropertyName("number"); writer.WriteValue(masterManage.modbusMastrList.Count); //number writer.WritePropertyName("time_uint"); writer.WriteValue("ms"); //时间单位 int index = 0; writer.WritePropertyName("conf"); writer.WriteStartArray(); //[ master节点下conf数组 for (int i = 0; i < masterManage.modbusMastrList.Count; i++) //遍历所有Client的集合 { ModbusMasterData data = masterManage.modbusMastrList.ElementAt(i); writer.WriteStartObject();//{ master节点下device writer.WritePropertyName("port"); if (data.transformChannel == "COM1") { writer.WriteValue("ser_port7"); } else if (data.transformChannel == "COM2") { writer.WriteValue("ser_port1"); } else if (data.transformChannel == "COM3") { writer.WriteValue("ser_port0"); } else if (data.transformChannel == "COM4") { writer.WriteValue("ser_port2"); } else if (data.transformChannel == "COM5") { writer.WriteValue("ser_port3"); } else if (data.transformChannel == "COM6") { writer.WriteValue("ser_port8"); } else { writer.WriteValue(""); } writer.WritePropertyName("response_timeout"); writer.WriteValue(data.responseTimeout); writer.WritePropertyName("packet_interval"); writer.WriteValue(data.packet_interval); string mode = null; if (data.transformMode == 0) { mode = "rtu"; } else if (data.transformMode == 1) { mode = "ascii"; } writer.WritePropertyName("mode"); writer.WriteValue(mode); writer.WritePropertyName("dev_namestr"); writer.WriteValue("mbserial" + "_master" + data.ID); writer.WritePropertyName("slave"); writer.WriteStartObject();//{ slave节点 从设备信息 writer.WritePropertyName("num"); writer.WriteValue(data.modbusDeviceList.Count); if (i > 0) { index = i * masterManage.modbusMastrList[i - 1].modbusDeviceList.Count; } writer.WritePropertyName("conf"); writer.WriteStartArray(); //[ slave节点conf for (int j = 0; j < data.modbusDeviceList.Count; j++) //循环添加每个设备的各参数至 { DeviceData dataDev = data.modbusDeviceList.ElementAt(j); writer.WriteStartObject();//{ conf数组下节点,从设备信息 writer.WritePropertyName("slave_id"); try { int sid = Convert.ToInt32(dataDev.slaveAddr); } catch { return; } writer.WriteValue(Convert.ToInt32(dataDev.slaveAddr)); writer.WritePropertyName("timeout_cnt_max"); writer.WriteValue(dataDev.permitTimeoutCount); writer.WritePropertyName("retry_interval"); writer.WriteValue(dataDev.reconnectInterval); writer.WritePropertyName("io_range"); writer.WriteStartObject(); //{ conf数组下 iorange writer.WritePropertyName("start"); writer.WriteValue(dataDev.curDeviceAddr); //io范围这块上位机设计方案中在master和client中并没有提到,需要确认 writer.WritePropertyName("bytes"); writer.WriteValue(dataDev.curDeviceLength); writer.WriteEndObject();//} conf数组下 iorange writer.WritePropertyName("restart_offset"); writer.WriteValue(0); writer.WritePropertyName("channel_cfg"); writer.WriteStartObject();//{ channel_cfg节点 writer.WritePropertyName("num"); writer.WriteValue(dataDev.modbusChannelList.Count); writer.WritePropertyName("conf"); writer.WriteStartArray(); //[ channel_cfg节点下conf数组 for (int k = 0; k < dataDev.modbusChannelList.Count; k++) //循环添加通道至子设备节点下 { ChannelData dataChannel = dataDev.modbusChannelList.ElementAt(k); writer.WriteStartObject();//{ channel_cfg节点下conf数组中channel信息 writer.WritePropertyName("channel_id"); writer.WriteValue(dataChannel.ID); writer.WritePropertyName("channel" + dataChannel.ID); writer.WriteValue(dataChannel.nameChannel); writer.WritePropertyName("msg_type"); writer.WriteValue(dataChannel.msgType); writer.WritePropertyName("trig_mode");//trig_mode的含义?? writer.WriteValue(dataChannel.trig_mode); writer.WritePropertyName("polling_time"); writer.WriteValue(dataChannel.pollingTime); writer.WritePropertyName("offset");//偏移这块和我们上位机草图设计有一些出入,需要确认 writer.WriteValue(dataChannel.readOffset); writer.WritePropertyName("quantity"); writer.WriteValue(dataChannel.readLength); writer.WritePropertyName("io_offset"); writer.WriteValue(dataChannel.curChannelAddr + 3 - dataDev.curDeviceAddr); writer.WritePropertyName("io_bytes"); writer.WriteValue(dataChannel.curChannelLength - 3); writer.WritePropertyName("trigger_offset"); writer.WriteValue(dataChannel.curChannelAddr - dataDev.curDeviceAddr); writer.WritePropertyName("error_offset"); writer.WriteValue(dataChannel.curChannelAddr + 1 - dataDev.curDeviceAddr); writer.WritePropertyName("direction"); //direction参数的含义?? writer.WriteValue("in"); writer.WriteEndObject(); //} channel_cfg节点下conf数组中channel信息 } writer.WriteEndArray(); //] channel_cfg节点下conf数组 writer.WriteEndObject(); //} channel_cfg节点 writer.WriteEndObject(); //} conf数组下节点,从设备信息 } writer.WriteEndArray(); //] slave节点conf writer.WriteEndObject(); //} slave节点 从设备信息 writer.WriteEndObject(); //} client节点下device } writer.WriteEndArray(); //] client节点下conf数组 writer.WriteEndObject(); //添加} master节点 } }
public void saveXml(ref XmlElement elem, ref XmlDocument doc) { XmlElement elem1 = doc.CreateElement("modbusmaster"); elem1.SetAttribute("number", dataGridView1.RowCount.ToString()); elem1.SetAttribute("time_unit", "ms"); elem.AppendChild(elem1); //XmlElement elemc = doc.CreateElement("conf"); //elemc.SetAttribute("mode",); //master项 for (int i = 0; i < masterManage.modbusMastrList.Count; i++)//遍历所有的modbusmaster集合 { ModbusMasterData data = masterManage.modbusMastrList.ElementAt(i); XmlElement elem1_m = doc.CreateElement("m"); //创建m节点 elem1_m.SetAttribute("id", data.ID.ToString()); //给id节点赋值,值为data.ID //transformChannel com1 com2 com3 elem1_m.SetAttribute("transformchannel", data.transformChannel); //0 RTU 1 ASCII elem1_m.SetAttribute("transformmode", data.transformMode.ToString()); elem1_m.SetAttribute("responsetimeout", data.responseTimeout.ToString()); elem1_m.SetAttribute("packet_interval", data.packet_interval.ToString()); elem1_m.SetAttribute("isready", data.isready.ToString()); //create devices for (int j = 0; j < data.modbusDeviceList.Count; j++) //循环添加每个设备的各参数值至xml { DeviceData dataDev = data.modbusDeviceList.ElementAt(j); XmlElement elem1_m_d = doc.CreateElement("device"); elem1_m_d.SetAttribute("ID", dataDev.ID.ToString()); elem1_m_d.SetAttribute("namedev", dataDev.nameDev.ToString()); elem1_m_d.SetAttribute("slaveaddr", dataDev.slaveAddr.ToString()); elem1_m_d.SetAttribute("responsetimeout", dataDev.reponseTimeout.ToString()); elem1_m_d.SetAttribute("permittimeoutcount", dataDev.permitTimeoutCount.ToString()); elem1_m_d.SetAttribute("reconnectinterval", dataDev.reconnectInterval.ToString()); elem1_m_d.SetAttribute("resetvaraible", dataDev.resetVaraible); elem1_m_d.SetAttribute("resetkey", dataDev.resetkey[0] + "m" + dataDev.resetkey[1]); elem1_m_d.SetAttribute("devstartaddr", dataDev.curDeviceAddr.ToString()); elem1_m_d.SetAttribute("devlength", dataDev.curDeviceLength.ToString()); //通道 for (int k = 0; k < dataDev.modbusChannelList.Count; k++)//循环添加通道至子设备节点下 { ChannelData dataChannel = dataDev.modbusChannelList.ElementAt(k); XmlElement elem1_m_d_c = doc.CreateElement("channel"); elem1_m_d_c.SetAttribute("ID", dataChannel.ID.ToString()); elem1_m_d_c.SetAttribute("namechannel", dataChannel.nameChannel); elem1_m_d_c.SetAttribute("msgtype", dataChannel.msgType.ToString()); elem1_m_d_c.SetAttribute("trig_mode", dataChannel.trig_mode.ToString()); elem1_m_d_c.SetAttribute("pollingtime", dataChannel.pollingTime.ToString()); elem1_m_d_c.SetAttribute("offset", dataChannel.readOffset.ToString()); elem1_m_d_c.SetAttribute("length", dataChannel.readLength.ToString()); elem1_m_d_c.SetAttribute("trigger_offset", dataChannel.trigger.ToString()); elem1_m_d_c.SetAttribute("triggeroffsetkey", dataChannel.offsetkey[0] + "m" + dataChannel.offsetkey[1] + dataChannel.offsetkey[2] + "m" + dataChannel.offsetkey1); elem1_m_d_c.SetAttribute("error_offset", dataChannel.error.ToString()); elem1_m_d_c.SetAttribute("erroroffsetkey", dataChannel.offsetkey[0] + "m" + dataChannel.offsetkey[1] + dataChannel.offsetkey[2] + "m" + dataChannel.offsetkey2); elem1_m_d_c.SetAttribute("channelstartaddr", dataChannel.curChannelAddr.ToString()); elem1_m_d_c.SetAttribute("channellength", dataChannel.curChannelLength.ToString()); // elem1_m_d_c.SetAttribute("writeoffset", dataChannel.writeOffset.ToString()); //elem1_m_d_c.SetAttribute("writelength", dataChannel.writeLength.ToString()); elem1_m_d_c.SetAttribute("note", dataChannel.note.ToString()); elem1_m_d_c.SetAttribute("edit", dataChannel.edit.ToString()); elem1_m_d.AppendChild(elem1_m_d_c);//将通道节点作为子节点加入设备节点 } elem1_m.AppendChild(elem1_m_d); } //XmlElement elem1_d = doc.CreateElement("device"); //elem1_m.SetAttribute("id", data.ID.ToString()); elem1.AppendChild(elem1_m); } }