C#调用PI实时库API

时间:2024-04-16 08:46:07

打开数据库

 void PIOpen() 

{
        PISDK.PISDK g_SDK = new PISDK.PISDKClass();//创建一个pisdk对象

        PISDK.Server myserver = g_SDK.Servers["192.168.1.1"];

        strConnectionString = "path=192.168.1.1;uid=piadmin;pwd=";
        myserver.ConnectTimeout = 20;
        myserver.Timeout = 60;
        try
        {
            if (!myserver.Connected)
            {

                myserver.Open(strConnectionString);
            }
        }
        catch (Exception ex)
        {
            throw new Exception();
        }

 }

关闭数据库

void PIClose() 
{
        if (myserver.Connected)
        {
            myserver.Close();
        }
}

获取某个点的实时值

public Point Pi_GetPointSnapshot(string tagName) 
{
        string value = string.Empty;
        Point p = new Point();
        try
        {
            PIOpen(); //打开数据库
            PISDK.PIPoints piPoints = myserver.PIPoints;
            PISDK.PIPoint snapShot = piPoints[tagName];
            PISDK.PIValue myValue = snapShot.Data.Snapshot;
            PISDK.PointAttributes pa = snapShot.PointAttributes;
            PISDKCommon._NamedValues nvs = pa.GetAttributes();

            p.TagName = tagName;                                               //点名

            p.Pvalue= myValue.Value.ToString();                                //点值
           
            Object valIndex=16;
            p.Description = nvs.get_Item(ref valIndex).Value.ToString();       //获取点描述字段
           
            PITimeServer.PITime pi = myValue.TimeStamp;
            p.PdateTime = pi.LocalDate.ToString();                             //时间戳       

        }
        catch(Exception ex)
        {
           
        }
        finally
        {
           PIClose();
        }
        return p;
}

获取某个点某段时间内的所有值

public List<Point> Pi_GetPointValues(string tagName,DateTime startTime,DateTime endTime) 

{
        List<Point> list = new List<Point>();
        try
        {
            PIOpen();
           

            PISDK.PIValues pvs = null;
            PISDK.PIPoint ipoint = myserver.PIPoints[tagName];
            pvs = ipoint.Data.RecordedValues(startTime, endTime, PISDK.BoundaryTypeConstants.btInside, "", PISDK.FilteredViewConstants.fvRemoveFiltered, null); 

            PISDK.PointAttributes pa = ipoint.PointAttributes;
            PISDKCommon._NamedValues nvs = pa.GetAttributes();
           
            Object valIndex = 16;
            string description = nvs.get_Item(ref valIndex).Value.ToString();//获取点描述字段

            foreach (PISDK.PIValue pv in pvs)
            {
                Point p = new Point();
               
                PITimeServer.PITime pi= pv.TimeStamp;
                p.PdateTime = pi.LocalDate.ToString();         //时间戳

                p.Description = description;                   //描述字段

                p.Pvalue = pv.Value.ToString();                //值

                p.TagName = tagName;

                list.Add(p);
            } 
        }
        catch(Exception ex)
        {
           
        }
        finally
        {
            PIClose();
        }
        return list;

}

 

    //点名称
    private string tagName;
    //点描述
    private string description;
    //时间戳
    private string pdateTime;
    //点值
    private string pvalue;
   
    public string TagName
    {
        get { return tagName; }
        set { tagName = value; }
    }

    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    public string PdateTime
    {
        get { return pdateTime; }
        set { pdateTime = value; }
    }

    public string Pvalue
    {
        get { return pvalue; }
        set { pvalue = value; }
    }