10
10
11
11
#include <stdint.h>
12
12
13
- #define MB_BROADCAST_ADD 0
14
- #define MB_RESERVED_ADD 248
13
+ /**
14
+ * Modbus Error codes
15
+ */
16
+ enum mb_err_e {
17
+ MB_OK , /**< No error. */
18
+ MB_ERR_INVALID_ARG = -1 , /**< Invalid argument */
19
+ MB_ERR_NOTSUP = 2 , /**< Mode or Function code not supported */
20
+ MB_ERR_TIMEOUT = -3 , /**< Modbus request timed out */
21
+ MB_ERR_INVALID_DATA = -4 ,/**< Invalid response received from slave */
22
+ MB_ERR_CRC_FAIL = -5 , /**< Checksum failed, CRC (for RTU) or LRC (for ASCII) */
23
+ MB_ERR_INIT_FAIL = -6 , /**< Fail to initialize port */
24
+ };
15
25
16
26
/**
17
27
* MODBUS function code
@@ -55,26 +65,46 @@ enum _mb_dir {
55
65
MB_DIR_TX ,/**< Enable write request */
56
66
};
57
67
68
+ /**
69
+ * Read operation structure
70
+ */
71
+ struct _mb_read {
72
+ uint32_t start_add ; /**< Start address */
73
+ uint16_t no_regs ; /**< Number of registers (16-bit) to read */
74
+ };
75
+
76
+ /**
77
+ * Write operation structure
78
+ */
79
+ struct _mb_write {
80
+ uint32_t wr_add ; /**< Start address */
81
+ uint16_t no_regs ; /**< Number of registers (16-bit) to write. For
82
+ writing a 32-bit value no_regs = 2 */
83
+ int len ; /**< Length of data in data buffer */
84
+ uint8_t * data ; /**< buffer containing data to write */
85
+ };
86
+
87
+ /**
88
+ * Modbus operation
89
+ */
90
+ union mb_op {
91
+ struct _mb_read read ; /**< Read operation, see @ref _mb_read */
92
+ struct _mb_write write ; /**< Write operation, see @ref _mb_write */
93
+ };
94
+
58
95
/**
59
96
* MODBUS request structure
60
97
*/
61
98
struct modbus_t {
62
99
uint8_t slaveid ; /**< Slave ID */
63
100
uint8_t function ; /**< Function code */
64
- union {
65
- struct _read {
66
- uint32_t start_add ; /**< Start address */
67
- uint16_t no_regs ; /**< Number of registers (16-bit) to read */
68
- } read ; /**< Read operation structure */
69
- struct _write {
70
- uint32_t wr_add ; /**< Start address */
71
- uint16_t no_regs ; /**< Number of registers (16-bit) to write. For writing a 32-bit value no_regs = 2 */
72
- int len ; /**< Length of data in data buffer */
73
- uint8_t * data ; /**< buffer containing data to write */
74
- } write ; /**< Write operation structure */
75
- };
76
- uint8_t * resp_buf ; /**< Buffer pointer to store MODBUS response. In MODBUS ASCII mode, data will be stored after conversion from ASCII to hex */
77
- uint16_t resp_buflen ; /**< Length of response buffer, On return this value will be updated with actual number of bytes stored in response buffer */
101
+ union mb_op op ; /**< Modbus Operation, see @ref mb_op */
102
+ uint8_t * resp_buf ; /**< Buffer pointer to store MODBUS response.
103
+ In MODBUS ASCII mode, data will be stored
104
+ after conversion from ASCII to hex */
105
+ uint16_t resp_buflen ; /**< Length of response buffer, On return this
106
+ value will be updated with actual number of
107
+ bytes stored in response buffer */
78
108
uint8_t resp_timeout ; /**< MODBUS response timeout in milliseconds */
79
109
int reserved ; /**< Reserved Unused */
80
110
};
@@ -94,14 +124,14 @@ extern "C" {
94
124
* @param com [in] UART device file (e.g. /dev/ttyS1)
95
125
* @param mode [in] Operation mode @see _mb_mode
96
126
* @param func [in] Direction control callback function
97
- * @return 0 on success, error code otherwise
127
+ * @return For return value see @ref mb_err_e
98
128
*/
99
129
int mb_init (char * com , int mode , mb_dirctl func );
100
130
101
131
/**
102
132
* Perform a MODBUS request
103
133
* @param mbus [in] Modbus handle from @ref mb_init
104
- * @return 0 on success, error code otherwise
134
+ * @return For return value see @ref mb_err_e
105
135
*/
106
136
int mb_request (struct modbus_t * mbus );
107
137
@@ -125,7 +155,7 @@ unsigned char mb_lrc(unsigned char *msg, int len);
125
155
/**
126
156
* MODBUS exception to string
127
157
* @param errnum [in] Exception code
128
- * @return error string (Do not free)
158
+ * @return error description as string
129
159
*/
130
160
char * mb_strerror (uint8_t errnum );
131
161
0 commit comments