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

Trying to get an archived value usingVB.NET Options · View
erburrell
#1 Posted : Thursday, April 02, 2009 7:48:48 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Hi all. I am new to VB.NET and the use of the PI SDK. I am trying to figure out how to get a value from my PI server at a specific time. Any help would be greatly appreciated. The code I have so far is as follows?

Quote:
Dim srv As PISDK.Server
Dim pt As PISDK.PIPoint
Friend WithEvents ServPickList1 As OSIsoft.PISDK.Controls.ServPickList


Private Sub ServConnect()
srv = ServPickList1.SelectedServer
If Not srv.Connected Then
Dim cxn As New PISDKDlg.Connections()
cxn.Login(srv)

If ((Err.Number <> 0) And Err.Number <> (PISDK.PISDKErrorConstants.pseSERVERIDMISMATCH)) Then
MessageBox.Show(Err.Description)
Exit Sub
Else
MsgBox("Connection to " & srv.Name & " completed.", , "PI Server Connection")
End If

End If
End Sub



Private Sub ServPickList1_picklistClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ServPickList1.picklistClick
Call ServConnect()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PtList As New PISDK.PointList
Dim i As Integer

PtList = TagSearch.Show()
For i = 1 To PtList.Count
ComboBox1.Items.Add(PtList(i).Name)
Next


End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Dim PIpt As PISDK.PIPoint
Dim rtype As New PISDK.RetrievalTypeConstants
Dim MyPiTime As New PITimeServer.PITimeFormat
Dim pv As New PISDK.PIValue

'PIpt = srv.PIPoints(ComboBox1.Text)
MyPiTime.InputString = "t"
rtype = PISDK.RetrievalTypeConstants.rtInterpolated
'pv = PIpt.Data.ArcValue(MyPiTime, rtype)
pv = srv.PIPoints(ComboBox1.Text).Data.ArcValue(MyPiTime, rtype, Nothing)
txtValue.Text = CStr(pv.Value)
End Sub
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.
RJK Solutions
#2 Posted : Thursday, April 02, 2009 8:43:43 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
Hi erburrel and welcome.

I have posted up some basics in the past, see this post: http://www.rjksolutionsl...and-Archive-values.aspx

However, you are not far off this from what you have posted.

All you need is the following:

Dim Pt as PISDK.PIPoint = srv.PIPoints(ComboBox1.Text)
Dim Pv as PISDK.PIValue = Pt.Data.ArcValue("t",rtAuto)

You don't need to create new instances of a PIValue, PITimeFormat or RetrievalTypeConstants. A PIValue object is returned from the ArcValue function.

Some considerations...check what the PointType is so you know if you will have a DigitalState object returned, a string or a float. Note, even for a string/float you may have an ErrorState returned (e.g. Bad).

Give this a try and get back to me and I can guide you through any more complex examples you want :-)

Cheers,

Rhys.
Principal Consultant
Real-Time Data Management @ Wipro Technologies
erburrell
#3 Posted : Thursday, April 02, 2009 9:53:29 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Rhys,

I thank you for your help. However, I am still receiving a runtime error. In the Button2_Click routine, I receive a NullReferenceException was unhandled error
The IDE is specifically pointing out the PIpt AS PISDK.PIPoint = srv.PIPoints(ComboBox1.Text) line. This is the same line that was giving me errors earlier.

Any ideas?

My code now looks like this.

Quote:
Dim srv As PISDK.Server
Dim pt As PISDK.PIPoint
Friend WithEvents ServPickList1 As OSIsoft.PISDK.Controls.ServPickList

Private Sub ServPickList1_picklistClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ServPickList1.picklistClick
Dim SrvCnt As Boolean

SrvCnt = ConnectToPI("PIDemo")
If SrvCnt = True Then
MsgBox("Connected to PI Server")
Else
MsgBox("Connection Failed")
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PtList As New PISDK.PointList
Dim i As Integer

PtList = TagSearch.Show()
For i = 1 To PtList.Count
ComboBox1.Items.Add(PtList(i).Name)
Next


End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim PIpt As PISDK.PIPoint = srv.PIPoints(ComboBox1.Text)
Dim rtype As New PISDK.RetrievalTypeConstants
Dim MyPiTime As New PITimeServer.PITimeFormat
Dim pv As New PISDK.PIValue

'MyPiTime.InputString = "t"
'rtype = PISDK.RetrievalTypeConstants.rtAuto

