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

Finding specific values on a TAG with VB Options · View
Markowic
#1 Posted : Wednesday, May 11, 2011 10:11:06 PM
Rank: Member
Groups: Member

Joined: 4/28/2011
Posts: 10
Location: Brasil
Hello,
First of all congratulations for the outstanding job.
I joined the forum a few days ago and was looking for ideas for a panel I need to do. Despite of finding some very useful code, I still could not reach what I want.
Basically I have stored in a tag values where each value refers to a day and a task. In one day I have several tasks. What I need is given a date to know if the value corresponding to a particular task is stored in the TAG. I've done a test code using Excel but without connection to the database and I translated the date for long format and merged with the task code. It worked well. What I need now is to jump from Excel to VB associated to PI System.
I understand that I should get a window of values from the TAG based on the date that I'm checking and store them in an array and then test the values there to know if the value I'm looking for exists. But as I have no practice with the PI System’s code, I'm lost.
Can you help me?
Sincerely,
Flavio.
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.
Shem
#2 Posted : Thursday, May 12, 2011 11:20:58 AM
Rank: Member
Groups: Member

Joined: 5/5/2011
Posts: 22
Location: Moscow
I can help you, probably.
In what difficulty consists?
Markowic
#3 Posted : Thursday, May 12, 2011 12:55:05 PM
Rank: Member
Groups: Member

Joined: 4/28/2011
Posts: 10
Location: Brasil
Shem wrote:
I can help you, probably.
In what difficulty consists?

Hi Shem,
Thanks for your answer.
My dificult is I do not know how to write the code to make the VB read the TAG and save the values in the array once I'm not used to PI-Datalink commands.

Best regards.
Shem
#4 Posted : Thursday, May 12, 2011 2:44:52 PM
Rank: Member
Groups: Member

Joined: 5/5/2011
Posts: 22
Location: Moscow
Hi, Markowic!
Open new Excel.
Press Alt+F11 (open VBA)
At first it is necessary to connect PI-SDK. Go to Tools/references choose "PI-SDK 1.3. Type Libary" and "PITimeServer 1.1 Type Libary". Press Ok. Save you work book.
Go to Data Link to connection and connect your PI-server. It can be done by means of VBA, but for simplicity we will make so.
Open VBA
Choose Sheet1 and create new function, for example
Code:
Private Sub My_Function()
Dim srv As Server
Dim dt As New PITimeFormat
Dim x As Double

dt.InputString = "12.05.2011 00:00:00"
Set srv = Servers("Gabov")
x = srv.PIPoints("SINUSOID").Data.ArcValue(dt, rtAfter)
End Sub


dt.InputString = "12.05.2011 00:00:00" - dt is time in PI format
Set srv = Servers("Gabov") - "Gabov" is name PI-server
x = srv.PIPoints("SINUSOID").Data.ArcValue(dt, rtAfter) - value "SINUSOID" on dt (12.05.2011 00:00:00)

More info about PI-SDK functions in help for SDK (C:\Program Files\PIPC\HELP\pisdk.chm)

Or other example
Code:
Private Sub My_Function()
Dim srv As Server
Dim dt As New PITimeFormat
Dim x As Double

dt.InputString = "12.05.2011 00:00:00"
Set srv = Servers("Gabov")
x = srv.PIPoints("SINUSOID").Data.Snapshot
End Sub


You will receive an Snapshot

So it is possible to obtain the data and save the values in the array. If there are other questions ask. Than to a smog I will help.
Markowic
#5 Posted : Thursday, May 12, 2011 3:25:33 PM
Rank: Member
Groups: Member

Joined: 4/28/2011
Posts: 10
Location: Brasil
Shem,
I appreciated the directions to connect to the PI server and I'll use them. But as I have read the code you are taking only one value with a specific timestamp.
What I need is, given a day (i.e. today 12/05/2011) I wanted to feed an array with all values in a time window, lets say 1 week. Maybe (*-1 week,*) I guess.
This is because I need to test all values within this timewindow to check if there is a specific value there. I mean doesn't care when it was put in the TAG and I will not know when it was put there.
Thanks,
Shem
#6 Posted : Friday, May 13, 2011 7:25:55 AM
Rank: Member
Groups: Member

