Dear all,
I am new to libmodbus, but I need it to use it to get some data from the registers of meters.
codes:
int main(void)
{
modbus_t *mdb;
uint16_t tab_reg[64];
int rc;
int i;
int item;
mdb = modbus_new_tcp("192.168.46.20", 502);
modbus_set_debug(mdb,TRUE);
if (modbus_connect(mdb) == -1)
{
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(mdb);
return -1;
}
rc = modbus_read_registers(mdb, 0x2821, 1, tab_reg);
if (rc == -1)
{
fprintf(stderr, "%s\n", modbus_strerror(errno));
return -1;
}
for (i=0; i < rc; i++)
{
printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
}
//}
modbus_close(mdb);
modbus_free(mdb);
}
output:
Connecting to
192.168.46.20:502[00][01][00][00][00][06][FF][03][28][21][00][01]
Waiting for a confirmation...
<00><01><00><00><00><05><02><03><02><82><10>
reg[0]=33296 (0x8210)
Problem:
the return data is wrong (the correct data should be a value around 2300). It seems to me that it just shifted the address and returned it.
When I ran a loop, I got a list like this:
item id is 2reg[0]=32 (0x20)
item id is 3reg[0]=48 (0x30)
item id is 4reg[0]=64 (0x40)
item id is 5reg[0]=80 (0x50)
item id is 6reg[0]=96 (0x60)
item id is 7reg[0]=112 (0x70)
item id is 8reg[0]=128 (0x80)
item id is 9reg[0]=144 (0x90)
item id is 10reg[0]=160 (0xA0)
They were just addresses, not the real values. Can someone help me out of it?
Many thanks in advance
Cheers