static void Main(string[] args) { var client = new OpcClient("opc.tcp://127.0.0.1:49320"); client.Security.UserIdentity = new OpcClientIdentity("Tim", "1qaz2wsx3edc4rfv"); client.Security.EndpointPolicy = new OpcSecurityPolicy(OpcSecurityMode.SignAndEncrypt, OpcSecurityAlgorithm.Basic256); client.Connect(); // 一次寫取多個Tag OpcWriteNode[] wCommands = new OpcWriteNode[] { new OpcWriteNode("ns=2;s=Channel2.Device1.Tag1", false), // 寫 boolean new OpcWriteNode("ns=2;s=Channel2.Device1.Tag2", "Test"), // 寫 sting new OpcWriteNode("ns=2;s=Channel2.Device1.Tag3", 8.7), // 寫 float new OpcWriteNode("ns=2;s=Channel2.Device1.Tag3", (ushort)88) // 寫 word }; OpcStatusCollection results = client.WriteNodes(wCommands); // 一次讀取多個Tag OpcReadNode[] rCommands = new OpcReadNode[] { new OpcReadNode("ns=2;s=Channel2.Device1.Tag1"), new OpcReadNode("ns=2;s=Channel2.Device1.Tag2"), new OpcReadNode("ns=2;s=Channel2.Device1.Tag3"), new OpcReadNode("ns=2;s=Channel2.Device1.Tag4") }; IEnumerable <OpcValue> job = client.ReadNodes(rCommands); int i = 0; foreach (OpcValue value in job) { Console.WriteLine("ReadNode: {0},\t = {1}", rCommands[i].NodeId, value); i++; } // 訂閱Tag5 OpcSubscription subscription = client.SubscribeDataChange("ns=2;s=Channel2.Device1.Tag5", HandleDataChanged); subscription.PublishingInterval = 1000; subscription.ApplyChanges(); Console.ReadLine(); client.Disconnect(); }
public OpcSubscription Subscribe(Common.DataSubscription subscriptionDef) { string[] nodes = subscriptionDef.monitored_items.Split("$"); string[] nodeparts; int ns; string path; List <OpcSubscribeNode> nodesList = new List <OpcSubscribeNode>(); OpcDataChangeFilter filter; if (subscriptionDef.report_on_timestamp_change) { filter = new OpcDataChangeFilter(OpcDataChangeTrigger.StatusValueTimestamp); } else { filter = new OpcDataChangeFilter(OpcDataChangeTrigger.StatusValue); } switch (subscriptionDef.deadband_type.ToLower()) { case "percent": filter.DeadbandType = OpcDeadbandType.Percent; break; case "absolute": filter.DeadbandType = OpcDeadbandType.Absolute; break; default: filter.DeadbandType = OpcDeadbandType.None; break; } filter.DeadbandValue = subscriptionDef.deadband; // create an empty subscription OpcSubscription sub = _client.SubscribeNodes(); // create a monitoredItem for each tag to be monitored and add it to the subscription OpcMonitoredItem thisItem; foreach (string node in nodes) { nodeparts = node.Split(":"); ns = int.Parse(nodeparts[0]); path = nodeparts[1]; thisItem = new OpcMonitoredItem(new OpcNodeId(path, ns), OpcAttribute.Value); thisItem.DataChangeReceived += DataChangeReceived; thisItem.Tag = Guid.Parse(nodeparts[2]); thisItem.Filter = filter; sub.AddMonitoredItem(thisItem); } //set the interval (milliseconds, 0 = whenever the value changes) sub.PublishingInterval = subscriptionDef.interval; sub.PublishingIsEnabled = true; // make the server aware of the changes to the subscription sub.ApplyChanges(); // set the Tag property of the subscription to the DataSubscription object that came from the database for later reference sub.Tag = subscriptionDef; // set the subscription-enabled status if (subscriptionDef.enabled) { sub.ChangeMonitoringMode(OpcMonitoringMode.Reporting); } else { sub.ChangeMonitoringMode(OpcMonitoringMode.Disabled); } sub.StartPublishing(); return(sub); }