001
12.07.2006, 18:01 Uhr
~peter_34
Gast
|
hallo!
ich bins nocheinmal
hier habe ich das beispiel was für excel ist!
Aber wie mache ich das mit den c++ builder ?
Danke!
Visual Basic: |
' Global variables for OPC Handling Dim ClientHandles(2) As Long Dim IsServerStarted As Boolean Dim IsGroupAdded As Boolean Dim IsItemAdded(2) As Boolean
' Objects from the OPC Automation interface Dim Item(2) As OPCItem Dim Items As OPCItems Dim Group As OPCGroup Dim Groups As OPCGroups Dim Server As OPCServer
Private Sub IBHOPCServermitMSExcel_Click()
End Sub
' Try to find all locally installed OPC servers using the OPCAutomation object Private Sub Worksheet_Activate()
' Initialize global variables and Worksheet user interface IsServerStarted = False IsGroupAdded = False IsItemAdded(0) = False IsItemAdded(1) = False Selectedserver.Text = "IBHsoftec.IBHOPC.DA" GroupToAdd.Text = "MyFirstGroup" PLCNameBox.Text = "S7_300" MW0Value.Text = "0" StateM20_0.Value = 0
' Now find the servers Dim ServerFinder As OPCServer Dim LocalServerNames As Variant Dim i As Integer On Error GoTo HandleError
' Find the installed servers Set ServerFinder = New OPCServer LocalServerNames = ServerFinder.GetOPCServers
' Show it to the user For i = LBound(LocalServerNames) To UBound(LocalServerNames) LocalServers.AddItem (LocalServerNames(i)) Next i
LocalServers.Text = LocalServers.List(0) ' Done Set ServerFinder = Nothing Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
Private Sub Worksheet_Deactivate() ' Call the disconnect sub DiconnectFromServer_Click End Sub
Private Sub CompleteStart_Click() ' Complete Startup procedure StartOPCServer_Click AddOPCGroup_Click AddOPCItemMW0_Click AddOPCItem_M20_0_Click End Sub
' Start the OPC Server Private Sub StartOPCServer_Click()
Dim MyServerName As String On Error GoTo HandleError ' Start the server (if not already) If (IsServerStarted = True) Then Exit Sub MyServerName = Selectedserver.Text Set Server = New OPCServer Server.Connect MyServerName IsServerStarted = True
Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Add a group on the started OPC Server Private Sub AddOPCGroup_Click()
Dim MyGroupName As String
On Error GoTo HandleError If (IsServerStarted = False) Then Exit Sub If (IsGroupAdded = True) Then Exit Sub ' Now Add the Group, set an update rate and activate it MyGroupName = GroupToAdd.Text Set Groups = Server.OPCGroups Set Group = Groups.Add(MyGroupName) Group.UpdateRate = 1000 Group.IsActive = True Set Items = Group.OPCItems IsGroupAdded = True Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Add an Item Private Sub AddOPCItemMW0_Click() Dim MyItemName As String
On Error GoTo HandleError If (IsServerStarted = False) Then Exit Sub If (IsGroupAdded = False) Then Exit Sub If (IsItemAdded(0) = True) Then Exit Sub ' Now Add the Item and activate it MyItemName = PLCNameBox.Text + ".MW0" ClientHandles(0) = 1 Set Item(0) = Items.AddItem(MyItemName, ClientHandles(0)) Item(0).IsActive = True IsItemAdded(0) = True Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Add an Item Private Sub AddOPCItem_M20_0_Click() Dim MyItemName As String
On Error GoTo HandleError If (IsServerStarted = False) Then Exit Sub If (IsGroupAdded = False) Then Exit Sub If (IsItemAdded(1) = True) Then Exit Sub ' Now Add the Item and activate it MyItemName = PLCNameBox.Text + ".M20.0" ClientHandles(1) = 2 Set Item(1) = Items.AddItem(MyItemName, ClientHandles(1)) Item(1).IsActive = True IsItemAdded(1) = True Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Read the Flagword 0 Private Sub ReadMW0_Click()
Dim Quality As Variant Dim TimeStamp As Variant Dim Value As Variant Dim iVal As Integer ' Use DIMed variables to make the datatype clear for OPC ' Now read the Flagword 0 On Error GoTo HandleError Item(0).Read 1, Value, Quality, TimeStamp ' Make it in 3 lines to see whether a VARIANT conversion iVal = Value ' error occurs. MW0Value.Text = Str(iVal)
Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Write the Flagword 0 Private Sub WriteMW0_Click()
Dim iValue As Integer ' Use DIMed variables to make the datatype clear for OPC
' Now write the Flagword 0 On Error GoTo HandleError iValue = MW0Value.Text Item(0).Write iValue
Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Read the Flag 20.0 Private Sub ReadM20_0_Click()
Dim Quality As Variant Dim TimeStamp As Variant Dim Value As Variant Dim bValue As Variant ' Use DIMed variables to make the datatype clear for OPC
' Now read the Flag 20.0 On Error GoTo HandleError Item(1).Read 1, Value, Quality, TimeStamp ' Make it in 3 lines to see whether a VARIANT conversion bValue = Value ' error occurs. StateM20_0.Value = bValue
Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Write the Flag 20.0 Private Sub WriteM20_0_Click() Dim bValue As Boolean ' Use DIMed variables to make the datatype clear for OPC ' Now write the Flag 20.0 On Error GoTo HandleError bValue = StateM20_0.Value Item(1).Write bValue
Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Disconnect Private Sub DiconnectFromServer_Click()
Dim Errors(2) As Long Dim NumItems As Integer On Error GoTo HandleError ' Disconnect and cleanup If (IsGroupAdded = True) Then Group.IsActive = False Groups.Remove Group.ServerHandle Set Group = Nothing Set Groups = Nothing IsGroupAdded = False End If If (IsServerStarted = True) Then Server.Disconnect Set Server = Nothing IsServerStarted = False End If Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
' Take the selection to the edit field Private Sub LocalServers_Change() ' Take the selection to the edit field On Error GoTo HandleError Selectedserver.Text = LocalServers.Text Exit Sub
HandleError: MsgBox "Please handle the Error occured", vbOKOnly End Sub
|
Dieser Post wurde am 12.07.2006 um 18:13 Uhr von FloSoft editiert. |