1

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

Meb
  • 11
  • 4
  • 1
    According to the table, temp is at address 100 and humidity is at 102?! You're using 100 and 200??? – Craig Estey Apr 13 '23 at 17:36
  • 100 is the PDU address but the logical address is 40101. Which should you be using? – Craig Estey Apr 13 '23 at 17:50
  • Looks like [this](https://www.teracomsystems.com/wp-content/uploads/tsh300/tsh300v2-mb-rtu-humidity-temperature-sensor-user-manual-r1.7.pdf) is the unit. Your example is reading holding register 202 (physical) - unclear why you read this and not 102?. Please update question to show the code that is failing (as error does not match the rest of your question - i.e. not register 102). Maybe check your firmware version as Fahrenheit (which I assume is registers 200-202) was added in firmware v2.14. – Brits Apr 14 '23 at 06:13
  • @CraigEstey I meant 102, not 200. – Meb Apr 14 '23 at 12:42
  • @CraigEstey the logical doesn't work, i've tried 40101 for temperature and 40103 for humidity – Meb Apr 14 '23 at 12:43
  • 1
    The error still does not match your code (`00 CA` = address 202) which is a bit confusing. Your code looks fine. The "When 40001 Really Means 1, or 0 Really Means 1" section in [this article](https://www.csimn.com/CSI_pages/Modbus101.html) explains the addressing; libmodbus expects the PDU (`102` based on my reading of the docs). It might be worth trying to read 4 registers starting at `100`. – Brits Apr 15 '23 at 22:49
  • @Brits I've updated my answer with the right error message. Thanks a lot for the article and for your help, it works fine now, with 4 registers starting at 100. – Meb Apr 20 '23 at 13:23
  • @Meb - what you were doing (reg 102, 2 registers) should work (in a standards compliant device). However, I have run into other devices where you need to take this approach (they only accept a few register addresses in the request) - it's frustrating! – Brits Apr 20 '23 at 20:13

0 Answers0