public OPC(Model.ControllersRow controllerInfo) { try { ControllerInfo = controllerInfo; String OpcName = "plcdb-" + ControllerInfo.PK; Server = new DAServer(ControllerInfo.opc_server, ControllerInfo.Address); TagGroup = Server.AddGroup(Interlocked.Increment(ref GroupClientId), OpcName, true, 100, (float)0.0); } catch (Exception ex) { Log.Error("Error creating OPC connection: " + ex.Message); } }
private void AddTagToGroup(Model.TagsRow tag) { try { if (TagGroup == null) { String OpcName = "plcdb-" + ControllerInfo.PK; TagGroup = Server.AddGroup(Interlocked.Increment(ref GroupClientId), OpcName, true, 100, (float)0.0); } OpcTag NewTag = new OpcTag(); NewTag.TagRow = tag; NewTag.Item = new Item() { //AccessPath = TagRow.Address, Active = true, ClientId = (int)tag.PK, ItemId = tag.Address, RequestedDataType = VarEnum.VT_EMPTY// TagRow.IsDataTypeNull() ? VarEnum.VT_UNKNOWN : TypeToOpcType(TagRow.DataType) }; NewTag.ItemValue = new ItemValue(); var TagResult = TagGroup.AddItems(new Item[] { NewTag.Item }).First(); NewTag.ItemResult = TagResult; ActiveTags.Add(NewTag); //add event handler -- make sure we are not adding a duplicate copy TagGroup.ReadComplete -= TagGroup_ReadComplete; TagGroup.DataChange -= TagGroup_ReadComplete; TagGroup.ReadComplete += TagGroup_ReadComplete; TagGroup.DataChange += TagGroup_ReadComplete; } catch (Exception ex) { throw new Exception("Error adding tag '" + tag + "' to group on OPC server: " + ControllerInfo.Address + "\\" + ControllerInfo.opc_server, ex); } }