mbmaster

结构体

struct  mb_handler
 

类型定义

typedef enum mb_status mb_status_t
 
typedef struct mb_handler mb_handler_t
 
typedef mb_status_t(* frame_send_func_t) (mb_handler_t *handler, uint32_t timeout)
 
typedef mb_status_t(* frame_recv_func_t) (mb_handler_t *handler)
 
typedef mb_status_t(* adu_assemble_func_t) (mb_handler_t *handler)
 
typedef mb_status_t(* adu_disassemble_func_t) (mb_handler_t *handler)
 

枚举

enum  mb_parity_t { MB_PAR_NONE , MB_PAR_ODD , MB_PAR_EVEN }
 
enum  mb_status {
  MB_SUCCESS = 0u , MB_MUTEX_ERROR , MB_INVALID_SLAVE_ADDR , MB_INVALID_PARAM ,
  MB_RESPOND_EXCEPTION , MB_RESPOND_LENGTH_ERR , MB_RESPOND_FRAME_ERR , MB_RESPOND_TIMEOUT ,
  MB_CANNOT_GET_HANDLER , MB_SLAVE_NO_RESPOND , MB_FRAME_SEND_ERR , MB_SERIAL_INIT_FAILED ,
  MB_SERIAL_UNINIT_FAILED , MB_FUNCTION_CODE_NOT_SUPPORT
}
 
enum  mb_mode_t { MB_RTU , MB_ASCII , MB_TCP }
 

函数

mb_status_t mbmaster_rtu_init (mb_handler_t **handler, uint8_t port, uint32_t baud_rate, mb_parity_t parity, uint32_t timeout)
 
mb_status_t mbmaster_rtu_uninit (mb_handler_t *req_handler)
 
mb_status_t mbmaster_read_coils (mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr, uint16_t quantity, uint8_t *respond_buf, uint8_t *respond_count, uint32_t timeout)
 
mb_status_t mbmaster_read_discrete_inputs (mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr, uint16_t quantity, uint8_t *respond_buf, uint8_t *respond_count, uint32_t timeout)
 
mb_status_t mbmaster_read_holding_registers (mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr, uint16_t quantity, uint8_t *respond_buf, uint8_t *respond_count, uint32_t timeout)
 
mb_status_t mbmaster_read_input_registers (mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr, uint16_t quantity, uint8_t *respond_buf, uint8_t *respond_count, uint32_t timeout)
 
mb_status_t mbmaster_write_single_coil (mb_handler_t *req_handler, uint8_t slave_addr, uint16_t coil_addr, uint16_t coil_value, uint16_t *resp_addr, uint16_t *resp_value, uint8_t *exception_code, uint32_t timeout)
 
mb_status_t mbmaster_write_single_register (mb_handler_t *req_handler, uint8_t slave_addr, uint16_t register_addr, uint16_t register_value, uint16_t *resp_addr, uint16_t *resp_value, uint8_t *exception_code, uint32_t timeout)
 
mb_status_t mbmaster_write_multiple_coils (mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr, uint16_t quantity, uint8_t *outputs_buf, uint16_t *resp_addr, uint16_t *resp_quantity, uint8_t *exception_code, uint32_t timeout)
 
mb_status_t mbmaster_write_multiple_registers (mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr, uint16_t quantity, uint8_t *outputs_buf, uint16_t *resp_addr, uint16_t *resp_quantity, uint8_t *exception_code, uint32_t timeout)
 

详细描述

modbus master protocol.

类型定义说明

◆ adu_assemble_func_t

typedef mb_status_t(* adu_assemble_func_t) (mb_handler_t *handler)

在文件 mbmaster.h67 行定义.

◆ adu_disassemble_func_t

typedef mb_status_t(* adu_disassemble_func_t) (mb_handler_t *handler)

在文件 mbmaster.h68 行定义.

◆ frame_recv_func_t

typedef mb_status_t(* frame_recv_func_t) (mb_handler_t *handler)

在文件 mbmaster.h66 行定义.

◆ frame_send_func_t

typedef mb_status_t(* frame_send_func_t) (mb_handler_t *handler, uint32_t timeout)

在文件 mbmaster.h65 行定义.

◆ mb_handler_t

typedef struct mb_handler mb_handler_t

在文件 mbmaster.h1 行定义.

◆ mb_status_t

typedef enum mb_status mb_status_t

枚举类型说明

◆ mb_mode_t

enum mb_mode_t
枚举值
MB_RTU 
MB_ASCII 
MB_TCP 

在文件 mbmaster.h55 行定义.

◆ mb_parity_t

