GaIA BIOS Library
Loading...
Searching...
No Matches
modbus_tcp_master.h File Reference

Modbus/TCP master function definitions. More...

#include <stdbool.h>
#include <stdint.h>
#include <modbus_core/modbus.h>
#include <modbus_core/modbus_exceptions.h>
#include <modbus_core/modbus_types.h>
#include <modbus_core/modbus_rtos.h>
#include <modbus_tcp/modbus_tcp.h>

Data Structures

struct  mb_tcp_master_ch_t
 Structure representing a Modbus/TCP master channel. More...

Macros

#define MB_TRANS_EVENT_COMPLETED   0
 Modbus/TCP transaction successfully completed event.
#define MB_TRANS_EVENT_TIMEOUT   1
 Modbus/TCP transaction timeout event.

Typedefs

typedef void(* transaction_callback) (uint16_t trans_id, int event)
 Transaction callback.

Functions

mb_tcp_master_ch_tmb_tcp_master_open (const ip_addr_t *slave_addr, uint16_t slave_port, uint8_t trans_count, transaction_callback trans_callback)
 Opens a master channel.
int mb_tcp_master_connect (mb_tcp_master_ch_t *ch, uint32_t timeout)
 Opens the connection to the slave.
int mb_tcp_master_disconnect (mb_tcp_master_ch_t *ch, uint32_t timeout)
 Closes the connection to the slave.
int mb_tcp_master_close (mb_tcp_master_ch_t *ch)
 Closes a Modbus/TCP master channel.
int mb_tcp_master_loop (mb_tcp_master_ch_t *ch)
 Processes pending transactions.
int mb_tcp_rd_coils (mb_tcp_master_ch_t *ch, uint8_t *buf, uint8_t unit_id, uint16_t addr, uint16_t qty, uint16_t timeout)
 Makes a read coils request on the provided channel.
int mb_tcp_rd_discrete_inputs (mb_tcp_master_ch_t *ch, uint8_t *buf, uint8_t unit_id, uint16_t addr, uint16_t qty, uint16_t timeout)
 Makes a read discrete inputs request on the provided channel.
int mb_tcp_rd_holding_regs (mb_tcp_master_ch_t *ch, uint16_t *buf, uint8_t unit_id, uint16_t addr, uint16_t qty, uint16_t timeout)
 Makes a read holding registers request on the provided channel.
int mb_tcp_rd_input_regs (mb_tcp_master_ch_t *ch, uint16_t *buf, uint8_t unit_id, uint16_t addr, uint16_t qty, uint16_t timeout)
 Makes a read input registers request on the provided channel.
int mb_tcp_wr_single_coil (mb_tcp_master_ch_t *ch, bool value, uint8_t unit_id, uint16_t addr, uint16_t timeout)
 Makes a write single coil request on the provided channel.
int mb_tcp_wr_single_reg (mb_tcp_master_ch_t *ch, uint16_t value, uint8_t unit_id, uint16_t addr, uint16_t timeout)
 Makes a write single register request on the provided channel.
int mb_tcp_wr_coils (mb_tcp_master_ch_t *ch, uint8_t *values, uint8_t unit_id, uint16_t addr, uint16_t qty, uint16_t timeout)
 Makes a write multiple coils request on the provided channel.
int mb_tcp_wr_regs (mb_tcp_master_ch_t *ch, uint16_t *values, uint8_t unit_id, uint16_t addr, uint16_t qty, uint16_t timeout)
 Makes a write multiple coils request on the provided channel.

Detailed Description

Modbus/TCP master function definitions.

Author
Tecnologix, 2021

Typedef Documentation

◆ transaction_callback

typedef void(* transaction_callback) (uint16_t trans_id, int event)

Transaction callback.

Parameters
trans_idtransaction id.
eventtransaction event. It can be MB_TRANS_EVENT_COMPLETED or MB_TRANS_EVENT_TIMEOUT.

Function Documentation

◆ mb_tcp_master_close()

int mb_tcp_master_close ( mb_tcp_master_ch_t * ch)

Closes a Modbus/TCP master channel.

The connection must be closed before calling this function.

Parameters
chopened master channel to be closed.
Returns
0 if the channel was successfully closed, a negative number otherwise.

◆ mb_tcp_master_connect()

int mb_tcp_master_connect ( mb_tcp_master_ch_t * ch,
uint32_t timeout )

Opens the connection to the slave.

Parameters
chopened master channel.
timeouttimeout for the connection to be established.
Returns
0 if the connection was successfully opened, a negative number otherwise.

◆ mb_tcp_master_disconnect()

int mb_tcp_master_disconnect ( mb_tcp_master_ch_t * ch,
uint32_t timeout )

Closes the connection to the slave.

Parameters
chopened master channel.
timeouttimeout for the connection to be closed.
Returns
0 if the connection was successfully closed, a negative number otherwise.

◆ mb_tcp_master_loop()

int mb_tcp_master_loop ( mb_tcp_master_ch_t * ch)

Processes pending transactions.

This function is supposed to be called in a loop and processes responses and timeouts calling the callback function provided when the master channel was opened.

Parameters
chopened master channel.
Returns
number of transactions that were processed (completed or timed out).

◆ mb_tcp_master_open()

mb_tcp_master_ch_t * mb_tcp_master_open ( const ip_addr_t * slave_addr,
uint16_t slave_port,
uint8_t trans_count,
transaction_callback trans_callback )

