Skip to content

Commit d0f951a

Browse files
committed
Update modbus header file and documentation
Signed-off-by: Ajay Bhargav <[email protected]>
1 parent bd1c329 commit d0f951a

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

docs/book/api/proto/modbus.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Modbus Slave
2-
============
1+
Modbus Master
2+
=============
33

44
API Reference
55
-------------

include/proto/modbus.h

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,18 @@
1010

1111
#include <stdint.h>
1212

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+
};
1525

1626
/**
1727
* MODBUS function code
@@ -55,26 +65,46 @@ enum _mb_dir {
5565
MB_DIR_TX,/**< Enable write request */
5666
};
5767

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+
5895
/**
5996
* MODBUS request structure
6097
*/
6198
struct modbus_t {
6299
uint8_t slaveid; /**< Slave ID */
63100
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 */
78108
uint8_t resp_timeout; /**< MODBUS response timeout in milliseconds */
79109
int reserved; /**< Reserved Unused */
80110
};
@@ -94,14 +124,14 @@ extern "C" {
94124
* @param com [in] UART device file (e.g. /dev/ttyS1)
95125
* @param mode [in] Operation mode @see _mb_mode
96126
* @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
98128
*/
99129
int mb_init(char *com, int mode, mb_dirctl func);
100130

101131
/**
102132
* Perform a MODBUS request
103133
* @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
105135
*/
106136
int mb_request(struct modbus_t *mbus);
107137

@@ -125,7 +155,7 @@ unsigned char mb_lrc(unsigned char *msg, int len);
125155
/**
126156
* MODBUS exception to string
127157
* @param errnum [in] Exception code
128-
* @return error string (Do not free)
158+
* @return error description as string
129159
*/
130160
char *mb_strerror(uint8_t errnum);
131161

0 commit comments

Comments
 (0)