Exemplo n.º 1
0
        /// <summary>
        /// 从xml文件导入配置
        /// </summary>
        /// <param name="filename">文件名</param>
        public bool ImportFromXml(string filename)
        {
            lock (obj)
            {
                mDataRegs.Clear();
                mFiles.Clear();
                XmlNodeList nlist, flist;
                try{
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(filename);
                    XmlNode root = xmlDoc.SelectSingleNode("Root");
                    nlist = root.SelectSingleNode("Regs").ChildNodes;
                    flist = root.SelectSingleNode("Files").ChildNodes;
                }
                catch {
                    return(false);
                }
                //寄存器数据
                string     name;
                UInt16     addr;
                Int16      val, hlimit, llimit, step;
                UpdateMode mode;
                foreach (XmlNode xn in nlist)
                {
                    XmlElement xe = (XmlElement)xn;
                    try{
                        name   = xe.GetAttribute("Name");
                        addr   = UInt16.Parse(xe.GetAttribute("Address"));
                        val    = Int16.Parse(xe.GetAttribute("Value"));
                        llimit = Int16.Parse(xe.GetAttribute("LLimit"));
                        hlimit = Int16.Parse(xe.GetAttribute("HLimit"));
                        step   = Int16.Parse(xe.GetAttribute("Step"));
                        switch (xe.GetAttribute("Mode"))
                        {
                        case "Const":
                            mode = UpdateMode.Const;
                            break;

                        case "Random":
                            mode = UpdateMode.Random;
                            break;

                        case "RollA":
                            mode = UpdateMode.RollA;
                            break;

                        case "RollD":
                            mode = UpdateMode.RollD;
                            break;

                        case "StepAdd":
                            mode = UpdateMode.StepAdd;
                            break;

                        case "StepDec":
                            mode = UpdateMode.StepDec;
                            break;

                        default:
                            mode = UpdateMode.Const;
                            break;
                        }
                    }
                    catch {
                        return(false);
                    }
                    mDataRegs.Add(addr, new ModbusData(name, addr, mode, val, llimit, hlimit, step));
                }
                //寄存器数据
                byte   type;
                byte[] data;
                foreach (XmlNode xn in flist)
                {
                    XmlElement xe = (XmlElement)xn;
                    try{
                        type = byte.Parse(xe.GetAttribute("Type"));
                        data = HexString.HexString2Bytes(xe.InnerText);
                    }
                    catch {
                        return(false);
                    }
                    mFiles.Add(type, new FileData(type, data));
                }
                return(true);
            }
        }