mbmaster.h
浏览该文件的文档.
1 
6 #ifndef MBMASTER_API_H
7 #define MBMASTER_API_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include "k_api.h"
17 
18 #include "mbmaster_default_config.h"
19 #include "adu.h"
20 #include "auxiliary.h"
21 
29 /* This enum is used to define the parity */
30 typedef enum {
35 
36 /* This enum is used to define the return value */
37 typedef enum mb_status {
38  MB_SUCCESS = 0u,
53 
54 /* This enum is used to define the modbus mode */
55 typedef enum {
58  MB_TCP
60 
61 /* forward declaration */
62 typedef struct mb_handler mb_handler_t;
63 
64 /* Define the callback function type */
65 typedef mb_status_t (*frame_send_func_t)(mb_handler_t *handler, uint32_t timeout);
69 
70 typedef struct mb_handler {
71  uint8_t used;
72  uint8_t slave_addr;
73  uint8_t mode;
74 
75  uint8_t mb_frame_buff[ADU_BUF_MAX_LENGTH];
76  uint8_t pdu_offset;
77  uint8_t pdu_length;
78  uint32_t mb_frame_length;
80  uint32_t respond_timeout;
81 
87  MB_MUTEX_T mb_mutex;
88 
89  void *private;
90 } mb_handler_t;
91 
101 mb_status_t mbmaster_rtu_init(mb_handler_t **handler, uint8_t port, uint32_t baud_rate, mb_parity_t parity, uint32_t timeout);
102 
109 
122 mb_status_t mbmaster_read_coils(mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr,
123  uint16_t quantity, uint8_t *respond_buf, uint8_t *respond_count,
124  uint32_t timeout);
125 
138 mb_status_t mbmaster_read_discrete_inputs(mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr,
139  uint16_t quantity, uint8_t *respond_buf, uint8_t *respond_count,
140  uint32_t timeout);
141 
155  uint16_t start_addr, uint16_t quantity,
156  uint8_t *respond_buf, uint8_t *respond_count, uint32_t timeout);
157 
170 mb_status_t mbmaster_read_input_registers(mb_handler_t *req_handler, uint8_t slave_addr,
171  uint16_t start_addr, uint16_t quantity,
172  uint8_t *respond_buf, uint8_t *respond_count, uint32_t timeout);
173 
186 mb_status_t mbmaster_write_single_coil(mb_handler_t *req_handler, uint8_t slave_addr, uint16_t coil_addr,
187  uint16_t coil_value, uint16_t *resp_addr, uint16_t *resp_value,
188  uint8_t *exception_code, uint32_t timeout);
189 
202 mb_status_t mbmaster_write_single_register(mb_handler_t *req_handler, uint8_t slave_addr, uint16_t register_addr,
203  uint16_t register_value, uint16_t *resp_addr, uint16_t *resp_value,
204  uint8_t *exception_code, uint32_t timeout);
205 
219 mb_status_t mbmaster_write_multiple_coils(mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr,
220  uint16_t quantity, uint8_t *outputs_buf, uint16_t *resp_addr,
221  uint16_t *resp_quantity, uint8_t *exception_code, uint32_t timeout);
235 mb_status_t mbmaster_write_multiple_registers(mb_handler_t *req_handler, uint8_t slave_addr, uint16_t start_addr,
236  uint16_t quantity, uint8_t *outputs_buf, uint16_t *resp_addr,
237  uint16_t *resp_quantity, uint8_t *exception_code, uint32_t timeout);
240 #ifdef __cplusplus
241 }
242 #endif
243 
244 #endif /* MBMASTER_API_H */
245 
enum mb_status mb_status_t
struct mb_handler mb_handler_t
Definition: mbmaster.h:62
mb_status_t mbmaster_rtu_uninit(mb_handler_t *req_handler)
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(* adu_disassemble_func_t)(mb_handler_t *handler)
Definition: mbmaster.h:68
mb_status
Definition: mbmaster.h:37
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_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_rtu_init(mb_handler_t **handler, uint8_t port, uint32_t baud_rate, mb_parity_t parity, 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_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_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(* frame_send_func_t)(mb_handler_t *handler, uint32_t timeout)
Definition: mbmaster.h:65
mb_parity_t
Definition: mbmaster.h:30
mb_status_t(* adu_assemble_func_t)(mb_handler_t *handler)
Definition: mbmaster.h:67
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_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)
mb_status_t(* frame_recv_func_t)(mb_handler_t *handler)
Definition: mbmaster.h:66
mb_mode_t
Definition: mbmaster.h:55
@ MB_CANNOT_GET_HANDLER
Definition: mbmaster.h:46
@ MB_RESPOND_LENGTH_ERR
Definition: mbmaster.h:43
@ MB_INVALID_SLAVE_ADDR
Definition: mbmaster.h:40
@ MB_RESPOND_EXCEPTION
Definition: mbmaster.h:42
@ MB_MUTEX_ERROR
Definition: mbmaster.h:39
@ MB_INVALID_PARAM
Definition: mbmaster.h:41
@ MB_SUCCESS
Definition: mbmaster.h:38
@ MB_SERIAL_UNINIT_FAILED
Definition: mbmaster.h:50
@ MB_RESPOND_TIMEOUT
Definition: mbmaster.h:45
@ MB_FUNCTION_CODE_NOT_SUPPORT
Definition: mbmaster.h:51
@ MB_SLAVE_NO_RESPOND
Definition: mbmaster.h:47
@ MB_RESPOND_FRAME_ERR
Definition: mbmaster.h:44
@ MB_SERIAL_INIT_FAILED
Definition: mbmaster.h:49
@ MB_FRAME_SEND_ERR
Definition: mbmaster.h:48
@ MB_PAR_EVEN
Definition: mbmaster.h:33
@ MB_PAR_NONE
Definition: mbmaster.h:31
@ MB_PAR_ODD
Definition: mbmaster.h:32
@ MB_RTU
Definition: mbmaster.h:56
@ MB_TCP
Definition: mbmaster.h:58
@ MB_ASCII
Definition: mbmaster.h:57
frame_recv_func_t frame_recv
Definition: mbmaster.h:83
adu_assemble_func_t adu_assemble
Definition: mbmaster.h:84
uint8_t mb_frame_buff[ADU_BUF_MAX_LENGTH]
Definition: mbmaster.h:75
uint8_t mode
Definition: mbmaster.h:73
uint8_t used
Definition: mbmaster.h:71
uint32_t respond_timeout
Definition: mbmaster.h:80
MB_MUTEX_T mb_mutex
Definition: mbmaster.h:87
adu_disassemble_func_t adu_disassemble
Definition: mbmaster.h:85
uint8_t pdu_length
Definition: mbmaster.h:77
uint8_t slave_addr
Definition: mbmaster.h:72
uint8_t pdu_offset
Definition: mbmaster.h:76
frame_send_func_t frame_send
Definition: mbmaster.h:82
uint32_t mb_frame_length
Definition: mbmaster.h:78