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

Modbus serial defnitions and functions. More...

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

Data Structures

struct  mb_ser_adu_t
 This structure represents a serial line modbus ADU. More...
struct  timer_t
struct  mb_ser_channel_t
 This structure represents the configuration of a modbus channel. More...

Macros

#define MB_SER_MODE_ASCII   (0)
 Modbus serial ASCII mode.
#define MB_SER_MODE_RTU   (1)
 Modbus serial RTU mode.

Functions

void mb_start_tx (mb_ser_channel_t *ch, mb_ser_adu_t *msg)
 Sends the given message on the given channel.
bool mb_is_rx_msg (mb_ser_channel_t *ch)
 Checks if there is a message in the receiving queue for the given channel.
mb_ser_channel_tmb_serial_open (uintptr_t task_no, uint8_t mb_ser_mode, uint8_t jbus, uint8_t baud, uint8_t frame, uint8_t port, uint8_t timer, mb_data_handlers_t *handlers, uint8_t address, uint16_t timeout)
 Opens a modbus serial channel.
void mb_serial_close (mb_ser_channel_t *ch)
 Closes e modbus channel.
void mb_serial_set_timeout (mb_ser_channel_t *ch, uint16_t timeout)
 Sets the timeout of the specified modbus channel.
uint16_t mb_slave_loop (mb_ser_channel_t *ch, uint32_t timeout)
 Handles the Modbus slave protocol stack.
int mb_ser_rd_coils (mb_ser_channel_t *ch, uint8_t *buf, uint8_t addr, uint16_t start_addr, uint16_t qty, uint16_t timeout)
 Read Coils.
int mb_ser_rd_discrete_inputs (mb_ser_channel_t *ch, uint8_t *buf, uint8_t addr, uint16_t start_addr, uint16_t qty, uint16_t timeout)
 Read Discrete Inputs.
int mb_ser_rd_holding_regs (mb_ser_channel_t *ch, uint16_t *buf, uint8_t addr, uint16_t start_addr, uint16_t qty, uint16_t timeout)
 Read Holding Registers.
int mb_ser_rd_input_regs (mb_ser_channel_t *ch, uint16_t *buf, uint8_t addr, uint16_t start_addr, uint16_t qty, uint16_t timeout)
 Read Input Registers.
int mb_ser_wr_single_coil (mb_ser_channel_t *ch, bool value, uint8_t addr, uint16_t output_addr, uint16_t timeout)
 Write Single Coil.
int mb_ser_wr_single_reg (mb_ser_channel_t *ch, uint16_t value, uint8_t addr, uint16_t output_addr, uint16_t timeout)
 Write Single Register.
int mb_ser_wr_coils (mb_ser_channel_t *ch, uint8_t *values, uint8_t addr, uint16_t start_addr, uint16_t qty, uint16_t timeout)
 Write Multiple Coils.
int mb_ser_wr_regs (mb_ser_channel_t *ch, uint16_t *values, uint8_t addr, uint16_t start_addr, uint16_t qty, uint16_t timeout)
 Write Multiple Registers.

Detailed Description

Modbus serial defnitions and functions.

Author
Tecnologix, 2014

Function Documentation

◆ mb_is_rx_msg()

bool mb_is_rx_msg ( mb_ser_channel_t * ch)

Checks if there is a message in the receiving queue for the given channel.

Parameters
chpointer to the channel to check.
Returns
true if a message was received, false otherwise.

◆ mb_ser_rd_coils()

int mb_ser_rd_coils ( mb_ser_channel_t * ch,
uint8_t * buf,
uint8_t addr,
uint16_t start_addr,
uint16_t qty,
uint16_t timeout )

Read Coils.

Parameters
chmodbus channel.
bufbuffer where to store read values.
addrslave address.
start_addrstarting address of coils to read.
qtynumber of coils to read.
timeoutmaximum time to wait for the response.
Returns
MB_RC_OK data were read without errors, an error code otherwise.

◆ mb_ser_rd_discrete_inputs()

int mb_ser_rd_discrete_inputs ( mb_ser_channel_t * ch,
uint8_t * buf,
uint8_t addr,
uint16_t start_addr,
uint16_t qty,
uint16_t timeout )

Read Discrete Inputs.

Parameters
chmodbus channel.
bufbuffer where to store read values.
addrslave address.
start_addrstarting address of discrete inputs to read.
qtynumber of discrete inputs to read.
timeoutmaximum time to wait for the response.
Returns
MB_RC_OK data were read without errors, an error code otherwise.

◆ mb_ser_rd_holding_regs()

int mb_ser_rd_holding_regs ( mb_ser_channel_t * ch,
uint16_t * buf,
uint8_t addr,
uint16_t start_addr,
uint16_t qty,
uint16_t timeout )

Read Holding Registers.