枚举值
MB_PAR_NONE 
MB_PAR_ODD 
MB_PAR_EVEN 

在文件 mbmaster.h30 行定义.

◆ mb_status

enum mb_status
枚举值
MB_SUCCESS 
MB_MUTEX_ERROR 
MB_INVALID_SLAVE_ADDR 
MB_INVALID_PARAM 
MB_RESPOND_EXCEPTION 

remote device respond exception

MB_RESPOND_LENGTH_ERR 

The length field is not consistent with the data length

MB_RESPOND_FRAME_ERR 

frame is too short or CRC error

MB_RESPOND_TIMEOUT 
MB_CANNOT_GET_HANDLER 

alloc handler failed. check whether MBMASTER_CONFIG_HANDLER_MAX is set large enough

MB_SLAVE_NO_RESPOND 
MB_FRAME_SEND_ERR 
MB_SERIAL_INIT_FAILED 
MB_SERIAL_UNINIT_FAILED 
MB_FUNCTION_CODE_NOT_SUPPORT 

在文件 mbmaster.h37 行定义.

函数说明

◆ mbmaster_read_coils()

mb_status_t mbmaster_read_coils ( mb_handler_t req_handler,
uint8_t  slave_addr,
uint16_t  start_addr,
uint16_t  quantity,
uint8_t *  respond_buf,
uint8_t *  respond_count,
uint32_t  timeout 
)

This function will initiate a read coils request(function code 0x01)

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
[in]slave_addrslave address of the remote device
[in]start_addrstarting address
[in]quantityquantity of coils
[out]respond_bufdata receive from remote device, length must >= (quantity / 8 + 1) bytes
[out]respond_countthe number of bytes of data received
[in]timeoutthe timeout time(ms) for sending the request, 0xFFFFFFFF means waiting for ever
返回
the operation status, MB_SUCCESS is OK, other is error
注解
when MB_RESPOND_EXCEPTION is returned, the first byte of respond_buf holds the exception code

◆ mbmaster_read_discrete_inputs()

mb_status_t mbmaster_read_discrete_inputs ( mb_handler_t req_handler,
uint8_t  slave_addr,
uint16_t  start_addr,
uint16_t  quantity,
uint8_t *  respond_buf,
uint8_t *  respond_count,
uint32_t  timeout 
)

This function will initiate a read discrete inputs request(function code 0x02)

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
[in]slave_addrslave address of the remote device
[in]start_addrstarting address
[in]quantityquantity of inputs
[out]respond_bufdata receive from remote device, length must >= (quantity / 8 + 1) bytes
[out]respond_countthe number of bytes of data received
[in]timeoutthe timeout time(ms) for sending the request, 0xFFFFFFFF means waiting for ever
返回
the operation status, MB_SUCCESS is OK, other is error
注解
when MB_RESPOND_EXCEPTION is returned, the first byte of respond_buf holds the exception code

◆ mbmaster_read_holding_registers()

mb_status_t mbmaster_read_holding_registers ( mb_handler_t req_handler,
uint8_t  slave_addr,
uint16_t  start_addr,
uint16_t  quantity,
uint8_t *  respond_buf,
uint8_t *  respond_count,
uint32_t  timeout 
)

This function will initiate a read holding registers request(function code 0x03)

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
[in]slave_addrslave address of the remote device
[in]start_addrstarting address
[in]quantityquantity of registers
[out]respond_bufdata receive from remote device, length must >= (quantity * 2) bytes
[out]respond_countthe number of bytes of data received
[in]timeoutthe timeout time(ms) for sending the request, 0xFFFFFFFF means waiting for ever
返回
the operation status, MB_SUCCESS is OK, other is error
注解
when MB_RESPOND_EXCEPTION is returned, the first byte of respond_buf holds the exception code

◆ mbmaster_read_input_registers()

mb_status_t mbmaster_read_input_registers ( mb_handler_t req_handler,
uint8_t  slave_addr,
uint16_t  start_addr,
uint16_t  quantity,
uint8_t *  respond_buf,
uint8_t *  respond_count,
uint32_t  timeout 
)

This function will initiate a read input registers request(function code 0x04)

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
[in]slave_addrslave address of the remote device
[in]start_addrstarting address
[in]quantityquantity of input registers
[out]respond_bufdata receive from remote device, length must >= (quantity * 2) bytes
[out]respond_countthe number of bytes of data received
[in]timeoutthe timeout time(ms) for sending the request, 0xFFFFFFFF means waiting for ever
返回
the operation status, MB_SUCCESS is OK, other is error
注解
when MB_RESPOND_EXCEPTION is returned, the first byte of respond_buf holds the exception code