pv = PIpt.Data.ArcValue("t", PISDK.RetrievalTypeConstants.rtAuto)
'pv = srv.PIPoints(ComboBox1.Text).Data.ArcValue(MyPiTime, rtype, Nothing)
txtValue.Text = CStr(pv.Value)
End Sub
RJK Solutions
#4 Posted : Thursday, April 02, 2009 10:11:52 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
OK check to see if your PISDK.Server object "srv" is connected or not "Is Nothing" i.e. If srv Is Nothing Then ConnectToPI("PIDemo").
Also, does the PI tag exist on the connected PI server?
Principal Consultant
Real-Time Data Management @ Wipro Technologies
erburrell
#5 Posted : Friday, April 03, 2009 2:20:48 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
I have added the If statement, and am still getting the error. The line in question is:

PIpt = srv.PIPoints(ComboBox1.Text)

I am attaching the Connection Code following:

Quote:
Module mdlPIFunctions
'For this module to be functional, you must add the following references to your project:
' OSIsoft.PISDK.Controls.PISDKCtrlDlg

Private AppObj As New PISDKDlg.ApplicationObject
Friend TagSearch As PISDKDlg.TagSearch = AppObj.TagSearch
Private PIServer As PISDK.Server
Private PIServerName As String
Private Const PIAdminName As String = "PIADMIN"
Private Const PIAdminPass As String = "1nt3rg3n"
Private Const PIDemoName As String = "PIDEMO"
Private Const PIDemoPass As String = ""

Public Function ConnectToPI(ByVal AccessLvl As String) As Boolean
Dim bRes As Boolean = False
Dim MySDK As New PISDK.PISDK

PIServerName = Form1.ServPickList1.SelectedServerName

Try
If PIServer Is Nothing Then PIServer = MySDK.Servers(PIServerName)
Try
If Not PIServer.Connected Then
If AccessLvl = "Admin" Then
PIServer.Open("UID=" & PIAdminName & ";PWD=" & PIAdminPass)
Else
PIServer.Open("UID=" & PIDemoName & ";PWD=" & PIDemoPass)
End If
End If
bRes = PIServer.Connected
Catch ex As Exception
bRes = False
End Try
Catch ex As Exception
bRes = False
End Try
Return bRes
End Function

End Module



I do appreciate all your assistance.
erburrell
#6 Posted : Friday, April 03, 2009 2:25:04 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Sorry, I forgot to answer one of your questions. The Tag is in PI. I am using the TagSearch.Show function to pull the PI tag, and have been using thte Sinusoid tag as a test. One quick question. The value in ComboBox1 is "SINUSOID". Is this correct, or should it be the pointID.

Thanks,

Ray
RJK Solutions
#7 Posted : Friday, April 03, 2009 3:12:10 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
Nah you can use the PI tag name.
Try the following as a very basic start to ensure your environment is set up correct:

Code:

        Dim PIServer As PISDK.Server

        Dim bRes As Boolean = False
        Dim SDK As New PISDK.PISDK
        Try
            If PIServer Is Nothing Then PIServer = SDK.Servers(PIServerName)
            Try
                If Not PIServer.Connected Then PIServer.Open("UID=" & PIUserName & ";PWD=" & PIUserPass)
                bRes = PIServer.Connected
            Catch ex As Exception
                bRes = False
            End Try
        Catch ex As Exception
            bRes = False
        End Try

        If bRes Then
            Dim Pt As PISDK.PIPoint = PIServer.PIPoints("SINUSOID")
            Dim Pv As PISDK.PIValue = Pt.Data.Snapshot
        End If


Can you see a value now? If so, start substituting hardcoded references to your combobox etc...
Principal Consultant
Real-Time Data Management @ Wipro Technologies
erburrell
#8 Posted : Friday, April 03, 2009 3:38:46 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
That seems to have worked!

Thank you very much!

Ray
erburrell
#9 Posted : Monday, April 06, 2009 8:13:27 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Another question for you.

I have the sinusoid values coming back just fine. I have attempted to get some digital sets back, but am having trouble. Attached is my current module for grabbing the data. any ideas?

Thank you,

Ray

Quote:

Module mdlPIFunctions
'For this module to be functional, you must add the following references to your project:
' OSIsoft.PISDK.Controls.PISDKCtrlDlg

Private AppObj As New PISDKDlg.ApplicationObject
Friend TagSearch As PISDKDlg.TagSearch = AppObj.TagSearch
Private PIServer As PISDK.Server
Public PIServerName As String = "Magnpisvp01"
Private Const PIAdminName As String = "PIADMIN"
Private Const PIAdminPass As String = "1nt3rg3n"
Private Const PIDemoName As String = "pidemo"
Private Const PIDemoPass As String = ""
Function GetPIData(ByVal txtTag As String) As String
Dim bRes As Boolean = False
Dim PIUserName As String = "piadmin"
Dim PIUserPass As String = "1nt3rg3n"
Dim SDK As New PISDK.PISDK
Dim MyPointType As String


