public override bool Write(plcdb_lib.Models.Model.TagsRow t, object val) { try { if (!ActiveTags.Select(p => p.TagRow).Contains(t)) { AddTagToGroup(t); } OpcTag ToWrite = ActiveTags.First(p => p.TagRow.PK == t.PK); TagGroup.SyncWriteItems(new int[] { ToWrite.ServerId }, new object[] { val }); return(true); } catch (Exception ex) { throw new Exception("Error writing tag '" + t + "' from OPC server: " + ControllerInfo.Address + "\\" + ControllerInfo.opc_server, ex); } }
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); } }