public static DeviceEntity ImportFromXmlNode(
            ProductionLineEntity line,
            XmlNode node,
            PLCType plcType,
            PLCEntity plcEntity)
        {
            DeviceEntity rlt = null;

            if (node.Name.ToUpper() != "DEVICE")
            {
                return(rlt);
            }

            rlt = new DeviceEntity(line)
            {
                Name    = XMLHelper.GetAttributeStringValue(node, "Name", ""),
                PLCType = plcType,
                DBType  =
                    (SiemensRegisterType)Enum.Parse(
                        typeof(SiemensRegisterType),
                        XMLHelper.GetAttributeStringValue(node, "DBType", "DB")),
                DBNumber      = XMLHelper.GetAttributeIntValue(node, "DBNumber", 0),
                CycleReadMode =
                    (CycleReadMode)Enum.Parse(
                        typeof(CycleReadMode),
                        XMLHelper.GetAttributeStringValue(node, "CycleReadBlock", "ControlBlock")),
                T133LeafID   = XMLHelper.GetAttributeIntValue(node, "T133LeafID", 0),
                T216LeafID   = XMLHelper.GetAttributeIntValue(node, "T216LeafID", 0),
                T107LeafID   = XMLHelper.GetAttributeIntValue(node, "T107LeafID", 0),
                SplitterTime = XMLHelper.GetAttributeIntValue(node, "SplitterTime", 100),
            };
            rlt.BelongPLC.IPAddress = plcEntity.IPAddress;
            rlt.BelongPLC.Rack      = plcEntity.Rack;
            rlt.BelongPLC.Slot      = plcEntity.Slot;

            XmlNode xmlGroup = node.FirstChild;

            while (xmlGroup != null)
            {
                GroupEntity group =
                    GroupEntity.ImportFromXmlNode(
                        rlt,
                        xmlGroup);
                if (group != null)
                {
                    rlt.Groups.Add(group);
                }

                xmlGroup = xmlGroup.NextSibling;
            }

            return(rlt);
        }
Exemple #2
0
        public List <DeviceEntity> ImportDevice(string path)
        {
            List <DeviceEntity> rlt = new List <DeviceEntity>();

            if (path == "")
            {
                return(rlt);
            }

            XmlDocument xml = new XmlDocument();

            try
            {
                xml.Load(path);
            }
            catch (Exception error)
            {
                throw new Exception(
                          $"解析[{Path.GetFileName(path)}]时出错:[{error.Message}]");
            }

            XmlNode root = xml.SelectSingleNode("root");

            if (root == null)
            {
                throw new Exception("配置文件中没有 root 根节点");
            }

            PLCType plcType = PLCType.SIEMENS;
            XmlNode plcNode = root.FirstChild;

            while (plcNode != null)
            {
                if (plcNode.Name.ToUpper() == "SIEMENSPLC")
                {
                    plcType = PLCType.SIEMENS;
                    PLCEntity plcEntity = new PLCEntity()
                    {
                        IPAddress = XMLHelper.GetAttributeStringValue(plcNode, "IPAddress", "127.0.0.1"),
                        Rack      = XMLHelper.GetAttributeIntValue(plcNode, "Rack", 0),
                        Slot      = XMLHelper.GetAttributeIntValue(plcNode, "Slot", 0),
                    };

                    XmlNode deviceNode = plcNode.FirstChild;
                    while (deviceNode != null)
                    {
                        DeviceEntity device =
                            DeviceEntity.ImportFromXmlNode(
                                this,
                                deviceNode,
                                plcType,
                                plcEntity);
                        if (device != null)
                        {
                            Devices.Add(device, _addToWholeEntityQueueHandler);
                            rlt.Add(device);
                        }

                        deviceNode = deviceNode.NextSibling;
                    }
                }

                plcNode = plcNode.NextSibling;
            }

            return(rlt);
        }