Try
If PIServer Is Nothing Then PIServer = SDK.Servers(PIServerName)
Try
If Not PIServer.Connected Then PIServer.Open("UID=" & PIUserName & ";PWD=" & PIUserPass)
bRes = PIServer.Connected

Catch ex As Exception
bRes = False
End Try
Catch ex As Exception
bRes = False
End Try
If bRes Then
Dim Pt As PISDK.PIPoint = PIServer.PIPoints(txtTag)
MyPointType = PointTypeString(Pt.PointType)
If MyPointType = "Double" Then
GetPIData = dblPIData(txtTag)
ElseIf MyPointType = "Integer" Then
GetPIData = intPIData(txtTag)
ElseIf MyPointType = "Digital" Then
GetPIData = digPIData(txtTag)
End If
Else
MsgBox("Error retreiving Value", , "PI Error")
GetPIData = 0.0
End If
Return GetPIData
End Function

Function dblPIData(ByVal txtTag As String) As Double
Dim PIServer As PISDK.Server
Dim SDK As New PISDK.PISDK

PIServer = SDK.Servers(PIServerName)
Dim Pt As PISDK.PIPoint = PIServer.PIPoints(txtTag)
Dim Pv As PISDK.PIValue = Pt.Data.Snapshot
MsgBox(PointTypeString(Pt.PointType))
dblPIData = Pv.Value


End Function

Function intPIData(ByVal txtTag As String) As Integer
Dim PIServer As PISDK.Server
Dim SDK As New PISDK.PISDK

PIServer = SDK.Servers(PIServerName)
Dim Pt As PISDK.PIPoint = PIServer.PIPoints(txtTag)
Dim Pv As PISDK.PIValue = Pt.Data.Snapshot
MsgBox(PointTypeString(Pt.PointType))
intPIData = Pv.Value


End Function

Function digPIData(ByVal txtTag As String) As String
Dim PIServer As PISDK.Server
Dim SDK As New PISDK.PISDK

PIServer = SDK.Servers(PIServerName)
Dim Pt As PISDK.PIPoint = PIServer.PIPoints(txtTag)
Dim Pv As PISDK.PIValue = Pt.Data.Snapshot
MsgBox(PointTypeString(Pt.PointType))
MsgBox(Convert.ToSingle(Pv.Value))
digPIData = Pv.Value


End Function

Function dblPITimeData(ByVal txtTag As String, ByVal txtTime As String) As Double
Dim bRes As Boolean = False
Dim PIServer As PISDK.Server
Dim PIUserName As String = "piadmin"
Dim PIUserPass As String = "1nt3rg3n"
Dim SDK As New PISDK.PISDK

Try
If PIServer Is Nothing Then PIServer = SDK.Servers(PIServerName)
Try
If Not PIServer.Connected Then PIServer.Open("UID=" & PIUserName & ";PWD=" & PIUserPass)
bRes = PIServer.Connected
Catch ex As Exception
bRes = False
End Try
Catch ex As Exception
bRes = False
End Try

If bRes Then
Dim Pt As PISDK.PIPoint = PIServer.PIPoints(txtTag)
Dim Pv As PISDK.PIValue = Pt.Data.ArcValue(txtTime, PISDK.RetrievalTypeConstants.rtAuto)

dblPITimeData = Pv.Value
Else
MsgBox("Could not get a value for the PI point", , "PI Error")
dblPITimeData = 0.0
End If

End Function

Function PointTypeString(ByVal ptType As PISDK.PointTypeConstants) As String
Select Case ptType
Case PISDK.PointTypeConstants.pttypBlob
PointTypeString = "Blob"
Case PISDK.PointTypeConstants.pttypDigital
PointTypeString = "Digital"
Case PISDK.PointTypeConstants.pttypFloat16
PointTypeString = "Double"
Case PISDK.PointTypeConstants.pttypFloat32
PointTypeString = "Double"
Case PISDK.PointTypeConstants.pttypFloat64
PointTypeString = "Double"
Case PISDK.PointTypeConstants.pttypInt16
PointTypeString = "Integer"
Case PISDK.PointTypeConstants.pttypInt32
PointTypeString = "Integer"
Case PISDK.PointTypeConstants.pttypString
PointTypeString = "String"
Case PISDK.PointTypeConstants.pttypNull
PointTypeString = "Unknown point type"
End Select

Return PointTypeString
End Function

