基于C#的PISDK研究(代码)

时间:2021-08-31 00:13:41
本篇文章主要利用PISDK从PI服务器取数,介绍多种取数方法。

首先需要一些基础的代码,比如获取PI服务的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// <summary>
/// 获取PI服务/// </summary>
/// <param name="servername">//UID=PIUser;PWD=PIUserPassWord
</param>
/// <returns></returns>
private Server GetServer(string servername)
{
    try
    {
        if (!string.IsNullOrEmpty(servername))
        {
            //采用认证方式登录
            server = pi.Servers[servername];
            server.Open(connStr);
        }
        else
        {
            //采用默认方式登录
            server = pi.Servers.DefaultServer;
            server.Open();
        }
  
        if (server.Connected)
        {
            return server;
        }
        else
        {
            return null;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return null;
    }
}

  还有到PI服务器中找点的方法:

1
2
3
4
5
6
7
8
9
10
/// <summary>
/// 搜索PI中对应的数据点
/// </summary>
/// <param name="server">PI服务对象</param>
/// <param name="condition">选择tag的条件</param>
/// <returns></returns>
private PointList SearchPIPoints(Server server, String condition)
{
      return server.GetPoints(condition);
}

  

下面介绍取数方法:

1. 取得单个tag快照值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// <summary>
/// 取得单个tag快照值
/// </summary>
/// <param name="server">PI服务对象</param>
/// <param name="tag">tag名称</param>
/// <returns></returns>
private string GetSingleSnap(Server server, string tag)
{
    string output=string.Empty;
    PIPoints piPoints = server.PIPoints;
    //采用标签名称的方式取得对象
    PIPoint piPoint = piPoints[tag];
    //调用方法取得快照值
    PIValue piValue = piPoint.Data.Snapshot;
    //取到该快照值时PI服务器时间
    PITimeServer.PITime piTime = piValue.TimeStamp;
    output = piTime.LocalDate.ToString() + " " + piValue.Value + Environment.NewLine;
    return output;
}

  2. 取得单个tag一段时间内的历史值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/// <summary>
/// 取得单个tag段时间内的历史值
/// </summary>
/// <param name="server">PI服务对象</param>
/// <param name="tag">tag名称</param>
/// <returns></returns>
private string GetSingleHistory(Server server, string tag)
{
    string output = string.Empty;
    PIPoints piPoints = server.PIPoints;
    PIPoint piPoint = piPoints[tag];
    //取得分钟前到当前时间的历史值
    PIValues piValues = piPoint.Data.RecordedValues(DateTime.Now.AddMinutes(-5).ToString(),
        DateTime.Now.ToString());
    foreach (PIValue piValue in piValues)
    {
        PITimeServer.PITime piTime = piValue.TimeStamp; //PI时间
        output += piTime.LocalDate.ToString() + " " + piValue.Value + Environment.NewLine;
    }
    return output;
}

  3. 取得多个tag快照值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <summary>
/// 取得多个tag取快照值
/// </summary>
/// <param name="server">PI服务对象</param>
/// <param name="tag">tag值(tag='' or tag='')</param>
/// <returns></returns>
private string MultipleSnap(Server server, string tag)
{
    string output = string.Empty;
    //取tag
    PointList ptlist = SearchPIPoints(server, tag);
    PISDKCommon.NamedValues nvErrs = new PISDKCommon.NamedValues();
    PointValues ptvalues = ptlist.Data.get_Snapshot(out nvErrs);
  
    foreach (PointValue myValue in ptvalues)
    {
        PITimeServer.PITime piTime = myValue.PIValue.TimeStamp; //PI时间
        if (myValue.PIValue.Value.GetType().IsCOMObject)
        {
            //状态值
            DigitalState myDigState = (DigitalState)myValue.PIValue.Value;
            output += myValue.PIPoint.Name + " " + piTime.LocalDate.ToString() + " " + myDigState.Name + Environment.NewLine;
        }
        else
        {
            //模拟值
            output += myValue.PIPoint.Name + " " + piTime.LocalDate.ToString() + " " + Convert.ToString(Math.Round(double.Parse(Convert.ToString(myValue.PIValue.Value)), 2)) + Environment.NewLine;
        }
    }
    return output;
}

  4. 取得多个tag一段历史时间所有值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/// <summary>
/// 取得多个tag段历史时间所有值
/// </summary>
/// <param name="server">PI服务对象</param>
/// <param name="tag">tag名称</param>
/// <returns></returns>
private string MultipleHistory1(Server server,string tag)
{
    string output = string.Empty;
    PointList ptlist = SearchPIPoints(server, tag);
    string outPut = string.Empty;
  
    PISDKCommon.NamedValues nvErrs = new PISDKCommon.NamedValues();
  
    foreach (PIPoint piPoint in ptlist)
    {
        //获取5分钟前到当前时间的历史值
        PIValues piValues = piPoint.Data.RecordedValues(DateTime.Now.AddMinutes(-5).ToString(),
        DateTime.Now.ToString());
  
        foreach (PIValue piValue in piValues)
        {
            PITimeServer.PITime piTime = piValue.TimeStamp; //PI时间
            output+= piPoint.Name + " " + piTime.LocalDate.ToString() + " " + piValue.Value + Environment.NewLine;
        }
    }
    return output;
}

  5. 取得多个tag历史时刻值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/// <summary>
/// 取得多个tag段历史时间点值
/// </summary>
/// <param name="server">PI服务对象</param>
/// <param name="tag">tag名称</param>
/// <returns></returns>
private string MultipleHistory2(Server server, string tag)
{
    string output = string.Empty;
    PointList ptlist = SearchPIPoints(server, tag);
      
    PISDKCommon.NamedValues nvErrs = new PISDKCommon.NamedValues();
  
    PITimeServer.PITimeFormat piTime1 = new PITimeServer.PITimeFormat();
    piTime1.InputString = "*-2h";//or "2012-2-2 2:2:2"
    //获取两小时前的时刻值
    PointValues ptvalues = ptlist.Data.ArcValue(piTime1, RetrievalTypeConstants.rtAuto, out nvErrs);
    foreach (PointValue myValue in ptvalues)
    {
        PITimeServer.PITime piTime = myValue.PIValue.TimeStamp; //PI时间
        if (myValue.PIValue.Value.GetType().IsCOMObject)
        {
            //状态值
            DigitalState myDigState = (DigitalState)myValue.PIValue.Value;
            output+= myValue.PIPoint.Name + " " + piTime.LocalDate.ToString() + " " + myDigState.Name + Environment.NewLine;
              
        }
        else
        {
            //模拟值
            output+= myValue.PIPoint.Name + " " + piTime.LocalDate.ToString() + " " + Convert.ToString(Math.Round(double.Parse(Convert.ToString(myValue.PIValue.Value)), 2)) + Environment.NewLine;
  
        }
    }
    return output;
}

  

转载自:https://www.cnblogs.com/Johnny_Z/p/3234849.html