I used OPCDotNetLib but can\'t read data from remote OPC Server there .
I can connect , like
Type typeofOPCserver = Type.GetTypeFromProgID(clsidOPCserve
I know it has been a while but to help those who need the libraries. To go along with Greg Buehlers answer. They are Free and part of the core OPC Foundation and can be found here click on Archives tab if you need previous versions. It took me forever to find them so I hope this may help someone.
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<Opc.Da.Item> 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<Opc.Da.Item>(); // I'm using a List<T>, 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;