Connection with PI Server C#

16 Oct

First of all you should include the PISDK reference (DLL Files) in your project.

using PISDK;
Server _piserver; /// Make
string strConnectionString = null;
strConnectionString = “UID=pidemo”;
_piserver = myPISDK.Servers[“testsrvr4”]; // You should specify your own parameters
_piserver.Open(strConnectionString);

DateTime startTime = DateTime.Now.AddSeconds(-1);
DateTime endTime = DateTime.Now;
TimeSpan span = new TimeSpan(0, 5, 0);

List values = GetValuesOfPointAsStrings(“MahtabPoint”, startTime, endTime, span); //”MahtabPoint” is my own specified point. You will have to insert it your own in order to get its value.

public List GetValuesOfPoint(string pointName, DateTime startTime, DateTime endTime, TimeSpan span)
{
List values = new List();
PIPoint point = _piserver.PIPoints[pointName];
DateTime tmpTime = startTime;

while (tmpTime < endTime)
{
values.Add(point.Data.ArcValue(tmpTime, RetrievalTypeConstants.rtAtOrBefore));
tmpTime += span;

}
//lbl_test.Text += point.Name.ToString();
return values;
}

public List GetValuesOfPointAsStrings(string pointName, DateTime startTime, DateTime endTime, TimeSpan span)
{
List stringValues = new List();

List values = GetValuesOfPoint(pointName, startTime, endTime, span);

foreach (PIValue value in values)
{
if (value.Value.GetType().IsCOMObject)
{
stringValues.Add((value.Value as DigitalState).Name.ToString());
}
else
{
stringValues.Add((value.Value).ToString());
}
}

return stringValues;
}

Advertisements

7 Responses to “Connection with PI Server C#”

  1. Chaman K Lalwani October 16, 2012 at 10:40 PM #

    Hi Mahtab, this is very useful article. Actually i am new to C# so please tell me about PI Server why we use it?

  2. Simas May 14, 2013 at 5:56 PM #

    Thank you for article!

    I cannot find the PI SDK to use as it was used on this article. Can you give me direct link to download the references (dll’s) for VS2008? Im creating C# application.

    • mahtabrasheed195 May 14, 2013 at 6:44 PM #

      Dear you have to purchase the PI SDK for development. My company purchased that from OSISOFT.

      Here is the link

      You might have to register to download.

      • Simas May 15, 2013 at 12:45 AM #

        Thank you for info! Keep up good work!

  3. RyanfaeScotland (@RyanfaeScotland) July 16, 2014 at 7:17 PM #

    New to C# and PI so this was a useful article to me.

    Can I point out to other readers that the reference to ‘myPISDK’ is a PISDK object. If you are getting problems with it add ‘PISDK.PISDK myPISDK = new PISDK.PISDK();’ and it should work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: