|
|
Rank: Administration
 Groups: Administration
Joined: 6/20/2008 Posts: 617 Location: Cheshire, United Kingdom.
|
Connecting to a PI server via SDK is simple but to save yourself time you should create a reusable method that can be called over and over to ensure your connection object is created and the connection to PI is open.
For this example you only need a reference to the PI SDK Type Library from your VB or VBA project.
Steps you need to take are: - Create a VB function that returns a Boolean (connection is either available to use "True" or not "False") - Create module/class level variables for the PISDK.Server object and for connection String constants. - First check in the VB function that the PISDK.Server variable has been created. - If the connection to the server is closed, then open it. - Return the PISDK.Server.Connected property as the result of the function.
This will ensure that before you perform anything against a PI server you make a simple call to this function first to ensure your connection is OK.
Your code may look something like the following:
Quote: Private PIServer As PISDK.Server
Private Const PIServerName As String = "piserver" Private Const PIUserName As String = "piusername" Private Const PIUserPass As String = "piuserpassword"
Public Function ConnectToPI() As Boolean
Dim bRes As Boolean: bRes = False
' Check to ensure object is created If TypeName(PIServer) = "Nothing" Then On Error Resume Next Err.Clear Set PIServer = PISDK.Servers(PIServerName) If Err.Number <> 0 Then bRes = False Exit Function End If On Error GoTo 0 End If
' Check connection status If Not PIServer.Connected Then On Error Resume Next Err.Clear Call PIServer.Open("UID=" & PIUserName & ";PWD=" & PIUserPass) If Err.Number <> 0 Then bRes = False Exit Function End If On Error GoTo 0 End If
' Final check of connection status - this is returned to calling method bRes = PIServer.Connected ConnectToPI = bRes
End Function
Then each time you perform a task, you would simple call:
Quote: If ConnectToPI Then ' Perform activities here... Else ' Handle bad connection... End If
The .Net version of this code will follow shortly. Other examples posted on this forum of PI SDK calls will make reference to this function we just created.
Principal Consultant Real-Time Data Management @ Wipro Technologies
|
|
|
|
|
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.
|
|
|
Rank: Administration
 Groups: Administration
Joined: 6/20/2008 Posts: 617 Location: Cheshire, United Kingdom.
|
A couple of students new to PI and .Net have asked for some C# versions of some of the examples on the forum (well connections to PI, data retrieval using PISDK) so for starters here is a C# version of the ConnectToPI method....enjoy!
Code: PISDK.Server PIServer;
String PIServerName = "piserver"; String PIUserName = "piusername"; String PIUserPass = "piuserpassword";
public Boolean ConnectToPI() { Boolean bRes = false;
PISDK.PISDKClass SDK = new PISDK.PISDKClass(); try { if (PIServer == null) PIServer = SDK.Servers[PIServerName]; try { if (!PIServer.Connected) PIServer.Open("UID=" + PIUserName + ";PWD=" + PIUserPass); bRes = PIServer.Connected; } catch (Exception e) { bRes = false; } } catch (Exception e) { bRes = false; }
return bRes; }
Principal Consultant Real-Time Data Management @ Wipro Technologies
|
|
|
Rank: Newbie Groups: Member
Joined: 9/10/2008 Posts: 3 Location: San Francisco, CA
|
I actually haven't found a reason to use the .Disconnect() method that the PI-SDK provides. So the above method is highly useful and should be used exactly once at the start of the application.
|
|
|
Rank: Newbie Groups: Member
Joined: 11/11/2008 Posts: 4
|
Could you please post this code in VB.Net format? It would be greatly appreciated. Also, I need help with writing code in VB.Net to display values of a point starting from when another value equals zero until that value equals zero again. In other words, I am trying to view "batch data" without setting up PIBatch. We have a cycle time counter so when the counter goes to zero, that is the end of the batch. I'd then like to view data such as max, min, etc for various points. If anyone can help, I'd be extremely thankful.
|
|
|
Rank: Administration
 Groups: Administration
