/// <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); } }
/// <summary> /// Копировать модель устройства из заданной /// </summary> public void CopyFrom(DeviceTemplate srcTemplate) { if (srcTemplate == null) { throw new ArgumentNullException("srcTemplate"); } // очистка списков групп элементов и команд ElemGroups.Clear(); Cmds.Clear(); // копирование групп элементов foreach (ElemGroup srcGroup in srcTemplate.ElemGroups) { ElemGroup elemGroup = new ElemGroup(srcGroup.TableType) { Name = srcGroup.Name, Address = srcGroup.Address, Active = srcGroup.Active, StartKPTagInd = srcGroup.StartKPTagInd, StartSignal = srcGroup.StartSignal, }; foreach (Elem srcElem in srcGroup.Elems) { elemGroup.Elems.Add(new Elem() { Name = srcElem.Name, ElemType = srcElem.ElemType, ByteOrder = srcElem.ByteOrder, // копируется ссылка на массив ByteOrderStr = srcElem.ByteOrderStr }); } ElemGroups.Add(elemGroup); } // копирование команд foreach (ModbusCmd srcCmd in srcTemplate.Cmds) { Cmds.Add(new ModbusCmd(srcCmd.TableType) { Multiple = srcCmd.Multiple, ElemCnt = srcCmd.ElemCnt, Address = srcCmd.Address, Name = srcCmd.Name, CmdNum = srcCmd.CmdNum }); } }
/// <summary> /// Loads the configuration from the specified reader. /// </summary> protected override void Load(TextReader reader) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); XmlElement rootElem = xmlDoc.DocumentElement; // template options if ((rootElem.SelectSingleNode("Options") ?? rootElem.SelectSingleNode("Settings")) is XmlElement optionsElem) { Options.LoadFromXml(optionsElem); } // element groups if (rootElem.SelectSingleNode("ElemGroups") is XmlNode elemGroupsNode) { int tagCnt = 0; foreach (XmlElement elemGroupElem in elemGroupsNode.SelectNodes("ElemGroup")) { ElemGroupConfig elemGroupConfig = CreateElemGroupConfig(); elemGroupConfig.LoadFromXml(elemGroupElem); elemGroupConfig.StartTagNum = tagCnt + 1; ElemGroups.Add(elemGroupConfig); tagCnt += elemGroupConfig.Elems.Count; } } // commands if (rootElem.SelectSingleNode("Cmds") is XmlNode cmdsNode) { foreach (XmlElement cmdElem in cmdsNode.SelectNodes("Cmd")) { CmdConfig cmdConfig = CreateCmdConfig(); cmdConfig.LoadFromXml(cmdElem); Cmds.Add(cmdConfig); } } }
/// <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); } }
/// <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); } } } }
/// <summary> /// Loads the template from the XML node. /// </summary> protected virtual void LoadFromXml(XmlNode rootNode) { // loading template settings var settingsNode = rootNode.SelectSingleNode("Settings"); if (settingsNode != null) { Sett.LoadFromXml((XmlElement)settingsNode); } // loading of groups of elements var elemGroupsNode = rootNode.SelectSingleNode("ElemGroups"); if (elemGroupsNode != null) { var elemGroupNodes = elemGroupsNode.SelectNodes("ElemGroup"); var kpTagInd = 0; foreach (XmlElement elemGroupElem in elemGroupNodes) { var 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 (var elem in elemGroup.Elems) { if (elem.ByteOrder == null) { elem.ByteOrder = Sett.GetDefByteOrder(elem.Length); } } } ElemGroups.Add(elemGroup); kpTagInd += elemGroup.Elems.Count; } } } // command loading var cmdsNode = rootNode.SelectSingleNode("Cmds"); if (cmdsNode != null) { var cmdNodes = cmdsNode.SelectNodes("Cmd"); foreach (XmlElement cmdElem in cmdNodes) { var 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); } } } }