Opens a master channel.

Parameters
slave_addrIP address of the slave.
slave_portslave TCP port to connect to.
trans_countmaximum number of concurrent transactions.
trans_callbackfunction to be invoked when a transaction ends or its timeout expires.
Returns
the opened channel or NULL if something went wrong.

◆ mb_tcp_rd_coils()

int mb_tcp_rd_coils ( mb_tcp_master_ch_t * ch,
uint8_t * buf,
uint8_t unit_id,
uint16_t addr,
uint16_t qty,
uint16_t timeout )

Makes a read coils request on the provided channel.

The transaction response is processed by the mb_tcp_master_loop function.

Parameters
chopened master channel.
bufbuffer to store the data read. It must be large enough to store the response.
unit_idunit id of the slave.
addrcoils starting address.
qtynumber of coils to be read.
timeouttransaction timeout.
Returns
the 16-bit transaction id or a negative number if an error occurs.

◆ mb_tcp_rd_discrete_inputs()

int mb_tcp_rd_discrete_inputs ( mb_tcp_master_ch_t * ch,
uint8_t * buf,
uint8_t unit_id,
uint16_t addr,
uint16_t qty,
uint16_t timeout )

Makes a read discrete inputs request on the provided channel.

The transaction response is processed by the mb_tcp_master_loop function.

Parameters
chopened master channel.
bufbuffer to store the data read. It must be large enough to store the response.
unit_idunit id of the slave.
addrdiscrete inputs starting address.
qtynumber of discrete inputs to be read.
timeouttransaction timeout.
Returns
the 16-bit transaction id or a negative number if an error occurs.

◆ mb_tcp_rd_holding_regs()

int mb_tcp_rd_holding_regs ( mb_tcp_master_ch_t * ch,
uint16_t * buf,
uint8_t unit_id,
uint16_t addr,
uint16_t qty,
uint16_t timeout )

Makes a read holding registers request on the provided channel.

The transaction response is processed by the mb_tcp_master_loop function.

Parameters
chopened master channel.
bufbuffer to store the data read. It must be large enough to store the response.
unit_idunit id of the slave.
addrholding regiters starting address.
qtynumber of holding registers to be read.
timeouttransaction timeout.
Returns
the 16-bit transaction id or a negative number if an error occurs.

◆ mb_tcp_rd_input_regs()

int mb_tcp_rd_input_regs ( mb_tcp_master_ch_t * ch,
uint16_t * buf,
uint8_t unit_id,
uint16_t addr,
uint16_t qty,
uint16_t timeout )

Makes a read input registers request on the provided channel.

The transaction response is processed by the mb_tcp_master_loop function.

Parameters
chopened master channel.
bufbuffer to store the data read. It must be large enough to store the response.
unit_idunit id of the slave.
addrinput registers starting address.
qtynumber of input registers to be read.
timeouttransaction timeout.
Returns
the 16-bit transaction id or a negative number if an error occurs.

◆ mb_tcp_wr_coils()

int mb_tcp_wr_coils ( mb_tcp_master_ch_t * ch,
uint8_t * values,
uint8_t unit_id,
uint16_t addr,
uint16_t qty,
uint16_t timeout )

Makes a write multiple coils request on the provided channel.

The transaction response is processed by the mb_tcp_master_loop function.

Parameters
chopened master channel.
valuesvalue to be written.
unit_idunit id of the slave.
addrstarting address of the coils to be written.
qtynumber of coils to be written.
timeouttransaction timeout.
Returns
the 16-bit transaction id or a negative number if an error occurs.

◆ mb_tcp_wr_regs()

int mb_tcp_wr_regs ( mb_tcp_master_ch_t * ch,
uint16_t * values,
uint8_t unit_id,
uint16_t addr,
uint16_t qty,
uint16_t timeout )

Makes a write multiple coils request on the provided channel.

The transaction response is processed by the mb_tcp_master_loop function.

Parameters
chopened master channel.
valuesvalue to be written.
unit_idunit id of the slave.
addrstarting address of the registers to be written.
qtynumber of registers to be written.
timeouttransaction timeout.
Returns
the 16-bit transaction id or a negative number if an error occurs.

◆ mb_tcp_wr_single_coil()

int mb_tcp_wr_single_coil ( mb_tcp_master_ch_t * ch,
bool value,
uint8_t unit_id,
uint16_t addr,
uint16_t timeout )

Makes a write single coil request on the provided channel.

The transaction response is processed by the mb_tcp_master_loop function.

Parameters
chopened master channel.
valuevalue to be written.
unit_idunit id of the slave.
addraddress of the coil to be written.
timeouttransaction timeout.
Returns
the 16-bit transaction id or a negative number if an error occurs.

◆ mb_tcp_wr_single_reg()

int mb_tcp_wr_single_reg ( mb_tcp_master_ch_t * ch,
uint16_t value,
uint8_t unit_id,
uint16_t addr,
uint16_t timeout )

Makes a write single register request on the provided channel.

The transaction response is processed by the mb_tcp_master_loop function.

Parameters
chopened master channel.
valuevalue to be written.
unit_idunit id of the slave.
addraddress of the register to be written.
timeouttransaction timeout.
Returns
the 16-bit transaction id or a negative number if an error occurs.