
QD1 IO Expansion Module User Manual
44 Document Version V3.0 (2020-08-18)
This library function is compiled based on libmodbus (open source
library)
function
transplantation;
For
more,
visit
Configuration of key functions:
Step 1
Create a new libmodbus environment. Use the modbus device linked by
tcp and apply the modbus_new_tcp () function. If the new creation is
successful, a struct modbus_t pointer will be returned. Afterwards the
operation on the modbus device is to operate the pointer; if it fails, a null
pointer will be returned.
Example:
modbus_t *ctx; ctx = modbus_new_tcp("192.168.0.1", 502)
Multiple modbus slave devices can be connected to one physical interface. Each
modbus slave device has its own independent ID, which is called slave ID and is an
integer. Therefore, you need to use the modbus_set_slave() function to set slave ID for
modbus_t structure, indicating specific modbus device to be connected. Release a
libmodbus environment called modbus_free() function.
Step 2
After successful creation, connection can be established using the
modbus_connect() function. Close a connection with modbus_close()
function. Refresh a connection with modbus_flush() function.
Step 3
After successful connection, you can call related functions to read and
write QD1.
The related functions are as follows:
int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
Read single IO/coil (i.e. DO) with function code of 0x01. Store the read value into
the array pointed to by the dest pointer. This function returns 0 on success and -1 on
failure.