
Chapter 4
Document Version V3.1.0 (2020-08-18) 45
Example:
uint8_t bits[MAX_IO] = {0};
int ret, i;
ret=modbus_read_bits(ctx,MODBUS_COIL_ADDR,MODBUS_COI
L_LEN, bits);
if (ret < 0)
{fprintf(stderr, “%s\n”, modbus_strerror(errno)); }
else
{printf(“BITS COILS: \n”)
,
for (i = 0; i < ret; i++)
{printf(“[%d] = %d\n”, i, bits[i]); } }
int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t
*dest);
Read single IO/discrete input (i.e. DI) with function code of 0x02. Store the read
value into the array pointed to by the dest pointer. This function returns 0 on success and
-
1 on failure.
Example:
ret=modbus_read_input_bits(ctx,MODBUS_DISCRETE_ADDR,MO
DBUS_DISCRETE_LEN, bits);
if (ret < 0)
{ fprintf(stderr, “%s\n”, modbus_strerror(errno)); }
else
{printf(“BITS DISCRETE: \n”);
for (i = 0; i < ret; i++)
{printf(“[%d] = %d\n”, i, bits[i]); }}
int modbus_read_input_registers(modbus_t *ctx, int addr, int nb,
uint16_t *dest);
Read 16-bit IO with function code of 0x04. Starting from addr, read the values of
consecutive nb registers, and put the results into the array pointed to by *dest. This
function returns the number of read registers on success and -1 on failure.