◆ mbmaster_rtu_init()

mb_status_t mbmaster_rtu_init ( mb_handler_t **  handler,
uint8_t  port,
uint32_t  baud_rate,
mb_parity_t  parity,
uint32_t  timeout 
)

This function will alloc a handler and initializes the port for communication

参数
[out]handlerreturns a pointer to the assigned handler
[in]portport usend to communication
[in]baud_ratebaud rate used by port
[in]parityparity used by port
[in]timeouttimeout for response
返回
the operation status, MB_SUCCESS is OK, other is error

◆ mbmaster_rtu_uninit()

mb_status_t mbmaster_rtu_uninit ( mb_handler_t req_handler)

This function will free the handler and uninitializes the port for communication

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
返回
the operation status, MB_SUCCESS is OK, other is error

◆ mbmaster_write_multiple_coils()

mb_status_t mbmaster_write_multiple_coils ( mb_handler_t req_handler,
uint8_t  slave_addr,
uint16_t  start_addr,
uint16_t  quantity,
uint8_t *  outputs_buf,
uint16_t *  resp_addr,
uint16_t *  resp_quantity,
uint8_t *  exception_code,
uint32_t  timeout 
)

This function will initiate a write mutiple coils request(function code 0x0F)

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
[in]slave_addrslave address of the remote device
[in]start_addrstart address
[in]quantityquantity of outputs
[in]outputs_bufoutputs value(bit array, 0 to be OFF. 1 to be ON)
[out]resp_addrstart address responded from remote device
[out]resp_quantityquantity responded from remote device
[out]excepiton_codeexception code, when MB_RESPOND_EXCEPTION is returned
[in]timeoutthe timeout time(ms) for sending the request, 0xFFFFFFFF means waiting for ever
返回
the operation status, MB_SUCCESS is OK, other is error

◆ mbmaster_write_multiple_registers()

mb_status_t mbmaster_write_multiple_registers ( mb_handler_t req_handler,
uint8_t  slave_addr,
uint16_t  start_addr,
uint16_t  quantity,
uint8_t *  outputs_buf,
uint16_t *  resp_addr,
uint16_t *  resp_quantity,
uint8_t *  exception_code,
uint32_t  timeout 
)

This function will initiate a write mutiple registers request(function code 0x10)

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
[in]slave_addrslave address of the remote device
[in]start_addrstart address
[in]quantityquantity of registers
[in]outputs_bufregisters value
[out]resp_addrstart address responded from remote device
[out]resp_quantityregisters quantity responded from remote device
[out]excepiton_codeexception code, when MB_RESPOND_EXCEPTION is returned
[in]timeoutthe timeout time(ms) for sending the request, 0xFFFFFFFF means waiting for ever
返回
the operation status, MB_SUCCESS is OK, other is error

◆ mbmaster_write_single_coil()

mb_status_t mbmaster_write_single_coil ( mb_handler_t req_handler,
uint8_t  slave_addr,
uint16_t  coil_addr,
uint16_t  coil_value,
uint16_t *  resp_addr,
uint16_t *  resp_value,
uint8_t *  exception_code,
uint32_t  timeout 
)

This function will initiate a write single coil request(function code 0x05)

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
[in]slave_addrslave address of the remote device
[in]register_addrcoil address
[in]register_valuecoil value, 0x0000 for off, 0xFF00 for on
[out]resp_addrcoil address responded from remote device
[out]resp_valuecoil value responded from remote device
[out]excepiton_codeexception code, when MB_RESPOND_EXCEPTION is returned
[in]timeoutthe timeout time(ms) for sending the request, 0xFFFFFFFF means waiting for ever
返回
the operation status, MB_SUCCESS is OK, other is error

◆ mbmaster_write_single_register()

mb_status_t mbmaster_write_single_register ( mb_handler_t req_handler,
uint8_t  slave_addr,
uint16_t  register_addr,
uint16_t  register_value,
uint16_t *  resp_addr,
uint16_t *  resp_value,
uint8_t *  exception_code,
uint32_t  timeout 
)

This function will initiate a write single register request(function code 0x06)

参数
[in]req_handlerthe handler pointer returned from mbmaster_rtu_init
[in]slave_addrslave address of the remote device
[in]register_addrregister address
[in]register_valueregitter value
[out]resp_addrregister address responded from remote device
[out]resp_valueregister value responded from remote device
[out]excepiton_codeexception code, when MB_RESPOND_EXCEPTION is returned
[in]timeoutthe timeout time(ms) for sending the request, 0xFFFFFFFF means waiting for ever
返回
the operation status, MB_SUCCESS is OK, other is error