Exemplo n.º 1
0
        /// <summary>
        /// Загрузить шаблон устройства
        /// </summary>
        public bool Load(string fileName, out string errMsg)
        {
            SetToDefault();

            try
            {
                // загрузка шаблона устройства
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);

                // загрузка настроек шаблона
                XmlNode settingsNode = xmlDoc.DocumentElement.SelectSingleNode("Settings");
                if (settingsNode != null)
                {
                    Sett.LoadFromXml((XmlElement)settingsNode);
                }

                // загрузка групп элементов
                XmlNode elemGroupsNode = xmlDoc.DocumentElement.SelectSingleNode("ElemGroups");
                if (elemGroupsNode != null)
                {
                    int kpTagInd = 0;

                    foreach (XmlElement elemGroupElem in elemGroupsNode.ChildNodes)
                    {
                        TableTypes tableType = elemGroupElem.GetAttrAsEnum <TableTypes>("tableType");
                        ElemGroup  elemGroup = new ElemGroup(tableType);
                        elemGroup.Name          = elemGroupElem.GetAttribute("name");
                        elemGroup.Address       = (ushort)elemGroupElem.GetAttrAsInt("address");
                        elemGroup.Active        = elemGroupElem.GetAttrAsBool("active", true);
                        elemGroup.StartKPTagInd = kpTagInd;
                        elemGroup.StartSignal   = kpTagInd + 1;

                        XmlNodeList elemNodes   = elemGroupElem.SelectNodes("Elem");
                        ElemTypes   defElemType = elemGroup.DefElemType;

                        foreach (XmlElement elemElem in elemNodes)
                        {
                            Elem elem = new Elem();
                            elem.Name     = elemElem.GetAttribute("name");
                            elem.ElemType = elemElem.GetAttrAsEnum("type", defElemType);

                            if (elemGroup.ByteOrderEnabled)
                            {
                                elem.ByteOrderStr = elemElem.GetAttribute("byteOrder");
                                elem.ByteOrder    = ModbusUtils.ParseByteOrder(elem.ByteOrderStr);
                                if (elem.ByteOrder == null)
                                {
                                    elem.ByteOrder = Sett.GetDefByteOrder(elem.Length);
                                }
                            }

                            elemGroup.Elems.Add(elem);
                        }

                        if (0 < elemGroup.Elems.Count && elemGroup.Elems.Count <= DataUnit.GetMaxElemCnt(tableType))
                        {
                            ElemGroups.Add(elemGroup);
                            kpTagInd += elemGroup.Elems.Count;
                        }
                    }
                }

                // загрузка команд
                XmlNode cmdsNode = xmlDoc.DocumentElement.SelectSingleNode("Cmds");
                if (cmdsNode != null)
                {
                    foreach (XmlElement cmdElem in cmdsNode.ChildNodes)
                    {
                        ModbusCmd cmd = new ModbusCmd(
                            cmdElem.GetAttrAsEnum <TableTypes>("tableType"),
                            cmdElem.GetAttrAsBool("multiple"),
                            cmdElem.GetAttrAsInt("elemCnt", 1));
                        cmd.ElemType = cmdElem.GetAttrAsEnum("elemType", cmd.DefElemType);
                        cmd.Address  = (ushort)cmdElem.GetAttrAsInt("address");
                        cmd.Name     = cmdElem.GetAttribute("name");
                        cmd.CmdNum   = cmdElem.GetAttrAsInt("cmdNum");

                        if (cmd.ByteOrderEnabled)
                        {
                            cmd.ByteOrderStr = cmdElem.GetAttribute("byteOrder");
                            cmd.ByteOrder    = ModbusUtils.ParseByteOrder(cmd.ByteOrderStr);
                            if (cmd.ByteOrder == null)
                            {
                                cmd.ByteOrder = Sett.GetDefByteOrder(cmd.ElemCnt);
                            }
                        }

                        if (0 < cmd.CmdNum && cmd.CmdNum <= ushort.MaxValue)
                        {
                            Cmds.Add(cmd);
                        }
                    }
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ModbusPhrases.LoadTemplateError + ":" + Environment.NewLine + ex.Message;
                return(false);
            }
        }