c# opc协议,连接plc

 原网址:https://www.coder.work/article/3045229

 我正在使用来自 OPCFoundation 的两个 .dll 文件 - opcNetApi.dll 和 opcNetApi.Com.dll

 

private void readplc() { Opc.URL url = new Opc.URL("opcda://48.5.0.05/RSLinx OPC Server"); Opc.Da.Server server = null; OpcCom.Factory fact = new OpcCom.Factory(); server = new Opc.Da.Server(fact, null); try { server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); } catch (Exception exy) { MessageBox.Show(exy.Message); } // Create a group Opc.Da.Subscription group; Opc.Da.SubscriptionState groupState = new Opc.Da.SubscriptionState(); groupState.Name = "Group"; groupState.Active = true; group = (Opc.Da.Subscription)server.CreateSubscription(groupState); // add items to the group. Opc.Da.Item[] items = new Opc.Da.Item[6]; items[0] = new Opc.Da.Item(); items[0].ItemName = "[ALARM]F20:9"; items[1] = new Opc.Da.Item(); items[1].ItemName = "[ALARM]F22:30"; items[2] = new Opc.Da.Item(); items[2].ItemName = "[ALARM]F22:6"; items[3] = new Opc.Da.Item(); items[3].ItemName = "[ALARM]F18:8"; items[4] = new Opc.Da.Item(); items[4].ItemName = "[ALARM]F22:32"; items[5] = new Opc.Da.Item(); items[5].ItemName = "[ALARM]F22:5"; items = group.AddItems(items); try { group.DataChanged += new Opc.Da.DataChangedEventHandler(OnTransactionCompleted); // COM EXCEPTION THROWN HERE Console.ReadLine(); } catch (Exception ex) { MessageBox.Show(ex.Message); Console.ReadKey(); } } private void OnTransactionCompleted(object group, object hReq, Opc.Da.ItemValueResult[] items) { for (int i = 0; i < items.GetLength(0); i++) { } }

posted @ 2023-09-21 16:18  xxb1  阅读(203)  评论(0编辑  收藏  举报