Zephyr API 3.6.99
Loading...
Searching...
No Matches
modbus.h File Reference

Go to the source code of this file.

Data Structures

struct  modbus_adu
 Frame struct used internally and for raw ADU support. More...
 
struct  modbus_user_callbacks
 Modbus Server User Callback structure. More...
 
struct  modbus_serial_param
 Modbus serial line parameter. More...
 
struct  modbus_server_param
 Modbus server parameter. More...
 
struct  modbus_raw_cb
 
struct  modbus_iface_param
 User parameter structure to configure Modbus interface as client or server. More...
 

Macros

#define MODBUS_MBAP_LENGTH   7
 Length of MBAP Header.
 
#define MODBUS_MBAP_AND_FC_LENGTH   (MODBUS_MBAP_LENGTH + 1)
 Length of MBAP Header plus function code.
 
#define MODBUS_CUSTOM_FC_DEFINE(name, user_cb, user_fc, userdata)
 INTERNAL_HIDDEN.
 
Modbus exception codes
#define MODBUS_EXC_NONE   0
 No exception.
 
#define MODBUS_EXC_ILLEGAL_FC   1
 Illegal function code.
 
#define MODBUS_EXC_ILLEGAL_DATA_ADDR   2
 Illegal data address.
 
#define MODBUS_EXC_ILLEGAL_DATA_VAL   3
 Illegal data value.
 
#define MODBUS_EXC_SERVER_DEVICE_FAILURE   4
 Server device failure.
 
#define MODBUS_EXC_ACK   5
 Acknowledge.
 
#define MODBUS_EXC_SERVER_DEVICE_BUSY   6
 Server device busy.
 
#define MODBUS_EXC_MEM_PARITY_ERROR   8
 Memory parity error.
 
#define MODBUS_EXC_GW_PATH_UNAVAILABLE   10
 Gateway path unavailable.
 
#define MODBUS_EXC_GW_TARGET_FAILED_TO_RESP   11
 Gateway target device failed to respond.
 

Typedefs

typedef int(* modbus_raw_cb_t) (const int iface, const struct modbus_adu *adu, void *user_data)
 ADU raw callback function signature.
 
typedef bool(* modbus_custom_cb_t) (const int iface, const struct modbus_adu *const rx_adu, struct modbus_adu *const tx_adu, uint8_t *const excep_code, void *const user_data)
 Custom function code handler function signature.
 

Enumerations

enum  modbus_mode { MODBUS_MODE_RTU , MODBUS_MODE_ASCII , MODBUS_MODE_RAW }
 Modbus interface mode. More...
 

Functions

int modbus_read_coils (const int iface, const uint8_t unit_id, const uint16_t start_addr, uint8_t *const coil_tbl, const uint16_t num_coils)
 Coil read (FC01)
 
int modbus_read_dinputs (const int iface, const uint8_t unit_id, const uint16_t start_addr, uint8_t *const di_tbl, const uint16_t num_di)
 Read discrete inputs (FC02)
 
int modbus_read_holding_regs (const int iface, const uint8_t unit_id, const uint16_t start_addr, uint16_t *const reg_buf, const uint16_t num_regs)
 Read holding registers (FC03)
 
int modbus_read_input_regs (const int iface, const uint8_t unit_id, const uint16_t start_addr, uint16_t *const reg_buf, const uint16_t num_regs)
 Read input registers (FC04)
 
int modbus_write_coil (const int iface, const uint8_t unit_id, const uint16_t coil_addr, const bool coil_state)
 Write single coil (FC05)
 
int modbus_write_holding_reg (const int iface, const uint8_t unit_id, const uint16_t start_addr, const uint16_t reg_val)
 Write single holding register (FC06)
 
int modbus_request_diagnostic (const int iface, const uint8_t unit_id, const uint16_t sfunc, const uint16_t data, uint16_t *const data_out)
 Read diagnostic (FC08)
 
int modbus_write_coils (const int iface, const uint8_t unit_id, const uint16_t start_addr, uint8_t *const coil_tbl, const uint16_t num_coils)
 Write coils (FC15)
 
int modbus_write_holding_regs (const int iface, const uint8_t unit_id, const uint16_t start_addr, uint16_t *const reg_buf, const uint16_t num_regs)
 Write holding registers (FC16)
 
int modbus_read_holding_regs_fp (const int iface, const uint8_t unit_id, const uint16_t start_addr, float *const reg_buf, const uint16_t num_regs)
 Read floating-point holding registers (FC03)
 
int modbus_write_holding_regs_fp (const int iface, const uint8_t unit_id, const uint16_t start_addr, float *const reg_buf, const uint16_t num_regs)
 Write floating-point holding registers (FC16)
 
int modbus_iface_get_by_name (const char *iface_name)
 Get Modbus interface index according to interface name.
 
int modbus_init_server (const int iface, struct modbus_iface_param param)
 Configure Modbus Interface as raw ADU server.
 
int modbus_init_client (const int iface, struct modbus_iface_param param)
 Configure Modbus Interface as raw ADU client.
 
int modbus_disable (const uint8_t iface)
 Disable Modbus Interface.
 
int modbus_raw_submit_rx (const int iface, const struct modbus_adu *adu)
 Submit raw ADU.
 
void modbus_raw_put_header (const struct modbus_adu *adu, uint8_t *header)
 Put MBAP header into a buffer.
 
void modbus_raw_get_header (struct modbus_adu *adu, const uint8_t *header)
 Get MBAP header from a buffer.
 
void modbus_raw_set_server_failure (struct modbus_adu *adu)
 Set Server Device Failure exception.
 
int modbus_raw_backend_txn (const int iface, struct modbus_adu *adu)
 Use interface as backend to send and receive ADU.
 
int modbus_register_user_fc (const int iface, struct modbus_custom_fc *custom_fc)
 Register a user-defined function code handler.