Joined: 6/20/2008 Posts: 617 Location: Cheshire, United Kingdom.
|
VB.Net version of the above code:
Code: Dim PIServer As PISDK.Server
Private Const PIServerName As String = "piserver" Private Const PIUserName As String = "piusername" Private Const PIUserPass As String = "piuserpassword"
Public Function ConnectToPI() As Boolean 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 Return bRes End Function Principal Consultant Real-Time Data Management @ Wipro Technologies
|
|
|
Rank: Newbie Groups: Member
Joined: 6/8/2009 Posts: 1 Location: Paris
|
RJK Solutions wrote:A couple of students new to PI and .Net have asked for some C# versions of some of the examples on the forum (well connections to PI, data retrieval using PISDK) so for starters here is a C# version of the ConnectToPI method....enjoy!
Code: PISDK.Server PIServer;
String PIServerName = "piserver"; String PIUserName = "piusername"; String PIUserPass = "piuserpassword";
public Boolean ConnectToPI() { Boolean bRes = false;
PISDK.PISDKClass SDK = new PISDK.PISDKClass(); try { if (PIServer == null) PIServer = SDK.Servers[PIServerName]; try { if (!PIServer.Connected) PIServer.Open("UID=" + PIUserName + ";PWD=" + PIUserPass); bRes = PIServer.Connected; } catch (Exception e) { bRes = false; } } catch (Exception e) { bRes = false; }
return bRes; }
- creation of SDK is (most of time) useful one time by application, so keep it outside of ConnectToPI method - imbricated try catch is useless in this case to my mind: Code: PISDK.Server PIServer;
String PIServerName = "piserver"; String PIUserName = "piusername"; String PIUserPass = "piuserpassword"; PISDK.PISDKClass SDK = null;
public Boolean ConnectToPI() { Boolean bRes;
if (SDK == null) SDK = new PISDK.PISDKClass(); try { if (PIServer == null) PIServer = SDK.Servers[PIServerName]; if (!PIServer.Connected) PIServer.Open("UID=" + PIUserName + ";PWD=" + PIUserPass); bRes = PIServer.Connected; } catch (Exception e) { bRes = false; }
return bRes; }
[/size] zymoly wrote:I actually haven't found a reason to use the .Disconnect() method that the PI-SDK provides. So the above method is highly useful and should be used exactly once at the start of the application.
Disconnect is only useful to properly exit any custom application (flush connections)
|
|
|
Rank: Newbie Groups: Member
Joined: 12/1/2011 Posts: 3
|
I get an error "'Server' is a type in 'PISDK' and cannot be used as an expression." when debugging the ConnectToPI() function:
specifically VB Express is complaining about this line:
PIServer = PISDK.Server(PIServerName)
other than that line, all the code seems to be error free.
I have the references set correctly, I thought.
Any help is appreciated.
|
|
|
Rank: Newbie Groups: Member
Joined: 12/1/2011 Posts: 3
|
Here's my code I'm using in VB Express 2010.
(have reference to PISDK 1.3 Type Library)
Module Module1
Sub Main()
If ConnectToPI Then Dim PITag As PISDK.PIPoint PITag = PIServer.PIPoints("SINUSOID")
' Retrieve the current value (Snapshot) and timestamp ' Assign Value to PISDK.PIValue variable Dim PITagVal As PISDK.PIValue PITagVal = PITag.Data.Snapshot
' Write the value out to Immediate window Debug.Print(PITagVal.TimeStamp.LocalDate & " " & PITagVal.Value)
' Retireve value from 1 day ago and write out to Immediate window PITagVal = PITag.Data.ArcValue("*-1d", PISDK.RetrievalTypeConstants.rtAuto) Debug.Print(PITagVal.TimeStamp.LocalDate & " " & PITagVal.Value)
' Retire a PI tag Point Attribute, in this case the descriptor Debug.Print(PITag.PointAttributes("descriptor").Value)
PITagVal = Nothing PITag = Nothing Else ' Handle bad connection... End If
End Sub
Private PIServer As PISDK.Server
Private Const PIServerName As String = "en1mspi003" Private Const PIUserName As String = "piadmin" Private Const PIUserPass As String = ""
Public Function ConnectToPI() As Boolean
Dim bRes As Boolean : bRes = False
' Check to ensure object is created If TypeName(PIServer) = "Nothing" Then On Error Resume Next Err.Clear() PIServer = PISDK.Server(PIServerName) If Err.Number <> 0 Then bRes = False Exit Function End If On Error GoTo 0 End If
' Check connection status If Not PIServer.Connected Then On Error Resume Next Err.Clear() Call PIServer.Open("UID=" & PIUserName & ";PWD=" & PIUserPass) If Err.Number <> 0 Then bRes = False Exit Function End If On Error GoTo 0 End If
' Final check of connection status - this is returned to calling method bRes = PIServer.Connected ConnectToPI = bRes
End Function
End Module
|
|
|
Rank: Newbie Groups: Member
Joined: 12/1/2011 Posts: 3
|
I got it working by adding this dim to the beginning of the function "ConnectToPI"
Public Function ConnectToPI() As Boolean Dim sdk As New PISDK.PISDK
and
PIServer = sdk.Servers(PIServerName)
not sure what happened when using the original code, but this seems to work now.
|
|
|
Rank: Advanced Member
 Groups: Member
Joined: 11/13/2011 Posts: 77 Location: Middle East
|
|
|
|
Rank: Administration
 Groups: Administration
Joined: 6/20/2008 Posts: 617 Location: Cheshire, United Kingdom.
|
Thanks for posting the link to your blog, Fima. Principal Consultant Real-Time Data Management @ Wipro Technologies
|
|
|
Rank: Advanced Member
 Groups: Member
Joined: 11/13/2011 Posts: 77 Location: Middle East
|
RJK Solutions wrote:Thanks for posting the link to your blog, Fima.
You are welcome, RJK Solutions! I just a novice to the PI OSIsoft system (but i have 4 years expirience in .NET, C++, embedded and multidisciplinary environment) and i all the time dig out some usefully information for me. So, for myself (it is a easy way to remember things) and maybe for other .NET developers i open a little blog. I try update it every time, i find some little solution for some little PI programming task. Thanks
|
|
|
Rank: Advanced Member
 Groups: Member
Joined: 11/13/2011 Posts: 77 Location: Middle East
|
Hello to all I wrote new implementation of watching PI server connection status by API and SDK libraries hereHope, it will help to somebody
|
|
|
|
Guest
|