|
Please use our Code Center to obtain source-code and example projects/solutions:
http://codecenter.softwaretoolbox.com
|
|
|

|
|
Back to Services Source Examples
 |
|
|
|

|
|
Using the OPCData.NET Components within a VB.NET NT Service Application
The following example shows you one possible way in which you could use the OPCData.NET Components within an NT Service application.
This example is not for all developers as NT Service development is not easy. Therefore this and other NT Service based examples should only be used by experienced NT Service programmers.
How this example is constructed
This example actually uses a Windows Form to display some options allowing the end-user to specify the OPC Server, Tag and PC etc. to write to The values are written to and a message box will
indicate a success or fail. A Form was used for simplicity in showing this example. Please note that a Windows Form is NOT required to use the OPC Web Client within an NT Service.
This example contains the following form:
The code in this example is applied to the Click event handler for the button in this window. The textfields in this window are identified as "txt" and then the name, i.e. "txtPC", "txtOPC", "txtTag", "txtAccess" and "txtValue".
IMPORTANT NOTE: When using an NT Service with Windows Forms you must ensure that the Service
properties (within the CONTROL PANEL > Administrative Tools > Services) are set to allow the service to interact with the desktop. If this setting is not applied then you will not see the Form!
Writing to a Tag
Try ' 'define our OPC Web Client object Dim objOWC As New
EasyOPCDALib.EasyDA3 ' 'do the write objOWC.WriteItemValue(txtPC.Text, _
txtOPC.Text, _ txtTag.Text, _ txtAccess.Text, _ txtValue.Text) '
'check if any errors occurred If Err.Number = 0 Then MsgBox("Wrote successfully") Else
MsgBox("Failed to write, error: " & Err.Number _ & vbCrLf & Err.Description) End If ' 'clean-up 'NOTE: in your application you probably would not destroy
'the objects as we are here. You would most likely 'want to keep them alive as long as possible. objOWC = Nothing Catch ex As Exception
MsgBox("Error: " & ex.Message) End Try
Writing to Multiple Tags within a single transaction
' 'the following constants are used here to help make the code more readable
'from an educational perspective. 'Feel free to modify the contents of the values below. Const strPCName = ""
Const strOPCServer = "OPCLabs.KitServer.2" Const strTag = "Simulation.Random" Const iDataType = 0
Const iNewValue = 10
Try ' 'define our OPC Web Client Dim objOWC As New EasyDAWS
' 'now to make the call to read the tag objOWC.WriteItemValue(strPCName, strOPCServer, strTag, iNewValue)
' 'now to show the value to the screen If Err.Number = 0 Then
MessageBox.Show("Write Successful!") Else
MessageBox.Show("Write Failed: " & Err.Number & ", " & Err.Description) End If '
'now to destroy the objects objOWC = Nothing ' Catch webex As Web.Services.Protocols.SoapException
'display exception to screen If webex.InnerException Is Nothing Then
MessageBox.Show(webex.Message) Else MessageBox.Show(webex.InnerException.Message)
End If Catch ex As Exception '
'display an exception to screen If ex.InnerException Is Nothing Then MessageBox.Show(ex.Message)
Else MessageBox.Show(ex.InnerException.Message) End If End Try
|
|

|
|
|
|

|
|
|