From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple DataSockect at run time

I am in the process of evaluating Measurement studio 8. Development environment is VisualStudio.Net 2003, language is C#.
 
I have a client application that needs to connect to OPC server and get real time data. For a sigle OPC item I can use DataSocket and get the value with DataUpdated event handler. With the new requirement I will be loading OPC itmes name from a text file at rutime and reading the realtime data from OPC Server.
 
Does anybody know how to access multple OPC item at run time if you dont know the items at design time?? Is there any other solution than DataSocket?
Any code sample would be helpful.
 
-Ali
0 Kudos
Message 1 of 7
(3,893 Views)

You can dynamically create a DataSocket object per URL and call the Connect method at run time. I posted some code below that shows this. You will need to add error checking to handle bad URLs or failed connections and you will need to make sure that you properly close your connections and dispose them to clean up properly, but this should give you an idea of what to do.


ArrayList connectionList = new ArrayList();
using (StreamReader sr = new StreamReader(urlFilePath))
{
    string url;
    while ((url = sr.ReadLine()) != null)
    {
        DataSocket ds = new DataSocket();
        ds.Connect(url, AccessMode.ReadAutoUpdate);
        connectionList.Add(ds);
    }
}


How many connections will your application typically make and what is the rate at which you will need to handle data update events? Before deciding on this approach, you should ensure that it can handle the number of connection and data rates that you need.

0 Kudos
Message 2 of 7
(3,869 Views)
Thanks for your reply.
 
My application will make typically 50 connections and the update rate would be 100 ms.
How would you handle the the DataUpdated event handler for dynamically created DataSockets at runtime??
 
- Ali
0 Kudos
Message 3 of 7
(3,865 Views)
You can attach a delegate to the event as follows:
 
ds.DataUpdated += new DataUpdatedEventHandler(ds_DataUpdated);
 
In this example, ds_DataUpdated needs to be defined in the class within which the delegate is being created. However, it is also possible to specify an object and a method to the constructor of the DataUpdatedEventHandler delegate to handle the DataUpdated event in another class.
 
With the number of connections and throughput you are expecting, you should definitely put together a prototype to verify that this approach will handle your performance requirements.
 
 
0 Kudos
Message 4 of 7
(3,861 Views)
There is an example of handling multiple datasocket updates with one callback here.

The example demonstrates reading multiple shared variables from a LabVIEW VI.  If you do not have LabVIEW, you can still take a look at the C# code to see how multiple datasocket objects can be initialized dynamically.

Regards,

Tyler
0 Kudos
Message 5 of 7
(3,855 Views)
Thanks a lot to drohacek and Tyler. I really appriciate your help. I will try that with my specifications to check if I can get the required throughput.
 
Thanks again,
 
-Ali
0 Kudos
Message 6 of 7
(3,850 Views)

I'd like to add a couple of things.

1) Keep in mind that when you dynamically create data socket objects and listen to the data updated event handler, the callback when data is received is *not* guaranteed to be coming in on the same thread as that on which the data socket was instantiated.

dataUpdatedEventHandler(object sender, DataUpdatedEventArgs e)
{
  // this call could be coming in on any thread...
}

2) In terms of keeping track of which data is coming in from where, you can check the URL property on data socket callback. Editing the useful code snippet that was posted earlier:

ArrayList connectionList = new ArrayList();
using (StreamReader sr = new StreamReader(urlFilePath))
{
    string url;
    while ((url = sr.ReadLine()) != null)
    {
        DataSocket ds = new DataSocket();
        ds.Connect(url, AccessMode.ReadAutoUpdate);

        ds.DataUpdated += dataUpdated
        connectionList.Add(ds);
    }
}

void DataUpdated(object sender, DataUpdatedEventArgs e)

{

  (sender as DataSocket).URL <- tells you which item was updated

}

0 Kudos
Message 7 of 7
(3,794 Views)