Modbus Master
Example Usage
#include <proto/modbus.h>
/* init modbus RTU master, on port UART0 */
mb_init("/dev/ttyS0", MB_RTU, dirctl);
/* configure modbus at 115200bps, 8bits, No parity, 1 stopbit*/
mb_config(115200, "8N1");
/* send modbus request */
struct modbus_t mbreq;
uint8_t buffer[256];
/* set slave id */
mbreq.slaveid = 1;
/* Set function code as Read holding register */
mbreq.function = MB_FUNC_RD_HOLDING;
/* set start address to read from */
mbreq.op.read.start_add = 0x00;
/* read 1 register */
mbreq.op.read.no_regs = 1;
/* configure response buffer */
mbreq.resp_buf = buffer;
mbreq.resp_buflen = sizeof(buffer);
/* set response timeout in milliseconds */
mbreq.resp_timeout = 1000;
/* initiate request */
mb_request(&mbreq);
/* doing, Modbus transceiver direction control, this function is passed as argument to mb_init() */
void dirctl(int dir)
{
if (dir == MB_DIR_RX) {
/* receive, control GPIO or something */
} else {
/* transmit, change gpio again */
}
}
API Reference
Header File
Source: include/proto/modbus.h
#include <proto/modbus.h>
Functions
-
int mb_config(unsigned int baud, const char *com_para)
Configure MODBUS Parameters
- Parameters:
baud – [in] Baudrate value
com_para –
[in] Communication parameter data bits, parity and stopbit in ascii format e.g. “8N1”, “8E2”
Data bits: 8, 7, 6, 5 (not supported on all platforms)
Parity: N - None, E - Even, O - Odd Parity
Stop Bits: 1 or 2
- Returns:
For return value see mb_err_e
-
unsigned short mb_crc(unsigned char *msg, int len)
Calculate MODBUS CRC
- Parameters:
msg – [in] Input buffer
len – [in] Length of data in buffer
- Returns:
CRC value
-
unsigned char mb_lrc(unsigned char *msg, int len)
Calculate MODBUS LRC
- Parameters:
msg – [in] Input buffer
len – [in] Length of data in buffer
- Returns:
LRC value
-
char *mb_strerror(uint8_t errnum)
MODBUS exception to string
- Parameters:
errnum – [in] Exception code
- Returns:
error description as string
Unions
-
union mb_op
- #include <modbus.h>
Modbus operation
Structures
-
struct _mb_read
Read operation structure
-
struct _mb_write
Write operation structure
-
struct modbus_t
MODBUS request structure
Public Members
-
uint8_t slaveid
Slave ID
-
uint8_t function
Function code
-
uint8_t *resp_buf
Buffer pointer to store MODBUS response. In MODBUS ASCII mode, data will be stored after conversion from ASCII to hex
-
uint16_t resp_buflen
Length of response buffer, On return this value will be updated with actual number of bytes stored in response buffer
-
uint8_t resp_timeout
MODBUS response timeout in milliseconds
-
int reserved
Reserved Unused
-
uint8_t slaveid
Type Definitions
Enumerations
-
enum mb_err_e
Modbus Error codes
Values:
-
enumerator MB_OK
No error.
-
enumerator MB_ERR_INVALID_ARG
Invalid argument
-
enumerator MB_ERR_NOTSUP
Mode or Function code not supported
-
enumerator MB_ERR_TIMEOUT
Modbus request timed out
-
enumerator MB_ERR_INVALID_DATA
Invalid response received from slave
-
enumerator MB_ERR_CRC_FAIL
Checksum failed, CRC (for RTU) or LRC (for ASCII)
-
enumerator MB_ERR_INIT_FAIL
Fail to initialize port or Port not initialized
-
enumerator MB_ERR_CONF_FAIL
Fail to configure port
-
enumerator MB_OK
-
enum _mb_func
MODBUS function code
Values:
-
enumerator MB_FUNC_INVAL
Invalid function code
-
enumerator MB_FUNC_RD_COIL
Read coil
-
enumerator MB_FUNC_RD_DISC_INPUT
Read discrete input
-
enumerator MB_FUNC_RD_HOLDING
Read holding register
-
enumerator MB_FUNC_RD_INPUT
Read input register
-
enumerator MB_FUNC_WR_COIL
Write coil
-
enumerator MB_FUNC_WR_REG
Write single holding register
-
enumerator MB_FUNC_RD_EXP
Read exception status
-
enumerator MB_FUNC_DIAG
Diagnostic
-
enumerator MB_FUNC_COMM_EVT_COUNT
Get COM event counter
-
enumerator MB_FUNC_COMM_EVT_LOG
Get COM event log
-
enumerator MB_FUNC_WR_MUL_COIL
Write multiple coils
-
enumerator MB_FUNC_WR_MUL_REG
Write multiple holding register
-
enumerator MB_FUNC_REP_SERV_ID
Report slave ID
-
enumerator MB_FUNC_RD_FILE_REC
Read file record
-
enumerator MB_FUNC_WR_FILE_REC
Write file record
-
enumerator MB_FUNC_MASK_WR_REG
Mask write register
-
enumerator MB_FUNC_RDWR_MUL_REG
Read/Write multiple register
-
enumerator MB_FUNC_RD_FIFO_Q
Read FIFO Queue
-
enumerator MB_FUNC_INVAL
