Connect a Siemens PLC (snap7.dll) with Xojo

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

Ciao Donato,
in your connectTo function I don’t see the client parameter
This library let you create virtual connection for different device and you use the this client parameter to select it
you get the client id with cli_create

Ciao Antonio,
ok, so i have to run this function first to find the client using for exapmle:
function Cli_Create : S7Object;

and then put the client’s integer value inside of
function Cli_ConnectTo(Client : S7Object; Address : PAnsiChar; Rack, Slot : integer) : integer;

Did I get it right?

No
First you have to create the client (CREATE->integer)
Then you use the CONNECT_TO to establish the connection (client, address, rack, slot)->integer

Cli_connect is used if you want to specify other connection parameters (I never used it)
Cli_connectTo is usually the connection method to call once you have created the client

Everything works! Thank you very much Antonio for your fundamental help!