YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Getting tag values in excel VBA Options · View
mjcordeiro
#1 Posted : Tuesday, January 24, 2012 7:35:17 AM
Rank: Member
Groups: Member

Joined: 1/24/2012
Posts: 10
Location: mjcordeiro
Hello,

I'm trying to use PI System with Microsoft Excel VBA.
What I want to do is:
Get a value in a certain date and compare that value with the current one.
This will help us because when we are starting up our machine It's good to compare some variables with the last good startup.
A practical exercise:

Last Good startup:
Start time: "2012-01-01 02:00:00"
End Time: "2012-01-01 05:00:00"
Variable name: "POWER"

Now, I will create a chart in Excel With "POWER" variable and refresh the chart each 5seconds.

How can I get "POWER" values and list them in a excel sheet with 5seconds of interval?
How can I get a instant value of "POWER"?
Any help is welcome

I have this code, but I'm not sure this does what I want (I have doubts about Interval, for example):


Quote:

Dim srv As Server
Dim TagName As String
Dim FilterString As String
Dim st As New PITimeFormat
Dim et As New PITimeFormat
Dim PIArray, PIArray1 As PIValues
Dim PIvalue As PIvalue

Set srv = Servers("myserver")
TagName = "POWER"
st.InputString = Format(Start, "dd-mm-yyyy h:mm:ss") ' Start time
et.InputString = Format(End1, "dd-mm-yyyy h:mm:ss") ' End time
FilterString = "TagVal('" & TagName & "')"
Set PIArray = srv.PIPoints(TagName).Data.RecordedValues(st, et, , FilterString, fvRemoveFiltered)
If PIArray.Count <> 0 Then
TimeOfValue = ""
Line = 5
Column = 2
For Each PIvalue In PIArray
TimeOfValue = TimeOfValue & Chr(10) & Chr(13) & PIvalue.TimeStamp.LocalDate
Cells(Line, Column).Value = PIvalue
Line = Line + 1
Next
MsgBox "You have " & PIArray.Count & " values in:" & TimeOfValue
Else
MsgBox "You have't values."
End If



regards,
Mário

Sponsor  
 

OSIsoft vCampus is a subscription-based, online offering that consists of providing everything people need to develop applications on the PI System.
We invite you to take a "tour" of the OSIsoft Virtual Campus - also feel free to consult the FAQ  or contact OSIsoft vCampus for more details.
squatty
#2 Posted : Tuesday, January 24, 2012 12:39:31 PM
Rank: Advanced Member

Groups: Member

Joined: 4/7/2011
Posts: 137
Location: KZN, South Africa
hi mjcordeiro

Don't you have datalink in Excel to do this for you.
mjcordeiro
#3 Posted : Tuesday, January 24, 2012 2:29:49 PM
Rank: Member
Groups: Member

Joined: 1/24/2012
Posts: 10
Location: mjcordeiro
Hello squatty,

I'm sorry but didn't understand your answer. Are you asking or are you telling me?

In a excel sheet I have this made with PI Addin:

Start Date: 2012-01-02 02:00:00
End Date: 2012-01-02 05:00:00
Step: 5s
Tag: "POWER"


After I use the addin, it will fill a lots of cells with the timestamp and the respective value.
The question is how can I do the same thing but using Excel VBA?
I've tried to record a macro, but didn't work :(((


Thank you in advance,
Mário
squatty
#4 Posted : Wednesday, January 25, 2012 7:48:20 AM
Rank: Advanced Member

Groups: Member

Joined: 4/7/2011
Posts: 137
Location: KZN, South Africa
Soory for the "dirty" code but I tried this and it worked.

Sub TEST()

Dim srv As Server
Dim TagName As String
Dim FilterString As String
Dim st As New PITimeFormat
Dim et As New PITimeFormat
Dim PIArray, PIArray1 As PIValues
Dim PIvalue As PIvalue
Dim ipid2 As IPIData2
Dim pdata As PIData
Dim pt As PIPoint


Set srv = PISDK.Servers.DefaultServer
TagName = "sinusoid"
st.InputString = Format(DateAdd("h", -12, Now()), "dd-mmm-yyyy h:mm:ss") ' Start time
Cells(2, 11).Value = st.InputString
et.InputString = Format(Now(), "dd-mmm-yyyy h:mm:ss") ' End time
Cells(3, 11).Value = et.InputString
Calculate
FilterString = "TagVal('" & TagName & "')"
Set pt = srv.PIPoints(TagName)
Set pdata = pt.Data
Set ipid2 = pdata

Set PIArray = ipid2.InterpolatedValues2(st, et, "5s")

Debug.Print PIArray.Count
If PIArray.Count <> 0 Then
TimeOfValue = ""
Line = 5
Column = 2
For Each PIvalue In PIArray
Debug.Print PIvalue.Value & vbTab & PIvalue.TimeStamp.LocalDate
TimeOfValue = TimeOfValue & Chr(10) & Chr(13) & PIvalue.TimeStamp.LocalDate
Cells(Line, Column).Value = PIvalue
Line = Line + 1
Next
MsgBox "You have " & PIArray.Count & " values in:" & TimeOfValue
Else
MsgBox "You have't values."
End If


End Sub
mjcordeiro
#5 Posted : Tuesday, January 31, 2012 12:32:26 PM
Rank: Member
Groups: Member

Joined: 1/24/2012
Posts: 10
Location: mjcordeiro
Thanks for your help.
I've tried and it works fine Smile))

Is there any function to calculate:
* Average
* Minimum value
* Maximum value

of the collected data?

Or I have to do it "manually"?

Thanks in advance
squatty
#6 Posted : Wednesday, February 01, 2012 6:30:29 AM
Rank: Advanced Member

Groups: Member

Joined: 4/7/2011
Posts: 137
Location: KZN, South Africa
Glad we could help.

You can try ipid2.Summary(st,et,calctype)
mjcordeiro
#7 Posted : Wednesday, February 01, 2012 11:32:50 AM
Rank: Member
Groups: Member

Joined: 1/24/2012
Posts: 10
Location: mjcordeiro
Thank you soooo much skatty.

regards,
Mário
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.