AnsweredAssumed Answered

Using Python to Retrieve Sampled Data with PI SDK

Question asked by BriJo on Jan 16, 2017
Latest reply on Jan 16, 2017 by gregor



I have some 'Sampled Data' I would like to pull from a PI Server using Python + PI SDK. I am having trouble using InterpolatedValues2(Start_Time, End_Time, SampleInterval) to pull the values. I suspect the issue is with the SampleInterval argument. I get an error when I pass a time interval of "24h" in which states 'The python instance can not be converted to a COM object'. I am currently also using win32com. Anyone know how I can pass this is as a COM object string?


I've included my code below for reference:


import datetime as dt
import win32com.client as w32lc
import numpy as np

def NODW_recorded_data(start_time, end_time, pi_tag):
    Fetches recorded data from the NODW PI server for a PI tag between the given dates
    :param start_time: a datetime object.
    :param end_time: a datetime object.
    :param pi_tag: a string
    :return: a numpy array with two columns. The first is the timestamp,
             and the second is the recorded value.

    pisdk = w32lc.Dispatch('PISDK.PISDK')
    server = pisdk.Servers('SERVER1')
    con = w32lc.Dispatch('PISDKDlg.Connections')
    con.Login(server, '', '', 1, 0)

    pi_time_start = w32lc.Dispatch('PITimeServer.PITimeFormat')
    pi_time_start.InputString = start_time.strftime('%m-%d-%Y %H:%M:%S')

    pi_time_end = w32lc.Dispatch('PITimeServer.PITimeFormat')
    pi_time_end.InputString = end_time.strftime('%m-%d-%Y %H:%M:%S')

    point = server.PIPoints(pi_tag)
    recorded_values = point.Data.InterpolatedValues2(pi_time_start, pi_time_end, "24h")
    rows = int(recorded_values.Count)
    results = np.zeros((rows, 2), dtype='object')
    for i, sample in enumerate(recorded_values):
        results[i, :] = np.array([dt.datetime.fromtimestamp(int(sample.Timestamp)), sample.Value])

    return results


Any help would be greatly appreciated.