´
' Verbindungsaufbau über das Beispiel von libnodave
Private Sub cmdConnect_Click()
' Verbindung herstellen
Dim RetCode As Long
lblConnect.Caption = "connected to " & S7_IP & " ... failed!" ' ... von Natur aus pessimistisch ;-)
hSocket = openSocket(102, S7_IP) ' for ISO over TCP, siehe Doku zu libnodave
If hSocket > 0 Then
hInterface = daveNewInterface(hSocket, hSocket, "DEMO", 0, daveProtoISOTCP, 0)
RetCode = daveInitAdapter(hInterface)
If RetCode = daveResOK Then
hConnection = daveNewConnection(hInterface, 0, 0, 2)
RetCode = daveConnectPLC(hConnection)
If RetCode = daveResOK Then
lblConnect.Caption = "connected to " & S7_IP
tcp_connection = True
End If
cmdConnect.Enabled = (RetCode <> 0)
cmdDisconnect.Enabled = (RetCode = 0)
cmdRead.Enabled = (RetCode = 0)
cmdWrite.Enabled = (RetCode = 0)
End If
End If
End Sub
' Verbindungsabbau über das Beispiel von libnodave
Private Sub cmdDisconnect_Click()
' Verbindung abbauen
Dim RetCode As Long
If hConnection <> 0 Then
RetCode = daveDisconnectPLC(hConnection)
'MsgBox ("1: " & RetCode)
daveFree hConnection
hConnection = 0
End If
If hInterface <> 0 Then
RetCode = daveDisconnectAdapter(hInterface)
'MsgBox ("2: " & RetCode)
daveFree hInterface
hInterface = 0
End If
If hSocket <> 0 Then
'MsgBox ("CP: " & hSocket)
RetCode = closePort(hSocket)
'MsgBox ("3: " & RetCode)
hSocket = 0
End If
tcp_connection = False
cmdConnect.Enabled = True
cmdDisconnect.Enabled = False
cmdRead.Enabled = False
cmdWrite.Enabled = False
lblConnect.Caption = ""
End Sub