Exemple #1
0
        public void saveJson(JsonTextWriter writer)
        {
            //添加modbusslave节点
            ModbusClientManage client = clientManage;

            if (client.modbusClientList.Count > 0)
            {
                writer.WritePropertyName("mbtcp_client");
                writer.WriteStartObject();                              //添加{  client节点
                writer.WritePropertyName("number");
                writer.WriteValue(clientManage.modbusClientList.Count); //number
                writer.WritePropertyName("time_uint");
                writer.WriteValue("ms");                                //时间单位
                int index = 0;
                writer.WritePropertyName("conf");
                writer.WriteStartArray();                                     //[ client节点下conf数组
                for (int i = 0; i < clientManage.modbusClientList.Count; i++) //遍历所有Client的集合
                {
                    ModbusClientData data = clientManage.modbusClientList.ElementAt(i);

                    writer.WriteStartObject();//{  client节点下device
                    writer.WritePropertyName("port");
                    if (data.transformChannel == "NET1")
                    {
                        writer.WriteValue("ethif_0");
                    }
                    else
                    {
                        writer.WriteValue("");
                    }
                    //writer.WritePropertyName("response_timeout");
                    //writer.WriteValue(data.responseTimeout);
                    string mode = null;
                    if (data.transformMode == 0)
                    {
                        mode = "tcp";
                    }
                    else if (data.transformMode == 1)
                    {
                        mode = "udp";
                    }
                    writer.WritePropertyName("mode");
                    writer.WriteValue(mode);
                    writer.WritePropertyName("dev_namestr");
                    writer.WriteValue("mb" + mode + "_client" + data.ID);
                    writer.WritePropertyName("slave");
                    writer.WriteStartObject();//{  slave节点 从设备信息
                    writer.WritePropertyName("num");
                    writer.WriteValue(data.modbusDeviceList.Count);
                    if (i > 0)
                    {
                        index = i * clientManage.modbusClientList[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_ip");
                        writer.WriteValue(dataDev.ipaddr);
                        writer.WritePropertyName("slave_port");
                        writer.WriteValue(dataDev.port);
                        writer.WritePropertyName("slave_id");
                        writer.WriteValue(dataDev.serverAddr);
                        writer.WritePropertyName("response_timeout");
                        writer.WriteValue(dataDev.reponseTimeout);
                        writer.WritePropertyName("retry_interval");
                        writer.WriteValue(dataDev.reconnectInterval);
                        writer.WritePropertyName("timeout_cnt_max");
                        writer.WriteValue(dataDev.permitTimeoutCount);
                        writer.WritePropertyName("io_range");
                        writer.WriteStartObject();//{  conf数组下 iorange
                        writer.WritePropertyName("start");
                        writer.WriteValue(dataDev.devstartaddr);
                        writer.WritePropertyName("bytes");
                        writer.WriteValue(dataDev.devlength);
                        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_name");
                            writer.WriteValue("channel" + dataChannel.ID);
                            writer.WritePropertyName("msg_type");
                            writer.WriteValue(dataChannel.msgType);
                            writer.WritePropertyName("trig_mode");
                            writer.WriteValue(dataChannel.trig_mode);
                            writer.WritePropertyName("polling_time");
                            writer.WriteValue(dataChannel.pollingTime);
                            writer.WritePropertyName("offset");
                            writer.WriteValue(dataChannel.Offset);
                            writer.WritePropertyName("quantity");
                            writer.WriteValue(dataChannel.Length);
                            writer.WritePropertyName("io_offset");
                            writer.WriteValue(dataChannel.channelstartaddr + 3 - dataDev.devstartaddr);
                            writer.WritePropertyName("io_bytes");
                            writer.WriteValue(dataChannel.Channellength - 3);
                            writer.WritePropertyName("trigger_offset");
                            writer.WriteValue(dataChannel.channelstartaddr - dataDev.devstartaddr);
                            writer.WritePropertyName("error_offset");
                            writer.WriteValue(dataChannel.channelstartaddr + 1 - dataDev.devstartaddr);
                            writer.WritePropertyName("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();             //添加}  client节点
            }
        }
Exemple #2
0
        //public int clientnumber;

        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);

                ModbusClientData data = new ModbusClientData();

                int.TryParse(e.GetAttribute("id"), out data.ID);//将所有id属性的值(字符串),转换成int32类型,输出变量为data.ID
                int.TryParse(e.GetAttribute("clientstartaddr"), out data.clientstartaddr);
                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("clientstartaddr"), out data.clientstartaddr);
                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.ipaddr  = e.GetAttribute("ipaddr");
                    int.TryParse(e.GetAttribute("serveraddr"), out deviceData.serverAddr);
                    int.TryParse(e.GetAttribute("port"), out deviceData.port);
                    int.TryParse(e.GetAttribute("responsetimeout"), out deviceData.reponseTimeout);
                    int.TryParse(e.GetAttribute("permittimeoutcount"), out deviceData.permitTimeoutCount);
                    int.TryParse(e.GetAttribute("reconnectinterval"), out deviceData.reconnectInterval);
                    //int.TryParse(e.GetAttribute("resetVaraible"), out deviceData.resetVaraible);
                    deviceData.resetVaraible = e.GetAttribute("resetvaraible");
                    string [] resetkey = e.GetAttribute("resetkey").Split('c');
                    deviceData.resetkey[0] = resetkey[0];
                    deviceData.resetkey[1] = resetkey[1];
                    int.TryParse(e.GetAttribute("devstartaddr"), out deviceData.devstartaddr);
                    int.TryParse(e.GetAttribute("devlength"), out deviceData.devlength);
                    //读取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);
                        channelData.msgdiscrib = e.GetAttribute("msgdiscrib");
                        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.Offset);
                        int.TryParse(e.GetAttribute("length"), out channelData.Length);
                        //int.TryParse(e.GetAttribute("writeoffset"), out channelData.writeOffset);
                        channelData.trigger_offset = e.GetAttribute("trigger_offset");
                        //int.TryParse(e.GetAttribute("writelength"), out channelData.writeLength);
                        string [] trigoffset = e.GetAttribute("triggeroffsetkey").Split('c');
                        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_offset = e.GetAttribute("error_offset");

                        int.TryParse(e.GetAttribute("channelstartaddr"), out channelData.channelstartaddr);
                        int.TryParse(e.GetAttribute("Channellength"), out channelData.Channellength);
                        int.TryParse(e.GetAttribute("type"), out channelData.type);
                        channelData.note = e.GetAttribute("note");
                        int.TryParse(e.GetAttribute("edit"), out channelData.edit);
                        deviceData.modbusChannelList.Add(channelData);
                    }


                    data.modbusDeviceList.Add(deviceData);
                }

                clientManage.add(data);//添加进xml
            }
        }