Parameters
chmodbus channel.
bufbuffer where to store read values.
addrslave address.
start_addrstarting address of holding registers to read.
qtynumber of holding registers to read.
timeoutmaximum time to wait for the response.
Returns
MB_RC_OK data were read without errors, an error code otherwise.

◆ mb_ser_rd_input_regs()

int mb_ser_rd_input_regs ( mb_ser_channel_t * ch,
uint16_t * buf,
uint8_t addr,
uint16_t start_addr,
uint16_t qty,
uint16_t timeout )

Read Input Registers.

Parameters
chmodbus channel.
bufbuffer where to store read values.
addrslave address.
start_addrstarting address of input registers to read.
qtynumber of input registers to read.
timeoutmaximum time to wait for the response.
Returns
MB_RC_OK data were read without errors, an error code otherwise.

◆ mb_ser_wr_coils()

int mb_ser_wr_coils ( mb_ser_channel_t * ch,
uint8_t * values,
uint8_t addr,
uint16_t start_addr,
uint16_t qty,
uint16_t timeout )

Write Multiple Coils.

Parameters
chmodbus channel.
valuesvalues of the coils to set.
addrslave address.
start_addrstarting address of the coils to write.
qtynumber of coils to write.
timeoutmaximum time to wait for the response.
Returns
MB_RC_OK data were written without errors, an error code otherwise.

◆ mb_ser_wr_regs()

int mb_ser_wr_regs ( mb_ser_channel_t * ch,
uint16_t * values,
uint8_t addr,
uint16_t start_addr,
uint16_t qty,
uint16_t timeout )

Write Multiple Registers.

Parameters
chmodbus channel.
valuesvalues of the registers to set.
addrslave address.
start_addrstarting address of the registers to write.
qtynumber of registers to write.
timeoutmaximum time to wait for the response.
Returns
MB_RC_OK data were written without errors, an error code otherwise.

◆ mb_ser_wr_single_coil()

int mb_ser_wr_single_coil ( mb_ser_channel_t * ch,
bool value,
uint8_t addr,
uint16_t output_addr,
uint16_t timeout )

Write Single Coil.

Parameters
chmodbus channel.
valuevalue of the coil to set.
addrslave address.
output_addraddress of the coil to write.
timeoutmaximum time to wait for the response.
Returns
MB_RC_OK data were written without errors, an error code otherwise.

◆ mb_ser_wr_single_reg()

int mb_ser_wr_single_reg ( mb_ser_channel_t * ch,
uint16_t value,
uint8_t addr,
uint16_t output_addr,
uint16_t timeout )

Write Single Register.

Parameters
chmodbus channel.
valuevalue of the register to set.
addrslave address.
output_addraddress of the register to write.
timeoutmaximum time to wait for the response.
Returns
MB_RC_OK data were written without errors, an error code otherwise.

◆ mb_serial_close()

void mb_serial_close ( mb_ser_channel_t * ch)

Closes e modbus channel.

Parameters
chpointer to the modbus channel to close.

◆ mb_serial_open()

mb_ser_channel_t * mb_serial_open ( uintptr_t task_no,
uint8_t mb_ser_mode,
uint8_t jbus,
uint8_t baud,
uint8_t frame,
uint8_t port,
uint8_t timer,
mb_data_handlers_t * handlers,
uint8_t address,
uint16_t timeout )

Opens a modbus serial channel.

Parameters
task_nonumber of the task that processes messages, 0 if signals should not be used.
mb_ser_modeMB_SER_MODE_ASCII or MB_SER_MODE_RTU.
jbuswhether or not this channel use jbus mode.
baudbaudrate to use.
frameconfiguration of the serial port.
portserial port to use.
timertimer to use.
handlerspointer to the data handlers structure containing the function pointers to use to process modbus requests.
addressaddress of the node, 0 if this node is a master.
timeouttimeout to set, expressed in number of time slots of 51 microsec.
Returns
a pointer to the opened channel.

◆ mb_serial_set_timeout()

void mb_serial_set_timeout ( mb_ser_channel_t * ch,
uint16_t timeout )

Sets the timeout of the specified modbus channel.

Parameters
chmodbus channel to set the timeout for.
timeouttimeout to set, expressed in number of time slots of 25 microsec.

◆ mb_slave_loop()

uint16_t mb_slave_loop ( mb_ser_channel_t * ch,
uint32_t timeout )

Handles the Modbus slave protocol stack.

It should be called regularly.

Parameters
chmodbus channel to set the timeout for.
timeoutif not set to 0, it's the maximum time to wait for a signal to wake-up the process calling this function.
Returns
MSG_RECEIVED if a message was received and processed, MSG_TIMEOUT otherwise.

◆ mb_start_tx()

void mb_start_tx ( mb_ser_channel_t * ch,
mb_ser_adu_t * msg )

Sends the given message on the given channel.

Parameters
chpointer to the channel to use.
msgmessage to send.