Exemplo n.º 1
0
        /// <summary>
        /// Copies the template properties from the source template.
        /// </summary>
        public virtual void CopyFrom(DeviceTemplate srcTemplate)
        {
            if (srcTemplate == null)
            {
                throw new ArgumentNullException("srcTemplate");
            }

            // копирование настроек шаблона
            Sett.CopyFrom(srcTemplate.Sett);

            // копирование групп элементов
            ElemGroups.Clear();
            foreach (ElemGroup srcGroup in srcTemplate.ElemGroups)
            {
                ElemGroup destGroup = CreateElemGroup(srcGroup.TableType);
                destGroup.CopyFrom(srcGroup);
                ElemGroups.Add(destGroup);
            }

            // копирование команд
            Cmds.Clear();
            foreach (ModbusCmd srcCmd in srcTemplate.Cmds)
            {
                ModbusCmd destCmd = CreateModbusCmd(srcCmd.TableType, srcCmd.Multiple);
                destCmd.CopyFrom(srcCmd);
                Cmds.Add(destCmd);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Copies the command properties from the source command.
        /// </summary>
        public virtual void CopyFrom(ModbusCmd srcCmd)
        {
            if (srcCmd == null)
            {
                throw new ArgumentNullException("srcCmd");
            }

            ElemCnt = srcCmd.ElemCnt;
            Address = srcCmd.Address;
            Name    = srcCmd.Name;
            CmdNum  = srcCmd.CmdNum;
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Loads the template from the XML node.
        /// </summary>
        protected virtual void LoadFromXml(XmlNode rootNode)
        {
            // загрузка настроек шаблона
            XmlNode settingsNode = rootNode.SelectSingleNode("Settings");

            if (settingsNode != null)
            {
                Sett.LoadFromXml((XmlElement)settingsNode);
            }

            // загрузка групп элементов
            XmlNode elemGroupsNode = rootNode.SelectSingleNode("ElemGroups");

            if (elemGroupsNode != null)
            {
                XmlNodeList elemGroupNodes = elemGroupsNode.SelectNodes("ElemGroup");
                int         kpTagInd       = 0;

                foreach (XmlElement elemGroupElem in elemGroupNodes)
                {
                    ElemGroup elemGroup = CreateElemGroup(elemGroupElem.GetAttrAsEnum <TableType>("tableType"));
                    elemGroup.StartKPTagInd = kpTagInd;
                    elemGroup.StartSignal   = kpTagInd + 1;
                    elemGroup.LoadFromXml(elemGroupElem);

                    if (elemGroup.Elems.Count > 0)
                    {
                        if (elemGroup.ByteOrderEnabled)
                        {
                            foreach (Elem elem in elemGroup.Elems)
                            {
                                if (elem.ByteOrder == null)
                                {
                                    elem.ByteOrder = Sett.GetDefByteOrder(elem.Quantity);
                                }
                            }
                        }

                        ElemGroups.Add(elemGroup);
                        kpTagInd += elemGroup.Elems.Count;
                    }
                }
            }

            // загрузка команд
            XmlNode cmdsNode = rootNode.SelectSingleNode("Cmds");

            if (cmdsNode != null)
            {
                XmlNodeList cmdNodes = cmdsNode.SelectNodes("Cmd");

                foreach (XmlElement cmdElem in cmdNodes)
                {
                    ModbusCmd cmd = CreateModbusCmd(
                        cmdElem.GetAttrAsEnum <TableType>("tableType"),
                        cmdElem.GetAttrAsBool("multiple"));
                    cmd.LoadFromXml(cmdElem);

                    if (cmd.ByteOrderEnabled && cmd.ByteOrder == null)
                    {
                        cmd.ByteOrder = Sett.GetDefByteOrder(cmd.ElemCnt);
                    }

                    if (cmd.CmdNum > 0)
                    {
                        Cmds.Add(cmd);
                    }
                }
            }
        }