As you can see pretty standard testing of the library, since im new to it.
int main() {
modbus_t* ctx;
ctx = modbus_new_tcp("127.0.0.1", 502);
if (ctx == nullptr) {
std::cerr << "Failed to create the Modbus context." << std::endl;
return -1;
}
// Enable debugging to get detailed output
modbus_set_debug(ctx, TRUE);
// Connect to the Modbus server
if (modbus_connect(ctx) == -1) {
// Print the error message using modbus_strerror
std::cerr << "Connection failed: " << modbus_strerror(errno) << std::endl;
// Use GetLastError() for additional Windows-specific error details
DWORD dwError = GetLastError();
std::cerr << "Windows error code: " << dwError << std::endl;
modbus_free(ctx);
return -1;
}
}
The output is as follows:
Connecting to 127.0.0.1:502
Connection failed: No error
Windows error code: 0
D:ProjectesProjectes_Visual_Studiolibmodbus_testlibmodbus_testReleaselibmodbus_test.exe (process 2928) exited with code -1 (0xffffffff).
Port is not in use and I don’t have the firewall blocking anything.
Any help could be appreciated my OS is Win32
Why is not giving a error code but failing regardless?
2
Ok, sorry i was trying to connect to my server wich was turned off. I just did ping and checked the port sorry for wasting your time