Good morning everyone,
I’m trying to connect the Xojo RAD to a S71200 PLC with profinet. I managed to load snap7.dll on Xojo but I have a doubt about the function declarations to establish the connection with the PLC. The strange thing for me is that when I start the runtime the application loads the library correctly, but when it tries to connect to the controller it exits the runtime without getting errors or warnings back.
I copy the code below.
//Function call to load the DLL
If TargetWindows Then
Declare Function DllLoad Lib “Kernel32” Alias “LoadLibraryA” (dllName As CString) As Integer
Dim dllPath As CString = “C:\Users\gianc\Desktop\Progetti_2023\Xojo\Test_SCADA\snap7\snap7.dll”
Dim handle As Integer = DllLoad(dllPath)
If handle = 0 Then
// Error loading DLL
MsgBox “Error loading DLL”
Else
// Caricamento riuscito
MsgBox “DLL loaded successfully”
end if
End If
// Create an instance of the Snap7 client
Declare Function Create Lib “C:\Users\gianc\Desktop\Progetti_2023\Xojo\Test_SCADA\snap7\snap7.dll” Alias “Cli_Create” () As Integer
Declare Function Connect Lib “C:\Users\gianc\Desktop\Progetti_2023\Xojo\Test_SCADA\snap7\snap7.dll” Alias “Cli_ConnectTo” (Indirizzo As CString, Rack As Integer, Slot As Integer) As Integer
Declare Sub Disconnect Lib “C:\Users\gianc\Desktop\Progetti_2023\Xojo\Test_SCADA\snap7\snap7.dll” Alias “Cli_Disconnect” (Client As Integer)
Dim client As Integer
client = Create()
// Establish connection with PLC
Dim ipAddress As String = “192.168.0.1”
Dim rack As Integer = 0
Dim slot As Integer = 1
Try
If Connect(ipAddress, rack, slot) <> 0 Then
// The connection was successful
MessageBox(“Connection to PLC OK”)
Else
// La connessione ha fallito
MessageBox(“Failed to establish connection with PLC.”)
End If
Catch err As RuntimeException
// Exception handling
MessageBox("Error connecting to PLC: " + err.Message)
End Try
Thanks for any help,
Donato