Exemple #3
0
        public void saveXml(ref XmlElement elem, ref XmlDocument doc)
        {
            XmlElement elem1 = doc.CreateElement("modbusclient");

            elem1.SetAttribute("number", dataGridView1.RowCount.ToString());
            elem1.SetAttribute("time_unit", "ms");
            elem.AppendChild(elem1);
            for (int i = 0; i < clientManage.modbusClientList.Count; i++)//遍历所有的modbusclient集合
            {
                ModbusClientData data    = clientManage.modbusClientList.ElementAt(i);
                XmlElement       elem1_m = doc.CreateElement("modbusclient"); //创建m节点
                elem1_m.SetAttribute("id", data.ID.ToString());               //给id节点赋值,值为data.ID
                elem1_m.SetAttribute("clientstartaddr", data.clientstartaddr.ToString());
                //transformChannel com1 com2 com3
                elem1_m.SetAttribute("transformchannel", data.transformChannel);
                //0 TCP    1 UDP
                elem1_m.SetAttribute("transformmode", data.transformMode.ToString());
                elem1_m.SetAttribute("responsetimeout", data.responseTimeout.ToString());
                elem1_m.SetAttribute("clientstartaddr", data.clientstartaddr.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("ipaddr", dataDev.ipaddr.ToString());
                    elem1_m_d.SetAttribute("serveraddr", dataDev.serverAddr.ToString());
                    elem1_m_d.SetAttribute("port", dataDev.port.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.ToString());
                    elem1_m_d.SetAttribute("resetkey", dataDev.resetkey[0] + "c" + dataDev.resetkey[1]);
                    elem1_m_d.SetAttribute("devstartaddr", dataDev.devstartaddr.ToString());
                    elem1_m_d.SetAttribute("devlength", dataDev.devlength.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("msgdiscrib", dataChannel.msgdiscrib);
                        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.Offset.ToString());
                        elem1_m_d_c.SetAttribute("length", dataChannel.Length.ToString());
                        elem1_m_d_c.SetAttribute("trigger_offset", dataChannel.trigger_offset);
                        elem1_m_d_c.SetAttribute("triggeroffsetkey", dataChannel.offsetkey[0] + "c" + dataChannel.offsetkey[1] + dataChannel.offsetkey[2] + "c" + dataChannel.offsetkey1);
                        elem1_m_d_c.SetAttribute("error_offset", dataChannel.error_offset);
                        elem1_m_d_c.SetAttribute("erroroffsetkey", dataChannel.offsetkey[0] + "c" + dataChannel.offsetkey[1] + dataChannel.offsetkey[2] + "c" + dataChannel.offsetkey2);
                        elem1_m_d_c.SetAttribute("channelstartaddr", dataChannel.channelstartaddr.ToString());
                        elem1_m_d_c.SetAttribute("Channellength", dataChannel.Channellength.ToString());
                        elem1_m_d_c.SetAttribute("type", dataChannel.type.ToString());
                        elem1_m_d_c.SetAttribute("note", dataChannel.note);
                        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);
            }
        }