Tag Archives: connection

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;
}