Johnny_Z

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

本篇文章主要利用PISDK从PI服务器取数,介绍多种取数方法。

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

        /// <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服务器中找点的方法:

        /// <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快照值

        /// <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一段时间内的历史值

        /// <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快照值

        /// <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一段历史时间所有值

        /// <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历史时刻值

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

以上主要介绍了如何去从PI服务器获取值,包括了获取单个tag和多个tag,也包括了获取快照值和历史值,也许从效率上来看并不是最优的,因为PI服务器版本限制,导致一些新的接口不能使用,如果您对效率有严格的要求,推荐您使用最新版本。

根据上述的方法,我们也可以制作一个小程序,界面如下:

2

posted on 2013-08-03 16:49  Johnny_Z  阅读(3196)  评论(4编辑  收藏  举报