/// <summary> /// Получить подходящий порядок байт по умолчанию /// </summary> public int[] GetDefByteOrder(int elemCnt) { switch (elemCnt) { case 1: if (defByteOrder2 == null) { defByteOrder2 = ModbusUtils.ParseByteOrder(DefByteOrder2); } return(defByteOrder2); case 2: if (defByteOrder4 == null) { defByteOrder4 = ModbusUtils.ParseByteOrder(DefByteOrder4); } return(defByteOrder4); case 4: if (defByteOrder8 == null) { defByteOrder8 = ModbusUtils.ParseByteOrder(DefByteOrder8); } return(defByteOrder8); default: return(null); } }
/// <summary> /// Loads the group from the XML node. /// </summary> public virtual void LoadFromXml(XmlElement groupElem) { if (groupElem == null) { throw new ArgumentNullException("groupElem"); } Name = groupElem.GetAttribute("name"); Address = (ushort)groupElem.GetAttrAsInt("address"); Active = groupElem.GetAttrAsBool("active", true); XmlNodeList elemNodes = groupElem.SelectNodes("Elem"); int maxElemCnt = MaxElemCnt; ElemType defElemType = DefElemType; foreach (XmlElement elemElem in elemNodes) { if (Elems.Count >= maxElemCnt) { break; } Elem elem = CreateElem(); elem.Name = elemElem.GetAttribute("name"); elem.ElemType = elemElem.GetAttrAsEnum("type", defElemType); if (ByteOrderEnabled) { elem.ByteOrderStr = elemElem.GetAttribute("byteOrder"); elem.ByteOrder = ModbusUtils.ParseByteOrder(elem.ByteOrderStr); } Elems.Add(elem); } }
/// <summary> /// Загрузить настройки шаблона из XML-узла /// </summary> public void LoadFromXml(XmlElement settingsElem) { if (settingsElem == null) { throw new ArgumentNullException("settingsElem"); } ZeroAddr = settingsElem.GetChildAsBool("ZeroAddr", false); DecAddr = settingsElem.GetChildAsBool("DecAddr", true); DefByteOrder2 = settingsElem.GetChildAsString("DefByteOrder2"); DefByteOrder4 = settingsElem.GetChildAsString("DefByteOrder4"); DefByteOrder8 = settingsElem.GetChildAsString("DefByteOrder8"); defByteOrder2 = ModbusUtils.ParseByteOrder(DefByteOrder2); defByteOrder4 = ModbusUtils.ParseByteOrder(DefByteOrder4); defByteOrder8 = ModbusUtils.ParseByteOrder(DefByteOrder8); }
/// <summary> /// Loads the command from the XML node. /// </summary> public virtual void LoadFromXml(XmlElement cmdElem) { if (cmdElem == null) { throw new ArgumentNullException("cmdElem"); } Address = (ushort)cmdElem.GetAttrAsInt("address"); ElemType = cmdElem.GetAttrAsEnum("elemType", DefElemType); ElemCnt = cmdElem.GetAttrAsInt("elemCnt", 1); Name = cmdElem.GetAttribute("name"); CmdNum = cmdElem.GetAttrAsInt("cmdNum"); if (ByteOrderEnabled) { ByteOrderStr = cmdElem.GetAttribute("byteOrder"); ByteOrder = ModbusUtils.ParseByteOrder(ByteOrderStr); } }
/// <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); } }