Modbus on Raspberry PLCs with C
Implementation for Raspberry based PLCs (C)
Installation
libmodbus is a C library which offers client and server implementation for Modbus RTU and TCP. You can find the library here, install it executing the command: sudo apt install libmodbus-dev.
Master
The following examples are Modbus master implementations with both RTU and TCP:
Configurations
The initial configuration for Modbus RTU and Modbus TCP primarily differs in the type of communication they use.
- For Modbus RTU, the configuration involves setting parameters for serial communication. This includes defining the SLAVE_ID to identify the specific Modbus slave device, the SERIAL_PORT to specify the serial interface used for communication, and communication parameters such as the BAUD_RATE, PARITY, DATA_BITS, and STOP_BITS. Configure these settings according to your configurations.
#define SLAVE_ID 1 #define SERIAL_PORT "/dev/ttySC0" #define BAUD_RATE 38400 #define PARITY 'E' #define DATA_BITS 8 #define STOP_BITS 1
#define SLAVE_ID 1 #define SERIAL_PORT "/dev/ttySC0" #define BAUD_RATE 38400 #define PARITY 'E' #define DATA_BITS 8 #define STOP_BITS 1
To create the Modbus context, Modbus RTU uses the following function, which initializes the connection with the specified serial parameters.
ctx = modbus_new_rtu(SERIAL_PORT, BAUD_RATE, PARITY, DATA_BITS, STOP_BITS);
ctx = modbus_new_rtu(SERIAL_PORT, BAUD_RATE, PARITY, DATA_BITS, STOP_BITS);
- Modbus TCP involves setting parameters for network communication over an IP network. Instead of serial port settings, you specify the SLAVE_IP address and the PORT number. The SLAVE_ID is still used to identify the slave device.
#define SLAVE_ID 1 #define SLAVE_IP "10.10.10.20" #define PORT 502
#define SLAVE_ID 1 #define SLAVE_IP "10.10.10.20" #define PORT 502
To create the Modbus context, Modbus TCP uses the following function, which sets up the connection over the specified IP address and port.
ctx = modbus_new_tcp(SLAVE_IP, PORT);
ctx = modbus_new_tcp(SLAVE_IP, PORT);
API Reference
This API reference provides details on the essential Modbus functions used for reading and writing data between a Modbus master and slave. The functions cover operations for coils, discrete inputs, holding registers, and input registers. These operations include reading the status or values and writing data to the Modbus slave.
Description:
Reads the contents of a specified number of coils from a slave device (FC=01).
Syntax:
modbus_read_bits(ctx, addr, nb, dest);
Parameters:
- ctx: The modbus context object.
- addr: The starting address of the coils.
- nb: The number of coils to read.
- dest: The array where is stored the reading.
Returns:
Returns the number of read bits if successful, -1 otherwise.
Description:
Reads the contents of a specified number of discrete inputs from a slave device (FC=02).
Syntax:
read_input_bits(ctx, addr, nb, dest);
Parameters:
- ctx: The modbus context object.
- addr: The starting address of the discrete inputs.
- nb: The number of discrete inputs to read.
- dest: The array where is stored the reading.
Returns:
Returns the number of read input status if successful, -1 otherwise.
Description:
Reads the contents of a specified number of holding registers from a slave device (FC=03).
Syntax:
modbus_read_registers(ctx, addr, nb, dest);
Parameters:
- ctx: The modbus context object.
- addr: The starting address of the holding registers.
- nb: The number of holding registers to read.
- dest: The array where is stored the reading.
Returns:
Returns the number of read registers if successful, -1 otherwise.
Description:
Reads the contents of a specified number of input registers from a slave device (FC=04).
Syntax:
modbus_read_input_registers(ctx, addr, nb, dest);
Parameters:
- ctx: The modbus context object.
- addr: The starting address of the input registers.
- nb: The number of input registers to read.
- dest: The array where is stored the reading.
Returns:
Returns the number of read inputs registers if successful, -1 otherwise.
Description:
Writes a single coil to either 0 or 1 in a slave device (FC=05).
Syntax:
modbus_write_bit(ctx, addr, status);
Parameters:
- ctx: The modbus context object.
- addr: The address of the coil.
- status: The value to write to the coil.
Returns:
Returns 1 if the operation was successful, -1 otherwise.
Description:
Writes a value to a single holding register in a slave device (FC=06).
Syntax:
modbus_write_register(ctx, addr, value);
Parameters:
- ctx: The modbus context object.
- addr: The address of the holding register.
- value: The value to write to the holding register.
Returns:
Returns 1 if the operation was successful, -1 otherwise.
Description:
Writes values to a sequence of coils in a slave device (FC=15).
Syntax:
modbus_write_bits(ctx, addr, nb, src);
Parameters:
- ctx: The modbus context object.
- addr: The starting address of the coils.
- nb: The number of coils to write.
- src: The array of values to write to the coils.
Returns:
Returns the number of written coils if successful, -1 otherwise.
Description:
Writes values to a sequence of holding registers in a slave device (FC=16).
Syntax:
modbus_write_registers(ctx, addr, nb, src);
Parameters:
- ctx: The modbus context object.
- addr: The starting address of the holding registers.
- nb: The number of holding registers to write.
- src: The array of values to write to the holding registers.
Returns:
Returns the number of written registers if successful, -1 otherwise.
There are no comments for now.