private Int16 GetTagCanonicalType(string itemName) { const int ItemCanonicalDataTypeProperty = 1; TreeNode rootNode = uiGroupsTab_AllFoldersTreeView.SelectedNode; while (rootNode.Parent != null) { rootNode = rootNode.Parent; } var opcServer = Settings.OpcServers.Where(os => rootNode.FullPath == os.ServerName).Single(); string urlstring = opcServer.URL; Server server = new Server(new Factory(), new URL(urlstring)); server.Connect(); ItemIdentifier [] itemIdentifiers = new ItemIdentifier[] {new ItemIdentifier(itemName)}; PropertyID[] propertyIDs = new PropertyID[] { new PropertyID(ItemCanonicalDataTypeProperty) }; var result = server.GetProperties(itemIdentifiers, propertyIDs, true); server.Disconnect(); var resultType = result[0][0].Value.GetType(); PropertyInfo nameProperty = resultType.GetProperty("Name"); string name = nameProperty.GetValue(result[0][0].Value, null) as string; switch (name.ToLower()) { case "string": case "char": return 0; case "int16": case "int32": case "int64": case "uint16": case "uint32": case "uint64": return 1; case "boolean": return 2; case "decimal": case "double": return 3; case "datetime": case "datetimeoffset": return 4; default: return 0; } }
/// <summary> /// Reads the current complex data item properties from the server. /// </summary> public void Update(Opc.Da.Server server) { // clear the existing state. Clear(); // check if the item supports any of the complex data properties. ItemPropertyCollection[] results = server.GetProperties( new ItemIdentifier[] { this }, CPX_PROPERTIES, true); // unexpected return value. if (results == null || results.Length != 1) { throw new ApplicationException("Unexpected results returned from server."); } // update object. if (!Init((ItemProperty[])results[0].ToArray(typeof(ItemProperty)))) { throw new ApplicationException("Not a valid complex item."); } // check if data filters are suppported for the item. GetDataFilterItem(server); }
public void WithItemProperties(Opc.Da.Server server, string itemName, Action <ItemPropertyCollection> action, Action <Exception> onError = null) { try { var prop = server.GetProperties(new[] { new ItemIdentifier(itemName) }, PropertyIds, false).Single(); action(prop); } catch (InvalidOperationException) { throw new Exception($"Item name<{itemName}> does not exist on server<{server.Url}>"); } }
public void Update(Opc.Da.Server server) { this.Clear(); ItemPropertyCollection[] propertysArray = server.GetProperties(new ItemIdentifier[] { this }, CPX_PROPERTIES, true); if ((propertysArray == null) || (propertysArray.Length != 1)) { throw new ApplicationException("Unexpected results returned from server."); } if (!this.Init((ItemProperty[])propertysArray[0].ToArray(typeof(ItemProperty)))) { throw new ApplicationException("Not a valid complex item."); } this.GetDataFilterItem(server); }
public string GetTypeDictionary(Opc.Da.Server server) { PropertyID[] propertyIDs = new PropertyID[] { Property.DICTIONARY }; ItemPropertyCollection[] propertysArray = server.GetProperties(new ItemIdentifier[] { this.m_dictionaryItemID }, propertyIDs, true); if (((propertysArray == null) || (propertysArray.Length == 0)) || (propertysArray[0].Count == 0)) { return(null); } ItemProperty property = propertysArray[0][0]; if (!property.ResultID.Succeeded()) { return(null); } return((string)property.Value); }
/// <summary> /// Gets the datatype of an OPC tag /// </summary> /// <param name="tag">Tag to get datatype of</param> /// <returns>System Type</returns> public System.Type GetDataType(string tag) { var item = new OpcDa.Item { ItemName = tag }; OpcDa.ItemProperty result; try { var propertyCollection = _server.GetProperties(new[] { item }, new[] { new OpcDa.PropertyID(1) }, false)[0]; result = propertyCollection[0]; } catch (NullReferenceException) { throw new OpcException("Could not find node because server not connected."); } return(result.DataType); }
private void GetProperties(System.Windows.Forms.TreeNode node) { try { Opc.Da.Server server = this.FindServer(node); BrowseElement browseElement = null; if (node.Tag != null && node.Tag.GetType() == typeof(BrowseElement)) { browseElement = (BrowseElement)node.Tag; } if (browseElement.IsItem) { node.Nodes.Clear(); ItemIdentifier itemIdentifier = new ItemIdentifier(browseElement.ItemPath, browseElement.ItemName); ItemPropertyCollection[] properties = server.GetProperties(new ItemIdentifier[] { itemIdentifier }, this.m_filters.PropertyIDs, this.m_filters.ReturnPropertyValues); if (properties != null) { ItemPropertyCollection[] array = properties; for (int i = 0; i < array.Length; i++) { ItemPropertyCollection itemPropertyCollection = array[i]; foreach (ItemProperty property in itemPropertyCollection) { this.AddItemProperty(node, property); } browseElement.Properties = (ItemProperty[])itemPropertyCollection.ToArray(typeof(ItemProperty)); } } node.Expand(); if (this.ElementSelected != null) { this.ElementSelected(browseElement); } } } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } }
/// <summary> /// Fetches the type description for the item. /// </summary> public string GetTypeDescription(Opc.Da.Server server) { ItemPropertyCollection[] results = server.GetProperties( new ItemIdentifier[] { m_typeItemID }, new PropertyID[] { Property.TYPE_DESCRIPTION }, true); if (results == null || results.Length == 0 || results[0].Count == 0) { return(null); } ItemProperty property = results[0][0]; if (!property.ResultID.Succeeded()) { return(null); } return((string)property.Value); }
/// <summary> /// Сигнал со словарем свойств сигнала (propID массив идентификаторов сигнала) /// </summary> public static string GetPropertyValue(Opc.Da.Server server_da, string tagName, int PropID) { string tempString = ""; try //Соединяемся с сервером и считываем свойство сигнала PropID { EventLog.WriteEntry("test", tagName); Opc.Da.Item[] temp = new Opc.Da.Item[1]; temp[0] = new Opc.Da.Item(new Opc.ItemIdentifier(tagName)); Opc.Da.ItemValueResult[] values = server_da.Read(temp); PropertyID[] propId = new PropertyID[1]; ItemIdentifier[] itemProp = new ItemIdentifier[] { new ItemIdentifier(tagName) }; ItemPropertyCollection[] propCollection; //EventLog.WriteEntry("test", "GetProperties - " + itemProp[0].ItemName + "; name - " + propId[0].Name); propCollection = server_da.GetProperties(itemProp, null, true); //EventLog.WriteEntry("test", "GetProperties - ok"); if (propCollection.Count() > 0) { foreach (Opc.Da.ItemProperty prop in propCollection[0]) { if (prop.ID.Code == PropID) { tempString = System.Convert.ToString(prop.Value); } } } } catch //В случае неудачного чтения, возвращаем null { tempString = null; } return(tempString); }