Timeout error when trying to read a register with limodbus

326 Views Asked by At

I'm trying to read different registers of a sensor using limodbus and I'm new to this and I can't find the answer on an other post. Thanks a lot for taking the time to help me find what I'm missing.

I've been able to get the temperature value with the address 100 , but when I try to read the humidity value, with address 102 (not 200 as I said previously), I get a time-out error message : "ERROR Connection timed out: select".

Here's the Modbus Address table : Modbus address table

Here is the piece of code that works for temperature :

int main()
{
    modbus_t *ctx;
    uint16_t tab_reg[64];

    ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'E', 8, 1);

    if (modbus_set_slave(ctx, 1) == -1) {
        fprintf(stderr, "set slave failed: %s\n", modbus_strerror(errno));
        exit(1);
    }

    modbus_set_response_timeout(ctx, 3, 0);
    modbus_set_debug( ctx, TRUE );

    if (modbus_connect(ctx) == -1) {
        fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
        modbus_free(ctx);
        exit(1);  
    }

    int rc = modbus_read_registers(ctx, 100, 2, tab_reg);
    if (rc == -1) {
        fprintf(stderr, "Failed to read registers: %s\n", modbus_strerror(errno));
        modbus_close(ctx);
        modbus_free(ctx);
        exit(1);
    }

That's what I've tried for humidity, it's exactly the same thing but I've changed the register address to 102 and I've also tried for 40103:

int main()
{
    modbus_t *ctx;
    uint16_t tab_reg[64];

    ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'E', 8, 1);

    if (modbus_set_slave(ctx, 1) == -1) {
        fprintf(stderr, "set slave failed: %s\n", modbus_strerror(errno));
        exit(1);
    }

    modbus_set_response_timeout(ctx, 3, 0);
    modbus_set_debug( ctx, TRUE );

    if (modbus_connect(ctx) == -1) {
        fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
        modbus_free(ctx);
        exit(1);  
    }

    int rc = modbus_read_registers(ctx, 102, 2, tab_reg);
    if (rc == -1) {
        fprintf(stderr, "Failed to read registers: %s\n", modbus_strerror(errno));
        modbus_close(ctx);
        modbus_free(ctx);
        exit(1);
    }

And here is the error I get :

Opening /dev/ttyUSB0 at 19200 bauds (E, 8, 1)
[01][03][00][66][00][02][24][14]
Waiting for a confirmation...
ERROR Connection timed out: select
Failed to read registers: Connection timed out

I thought the only thing that would change would be the address in the modbus_read_registers function. They are both 32 bits float. I am missing something ? Thank yooou

0

There are 0 best solutions below