public PlcVersion(string versionString) { InitializeMembers(); SupportedExecuters = new SupportedExecuterTypes(0); ParseVersion(versionString); m_receiveBufferSize = m_plcBuffer; m_sendBufferSize = m_plcBuffer; }
private void SetPLCDetailsFromOSXMLElement(XmlElement childElem) { string[] xmlVersionString; string[] responseOSVersion; int xmlWeightedOSFromVersion; int xmlWeightedOSToVersion; int plcWeightedOSVersion; string fromVersion = ""; string toVersion = ""; // The code had the From='Value' To='Value' missing // I'm just offering a code which shorter by assuming that there cannot be a version before 0.0 // and that there cannot be a version after 999.999 // Weighted Version is calculated as Major*1000 + Minor fromVersion = childElem.Attributes[Utils.HelperPlcVersion.xmlFromVersion].Value.ToString(); toVersion = childElem.Attributes[Utils.HelperPlcVersion.xmlToVersion].Value.ToString(); responseOSVersion = m_osVersion.Replace('?', '0').Split('.'); plcWeightedOSVersion = Convert.ToInt32(responseOSVersion[0]) * 1000 + Convert.ToInt32(responseOSVersion[1]); if (fromVersion == "*") { xmlWeightedOSFromVersion = 0; } else { xmlVersionString = fromVersion.Split('.'); xmlWeightedOSFromVersion = Convert.ToInt32(xmlVersionString[0]) * 1000 + Convert.ToInt32(xmlVersionString[1]); } if (toVersion == "*") { xmlWeightedOSToVersion = 999999; } else { xmlVersionString = toVersion.Split('.'); xmlWeightedOSToVersion = Convert.ToInt32(xmlVersionString[0]) * 1000 + Convert.ToInt32(xmlVersionString[1]); } if ((plcWeightedOSVersion >= xmlWeightedOSFromVersion) && (plcWeightedOSVersion <= xmlWeightedOSToVersion)) { SetPLCExecuterType(Convert.ToInt32(childElem.Attributes[Utils.HelperPlcVersion.xmlExecuter].Value)); setOperandsCount(Convert.ToInt32(childElem.Attributes[Utils.HelperPlcVersion.xmlOpMemoryMapID].Value)); m_plcBuffer = Convert.ToInt32(childElem.Attributes[Utils.HelperPlcVersion.xmlBufferSize].Value); SupportedExecuters = new SupportedExecuterTypes( Convert.ToInt32(childElem.Attributes[Utils.HelperPlcVersion.xmlSupportedExecuters].Value)); } }
private void SetPlcType() { string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase .Replace(@"file:///", "")); string dirSeparator = Path.DirectorySeparatorChar.ToString(); if (path.Substring(path.Length - 1) == dirSeparator) { path = path.Substring(0, path.Length - 1); } path += dirSeparator + "PLCModels.xml"; XmlDocument xmlDoc = new XmlDocument(); if (!PLCFactory.WorkWithXmlInternally) { // If the PLCModels.xml file doesn't exist // create it from resource. if (!File.Exists(path)) { try { File.WriteAllText(path, ComDriverResource.PLCModels); } catch { throw new ComDriveExceptions("Error creating file 'PLCModels.xml'", ComDriveExceptions.ComDriveException.CannotCreateFile); } } else { // Check if the existing XML version should be replaced with a new one. int versionFromExistingFile = getXmlVersionFromFile(path); int versionInComDrive = getXmlVersionForFileInComDrive(); if (versionInComDrive > versionFromExistingFile) { // If the XML Version in the Com Drive is newer than overwrite the XML file try { File.WriteAllText(path, ComDriverResource.PLCModels); } catch { throw new ComDriveExceptions("Error creating file 'PLCModels.xml'", ComDriveExceptions.ComDriveException.CannotCreateFile); } } } // Load the PLCModels.xml from local drive. xmlDoc.Load(path); } else { MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(ComDriverResource.PLCModels)); xmlDoc.Load(ms); } XmlNodeList nodeList = xmlDoc.GetElementsByTagName("PLC"); IEnumerator enumerator = nodeList.GetEnumerator(); while (enumerator.MoveNext()) { XmlElement xmlElem = (XmlElement)enumerator.Current; if (xmlElem.HasAttribute("Reply") && xmlElem.HasAttribute("Model")) { bool isRegex = false; if (xmlElem.HasAttribute("ExpressionType") && xmlElem.Attributes["ExpressionType"].Value == "Regex") { isRegex = true; } if (xmlElem.Attributes["Reply"].Value.ToString() == plcReply || (isRegex && Regex.IsMatch(plcReply, xmlElem.Attributes["Reply"].Value))) { if (isRegex) { var match = Regex.Match(plcReply, xmlElem.Attributes["Reply"].Value).Groups[1].Value.Trim(); m_oplcModel = xmlElem.Attributes["Model"].Value.Replace("*", match); } else { m_oplcModel = xmlElem.Attributes["Model"].Value.ToString(); } if (xmlElem.HasAttribute("SuppressEthernetHeader")) { bool.TryParse(xmlElem.Attributes["SuppressEthernetHeader"].Value, out _suppressEthernetHeader); } if (xmlElem.HasChildNodes) { XmlNodeList childs = xmlElem.ChildNodes; IEnumerator childsEnumerator = childs.GetEnumerator(); while (childsEnumerator.MoveNext()) { XmlElement childElem = (XmlElement)childsEnumerator.Current; if (childElem.Name.Equals("OS") && ValidateOSXMLElement(childElem)) { SetPLCDetailsFromOSXMLElement(childElem); } else { if (childElem.Name.Equals("PreBoot") || childElem.Name.Equals("Boot")) { SetPLCExecuterType(Convert.ToInt32(childElem .Attributes[Utils.HelperPlcVersion.xmlExecuter].Value)); if (childElem.HasAttribute(Utils.HelperPlcVersion.xmlOpMemoryMapID)) { setOperandsCount(Convert.ToInt32(childElem .Attributes[Utils.HelperPlcVersion.xmlOpMemoryMapID].Value)); } m_plcBuffer = Convert.ToInt32(childElem .Attributes[Utils.HelperPlcVersion.xmlBufferSize].Value); SupportedExecuters = new SupportedExecuterTypes( Convert.ToInt32(childElem .Attributes[Utils.HelperPlcVersion.xmlSupportedExecuters].Value)); } } } } return; } } } }