PIValues Example

 

 

This example, during the Form_Load, connects to the default server, clears and initializes the display with reasonable defaults. When Command1 is hit it validates the user's input, retrieves the snapshot, the requested recorded values and displays them. 

 

Build the PIValues example as follows:

1. Create a new project and add a reference to the PISDK library.

2. Add the visual elements as shown in the picture. Make sure the numbered element names (label1, list1, etc.) match those shown.

3. Cut and paste the code below into the project.

 

Option Explicit
Dim srv As Server
Dim piconst As PIConstant
Private Sub Command1_Click()
On Error GoTo handler
   ' validate the input
   If Len(Text1.Text) = 0 Then
      MsgBox "Please enter a tag name"
      Exit Sub
   End If
   Dim dt As Date, dt2 As Date
   dt = CDate(Text2.Text)
   If dt < 25568.5 Then
      MsgBox "Invalid start time."
      Exit Sub
   End If
   dt2 = CDate(Text3.Text)
   If dt2 < 25568.5 Then
      MsgBox "Invalid end time."
      Exit Sub
   End If
   Dim btType As BoundaryTypeConstants
   btType = piconst.Item(List1.Text).Value
   Dim pt As PIPoint
   Set pt = srv.PIPoints(Text1.Text)
   ' clear the results
   Text4.Text = ""
   Text5.Text = ""
   List2.Clear
   ' Get the snapshot
   Dim pv As PIValue
   Set pv = pt.Data.Snapshot
   Text4.Text = CStr(pv.Value)
   Text5.Text = CStr(pv.TimeStamp.LocalDate)
   ' Get the recorded values
   Dim pvs As PIValues
   Set pvs = pt.Data.RecordedValues(dt, dt2, btType)
   For Each pv In pvs
      List2.AddItem CStr(pv.TimeStamp.LocalDate) + vbTab + CStr(pv.Value)
   Next
   Label11.Caption = CStr(pvs.Count)
Exit Sub
handler:
   MsgBox Err.Description
End Sub

Private Sub Form_Load()
On Error GoTo handler
   Set srv = Servers.DefaultServer
   ClearForm
Exit Sub
handler:
   MsgBox Err.Description
End Sub
Private Sub ClearForm()
   Text1.Text = "Sinusoid"
   Dim dt As Date, dt2 As Date
   dt = Now()
   dt2 = dt - 8 / 24#  ' last eight hours
   Text2.Text = CStr(dt2)
   Text3.Text = CStr(dt)
   List1.Clear
   Text4.Text = ""
   Text5.Text = ""
   Label11.Caption = ""
   List2.Clear
   On Error GoTo handler
   Set piconst = PIConstants.Item("BoundaryTypeConstants")
   Dim nv As NamedValue
   For Each nv In piconst
      List1.AddItem nv.Name
   Next
   List1.ListIndex = 0
   Exit Sub
handler:
   MsgBox Err.Description
End Sub


Enabling Operational Intelligence