Hello,
I'm trying to write a single coil value to a physical Modbus device using libmodbus with this function:
int test_lib()
{
modbus_t *mb;
mb = modbus_new_tcp("192.168.1.109", 502);
if (mb == NULL)
{
printf("Error creating TCP context!\n");
modbus_free(mb);
return -1;
}
if (modbus_connect(mb) == -1)
{
printf("Error connecting to modbus device!\n");
modbus_free(mb);
return -2;
}
modbus_set_debug(mb, 1);
//int res = modbus_write_bit(mb, 1027, TRUE);
int res = modbus_write_register(mb, 7914, 555);
printf("result: %d\n", res);
if (res == -1)
{
printf("%s\n", modbus_strerror(errno));
modbus_close(mb);
modbus_free(mb);
return -3;
}
modbus_close(mb);
modbus_free(mb);
return 0;
}
[00][01][00][00][00][06][FF][06][1E][EA][02][2B]
Waiting for a confirmation...
ERROR Unknown error: select
result: -1
Unknown error
If you are on a Windows box anyway, try something like Kepware to communicate with your device first using the exact same hardware. Kepware works fully powered for free for ½ hour I believe. All you have to do is restart the service and it will run for another ½ hour. Long enough for troubleshooting.
This will tell you that all the hardware and cabling works and that you have the correct settings. Try to configure Kepware to communicate exactly as you would read the device using your libmodbus code. Same number of registers per read for example. Also watch for the MODBUS base offset for addressing.
Because of imperfect standards, MODBUS devices can be tricky to communicate with initially.
Chuck Watson BS, CEM, CMVP
Direct USA 724-799-1529
--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "libmodbus".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.