OPC Client - how to read from Remote OPC Server

后端 未结 2 1701
不知归路
不知归路 2021-02-02 04:36

I used OPCDotNetLib but can\'t read data from remote OPC Server there .

I can connect , like

Type typeofOPCserver = Type.GetTypeFromProgID(clsidOPCserve         


        
2条回答
  •  一向
    一向 (楼主)
    2021-02-02 05:29

    You've got a few options for OPC compatibility. You can use the classical COM wrappers provided by OPC Foundation, or you can use the newer OPC library designed for .Net.

    I haven't personally switched over to the newer library yet, but this is a break down of how to use OpcNetApi.dll,OpcNetApi.Com.dll, and OpcRcw.Da.dll to initialize a connection and subscribe to the DataChanged event:

    Opc.Da.Server scadaServer = null;
    List scadaItems = null;
    Opc.Da.Subscription scadaSubscription = null;
    
    string scadaUrl = string.Format("opcda://{0}/{1}", hostname,
                                                       opcServerVendor);
    
    scadaServer = new Opc.Da.Server(new OpcCom.Factory(), new Opc.URL(scadaUrl));
    scadaServer.Connect();
    
    var scadaItems = new List(); // I'm using a List, but cast back to a simple array using ToArray();
    
    // Repeat this next part for all the items you need to subscribe
    Opc.Da.Item item = new Opc.Da.Item();
    item.ItemName = TagPath; // Where TagPath is something like device.channel.tag001;
    item.ClientHandle = handle; // handle is up to you, but i use a logical name for it
    item.Active = true;
    item.ActiveSpecified = true;
    
    scadaItems.Add(item);
    
    Opc.Da.SubscriptionState subscriptionState = new Opc.Da.SubscriptionState();
    subscriptionState.Active = true;
    subscriptionState.UpdateRate = 40;
    subscriptionState.Deadband = 0;
    
    scadaSubscription = scadaSubscription ?? (Opc.Da.Subscription)scadaServer.CreateSubscription(subscriptionState);
    
    Opc.Da.ItemResult[] result = scadaSubscription.AddItems(scadaItems.ToArray());
    for (int i = 0; i < result.Length; i++)
        scadaItems[i].ServerHandle = result[i].ServerHandle;
    
    scadaSubscription.DataChanged += new Opc.Da.DataChangedEventHandler(OnDataChange);
    scadaSubscription.State.Active = true;
    

提交回复
热议问题