End Module
RJK Solutions
#10 Posted : Monday, April 06, 2009 8:28:23 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
More questions the better!!

So what you need to be aware of is for a Digital tag you get a "DigitalState" object returned rather than a "PIValue". So if I was you I would change your digPIData function to:


Function digPIData(ByVal txtTag As String) As String
Dim PIServer As PISDK.Server
Dim SDK As New PISDK.PISDK

PIServer = SDK.Servers(PIServerName)
Dim Pt As PISDK.PIPoint = PIServer.PIPoints(txtTag)
Dim Ds As PISDK.DigitalState = Pt.Data.Snapshot
digPIData = Ds.Name
End Function

You can return the code for the digital state or the digital state name.

Cheers,

Rhys.
Principal Consultant
Real-Time Data Management @ Wipro Technologies
erburrell
#11 Posted : Monday, April 06, 2009 9:32:01 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
I tried this code, but I get an exception on the Dim Ds AS PISDK.DigitalState = Pt.Data.Snapshot declaration.

The system states that an InvalidCastException was unhandled and in the description states that "No such interface supported". Any ideas as to why this would occur?

Thanks,
Ray
RJK Solutions
#12 Posted : Monday, April 06, 2009 9:50:42 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
Do you have a reference to PISDK.Common and PISDK.PITimeServer?
Are you sure your tag is a digital point type?

Try...

Dim Ds As PISDK.DigitalState = Ctype(Pt.Data.Snapshot, PISDK.DigitalState)
Principal Consultant
Real-Time Data Management @ Wipro Technologies
erburrell
#13 Posted : Tuesday, April 07, 2009 3:44:22 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Yes, both items are in my references. In addition, the type is digital. In addition to knowledge, the program determines what type the tag is, and calls the appropriate function by looking at the PointType in the PointTypeSting Function and using this to determine which function to call.

Even with the CType call, I still get an InvalidCastException returned. It states the following:

Quote:
Unable to cast COM object of type 'PISDK.PIValueClass' to interface type 'PISDK.DigitalState'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{E56BEEA7-60FB-11D1-BD64-0060B0290178}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).


Troubleshhoting tips:

Quote:
When casting from a number, the value must be a number less than infinity. Make sure the source type is convertible to the destination type.


Does it matter what the digital set for the tag is? This one happens to be One_Zero.

Thank you,

Ray
RJK Solutions
#14 Posted : Tuesday, April 07, 2009 4:17:38 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
Nothing wrong just my typo! Ooops.

Dim Ds As PISDK.DigitalState = Ctype(Pt.Data.Snapshot.Value, PISDK.DigitalState)
Principal Consultant
Real-Time Data Management @ Wipro Technologies
erburrell
#15 Posted : Tuesday, April 07, 2009 4:22:47 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Worked perfectly without the Ctype conversion even!! Dancing

Thank you very much!

I'm sure I'll be nack later with more.
erburrell
#16 Posted : Monday, May 04, 2009 5:32:26 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Ok, now I have swapped over to C#. Always wanted to learn C, so I figured why not now? (I am obviously somewhat insane Smile)!

I am trying to get a digital state, but am getting some errors from the compiler on my code which is as follows:

Quote:

string GetDigData(string myTag, string myTime)
{
PISDK.PIPoint pt = myPIServer.PIPoints[myTag];
PISDK.DigitalState ds = pt.Data.ArcValue(myTime,PISDK.RetrievalTypeConstants.rtInterpolated,new PISDKCommon.PIAsynchStatus());
GetDigData = ds.Name;
}


The errors I am getting are:
1. Cannot implicitly convert type PISDK.PIValue to PISDK.DigitalStat. An explicit conversion exists...
2. Cannot assign 'GetDigData because it is a method group.

I am assuming 2 is related to 1.

Thanks,

Ray
RJK Solutions
#17 Posted : Tuesday, May 05, 2009 9:35:13 AM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
Hi Ray,

So something to note, C# is strict. You must be explicit in your casts, so the result from ArcValue is of type Object and you want a type of DigitalState, you need to cast the Object to a DigitalState.

Code:

PISDK.DigitalState ds = pt.Data.ArcValue(myTime,PISDK.RetrievalTypeConstants.rtInterpolated,new PISDKCommon.PIAsynchStatus());


becomes

Code:

PISDK.DigitalState ds = (PISDK.DigitalState) pt.Data.ArcValue(myTime,PISDK.RetrievalTypeConstants.rtInterpolated,new PISDKCommon.PIAsynchStatus());


Hope this helps.

Rhys.
Principal Consultant
Real-Time Data Management @ Wipro Technologies
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.