...
µC/Modbus is initialized by simply calling MB_Init()
and specifying the Modbus RTU frequency as an argument. Once initialized, you simply need to configure each Modbus channels (using MB_CfgCh()
) as shown in the example below. Here, our product has three Modbus ports: a Modbus RTU port communicating at 9600 baud and a Modbus ASCII port communicating at 19200 baud and a Modbus ASCII Master port communicating at 19200 baud. Both Modbus Slave ports assume Modbus address 1 but, you can specify different node address for each one if you want.
...
Code Block | ||
---|---|---|
| ||
MB_Init(1000); // Initialize uC/Modbus, RTU timer at 1000 Hz
MB_CfgCh( 1, // ... Modbus Node # for this slave channel
MODBUS_SLAVE, // ... This is a SLAVE
0, // ... 0 when a slave
MODBUS_MODE_RTU, // ... Modbus Mode (_ASCII or _RTU)
1, // ... Specify UART #1
9600, // ... Baud Rate
8, // ... Number of data bits 7 or 8
MODBUS_PARITY_NONE,// ... Parity: _NONE, _ODD or _EVEN 1,
// ... Number of stop bits 1 or 2
MODBUS_WR_EN); // ... Enable (_EN) or disable (_DIS) writes
MB_CfgCh( 1, // ... Modbus Node # for this slave channel
MODBUS_SLAVE, // ... This is a SLAVE
0, // ... 0 when a slave
MODBUS_MODE_ASCII, // ... Modbus Mode (_ASCII or _RTU)
1, // ... Specify UART #2
19200, // ... Baud Rate
8, // ... Number of data bits 7 or 8
MODBUS_PARITY_NONE,// ... Parity: _NONE, _ODD or _EVEN
1, // ... Number of stop bits 1 or 2
MODBUS_WR_EN); // ... Enable (_EN) or disable (_DIS) writes |
Important
If your application is using a RTOS interface, once a µC/Modbus-S channel has been configured, you do not need to do anything else in your code. In other words, a Modbus master can start communicating with your Modbus slave without having to add any additional code in your application tasks! Refer to section 7 for details on how this works.
If your application is not using a RTOS interface, once a µC/Modbus-S channel has been configured, your application needs to call MB_OS_RxTask()to poll the Modbus Slave channels. Refer to section 8 for details on how this works.
Code Block | ||
---|---|---|
| ||
MB_CfgCh( 1, // ... Modbus Node # for this slave channel MODBUS_MASTER, // ... This is a MASTER OS_TICKS_PER_SEC, // ... One second timeout waiting for slave response MODBUS_MODE_ASCII, // ... Modbus Mode (_ASCII or _RTU) 2, // ... Specify UART #3 19200, // ... Baud Rate 8, // ... Number of data bits 7 or 8 MODBUS_PARITY_NONE,// ... Parity: _NONE, _ODD or _EVEN 1, // ... Number of stop bits 1 or 2 MODBUS_WR_EN); // ... Enable (_EN) or disable (_DIS) writes |
Important
Once a µC/Modbus-M channel has been configured, your application code needs to call MBM_FC??_???()
functions as described in this section in order to obtain data from Modbus slaves connected to that channel. Refer to section 8 for details on how this works.
...