Best way of connecting to a specific PI Collective member in C# using AF SDK

I have recently started playing around with development using the PI AF SDK and C#. I have been looking for a way to connect to only one member of a PI Collective. I have been testing the following code snippet, but I am not sure if this is the best way of doing it.

static void Main(string[] args)
{
    string piServerName = "myCollectiveName";

    PIServers myPIServers = new PIServers();
    PIServer myPIServer = myPIServers[piServerName];

    try
    {
        if (myPIServer.Collective.Members.Count > 1)
        {
            Console.WriteLine(myPIServer.Collective.Members.Count);

            //Connect to a specific collective member name.
            PICollectiveMember myPiCollMember = myPIServer.Collective.Members["myMemberName"];
            Console.WriteLine("The currently connected collective member name is: {0}", myPiCollMember.Name);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error connecting to PI Server: {ex.Message}");
    }
    finally
    {
        myPIServer.Disconnect();
    }
}

This seems to work, and I can specify the member to connect to. My use case for wanting to control the collective member is to pull data only from a specific member where historic archives from more then 10 year back are connected. Please let me know if there are better ways of doing this.

Parents
  • Gregor is correct that you should use the ConnectDirect method. Your code does not actually connect to a member as a PIServer object. Rather you are interacting with it as a PICollectiveMember object, which is quite different. You can have a member object in your code but its IsConnected property could be false. And the available methods and properties for a PICollectiveMember are very different than with a PIServer object.

     

Reply
  • Gregor is correct that you should use the ConnectDirect method. Your code does not actually connect to a member as a PIServer object. Rather you are interacting with it as a PICollectiveMember object, which is quite different. You can have a member object in your code but its IsConnected property could be false. And the available methods and properties for a PICollectiveMember are very different than with a PIServer object.

     

Children