Joined: 5/5/2011
Posts: 22
Location: Moscow
Hi!
You can use this code
Code:
Private Sub My_Function()
Dim srv As Server
Dim TagName As String
Dim FilterString As String
Dim st As New PITimeFormat
Dim et As New PITimeFormat
Dim PIArray As PIValues
Dim PIvalue As PIvalue

Set srv = Servers("Gabov")
TagName = "SINUSOID"

st.InputString = "12.05.2011 11:00:00" ' Start tine
et.InputString = "12.05.2011 16:00:00" ' End time

FilterString = "TagVal('" & TagName & "') > 50"
Set PIArray = srv.PIPoints(TagName).Data.RecordedValues(st, et, , FilterString, fvRemoveFiltered)

If PIArray.Count <> 0 Then
    'if you have values
    '
    '.....
    '
    'for example
   
    TimeOfValue = ""
    For Each PIvalue In PIArray
        TimeOfValue = TimeOfValue & Chr(10) & Chr(13) & PIvalue.TimeStamp.LocalDate
    Next
    MsgBox "You have " & PIArray.Count & " values in:" & TimeOfValue
Else
    MsgBox "You have't values."
End If

End Sub


Instead of an "st" and "et" it is possible to use an PIdata ("*"). Then
Set PIArray = srv.PIPoints(TagName).Data.RecordedValues("*-7d", "*", , FilterString, fvRemoveFiltered)

FilterString -A string containing a filter expression (see help "PI Performance Equation"). You can use
"TagVal('SINUSOID') > 50"
or
"TagVal('SINUSOID') = ""Shutdown"""

More in detail about this function RecordedValues it is written in the help.

I hope you now at you it will turn out to solve your problem ;)
Markowic
#7 Posted : Friday, May 13, 2011 1:56:26 PM
Rank: Member
Groups: Member

Joined: 4/28/2011
Posts: 10
Location: Brasil
Hello Shem,

This code seems to be what I am looking for. I'll make some tests here and will give you a follow up later.
Thanks a lot for your help.
ejyoungone
#8 Posted : Friday, May 13, 2011 3:37:15 PM
Rank: Member
Groups: Member

Joined: 11/30/2010
Posts: 15
Location: New York, USA
shem a lot of good information here .. thanks ! i myself am trying to learn how to use VBA and pi - sdk to access data etc. from the pi server, gave a quick browse and will look more later. i find the best way for me to learn is to look through everyone s code samples to try to learn VBA as well as the best ways of accessing data from pi
Markowic
#9 Posted : Friday, May 13, 2011 9:59:37 PM
Rank: Member
Groups: Member

Joined: 4/28/2011
Posts: 10
Location: Brasil
Shem wrote:
Hi!
You can use this code

Instead of an "st" and "et" it is possible to use an PIdata ("*"). Then
Set PIArray = srv.PIPoints(TagName).Data.RecordedValues("*-7d", "*", , FilterString, fvRemoveFiltered)

FilterString -A string containing a filter expression (see help "PI Performance Equation"). You can use
"TagVal('SINUSOID') > 50"
or
"TagVal('SINUSOID') = ""Shutdown"""

More in detail about this function RecordedValues it is written in the help.

I hope you now at you it will turn out to solve your problem ;)


Hello Shem,

I've worked over the code you've sent me and it is doing what I wanted. Now I just need to do some improvements.

Code:
Set srv = Servers("PISERV1")
TagName = "CHECKLIST PROG"

DataProg = UserForm1.TextBox2.Value
ValorData = CDbl(DateValue(DataProg))
AuxDataprog = ValorData - 7
DataProg2 = CDate(AuxDataprog)

st.InputString = DataProg2 & " 00:00:00" ' Start tine
et.InputString = DataProg & " 00:00:00" ' End time

FilterString = "TagVal('" & TagName & "') > 50"
Set PIArray = srv.PIPoints(TagName).Data.RecordedValues(st, et, , FilterString, fvRemoveFiltered)


This way you can handle with any timewindow you want. Smile

Thanks again,
Flavio.
Shem
#10 Posted : Monday, May 16, 2011 6:30:57 AM
Rank: Member
Groups: Member

Joined: 5/5/2011
Posts: 22
Location: Moscow
Hi, All!
Markowic, good idea!
I am glad that my help was useful to you.

Ejyoungone, you can create new topics on the forum that interests you and I am sure that the forum will be people who can help you. The main thing that you are properly formulated question. If I can, I'll help you ;)
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.