TCP client example?

128 views
Skip to first unread message

flux556

unread,
Oct 29, 2017, 6:21:37 AM10/29/17
to libmodbus


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;
}
The context creation and connecting to device works, but using any read or write function returns -1
(modbus_write_bit and modbus_write_register in the above example, but I also tried modbus_read_registers).
The debug messages for the above function is:
[00][01][00][00][00][06][FF][06][1E][EA][02][2B]
Waiting for a confirmation...
ERROR Unknown error: select
result: -1
Unknown error
To test that my modbus device works, I tried using pymodbus and it works without problems.

I'm on Windows 10 x64 with Visual Studio 2017.

Any ideas?

Matic

Chuck Watson

unread,
Oct 29, 2017, 2:20:01 PM10/29/17
to libm...@googlegroups.com

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

www.EnergyChaser.com

--
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.

image001.gif

flux556

unread,
Oct 29, 2017, 3:49:31 PM10/29/17
to libmodbus
Hey Chuck,

I downloaded the SimplyModbus TCP client demo and tried it.
In short I discovered that the coil address was fine, but the Slave ID for the device is 1, which of course I forgot to set with libmodbus.

Thanks for the hint!
Matic
Reply all
Reply to author
Forward
0 new messages