The following content describes the basics matters of using libmodbus functions.
The method for creating a project is different for each language, so please refer to [Creating Project] section.
Processing Flow
When creating a Modbus master communication program using the libmodbus functions, the following processing is required.
Please refer to each sample and function reference for specific function usage.
Communication settings
TCP : Specify IP address and port number
RTU : Specify COM port, baudrate, parity, slave ID, etc.
Please use the modbus_new_tcp
function and modbus_new_rtu
function respectively.
In the above functions, the structure for modbus communication is secured,
so be sure to execute the memory release process at the end of communication.
Start communication
Connection process (Use modbus_connect
function)
Send/receive data
Execute various function codes
Execute conversion process according to the data
Communication end
Disconnection process (Use modbus_close
function)
Memory release process (Use modbus_free
function)
Get Error Contents
For the libmodbus functions, the success or failure of the function is judged by the return value, and the details are expressed by errno.
Since errno is common to each function, get the error details immediately after executing the function.
* When other functions are executed, errno may be overwritten.
Please refer to each sample for the specific method of getting errno and getting the error content.
Notes on Getting Error
Due to the specifications of libmodbus, there are some functions that do not update errno when the function execution fails.
When the execution of the above function fails, the return value is failed, but errno is not updated,
and "No Error" may be displayed or the details of the previous error may be displayed. Please note that.
Also, errno is not updated when the function succeeds.