Using µC/Modbus

Using µC/Modbus

In order to use µC/Modbus in your product, you need to make sure you have the following elements:

Setup the µC/CPU for the CPU YOU are using:

You need to create a cpu.h and cpu_a.asm files (see section 2.04).

Setup the BSP for the UARTs and the RTU timer YOU are using:

You need to create a mb_bsp.c file (see section 5). You should note that µC/Modbus includes a mb_bsp.c for different processors and boards. You can use these files as examples on how to write the BSP.

Setup the RTOS Interface for the RTOS YOU are using:

µC/Modbus includes RTOS interfaces for µC/OS-II and µC/OS-III (see section 6). If you are using a different RTOS, you will need to provide an mb_os.c file. You can actually model your RTOS interface from the one provided for µC/OS-II and µC/OS-III.

For µC/OS-II and µC/OS-III, don’t forget to configure #defines to setup the task priority and stack size (should be placed in your application’s app_cfg.h file).

If your product doesn’t require the use of a RTOS you can use the No-OS port. This port is only for µC/Modbus-S. µC/Modbus-M always requires a RTOS interface.

Initialize µC/Modbus and configure your channels.

µ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.

 

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.

 

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.

Your application interfaces to µC/Modbus via a number of functions that allow you to change the behavior of channels. For each interface functions µC/Modbus applies to both Master or Slave channels, µC/Modbus-S applies only to Slave channels and µC/Modbus-M applies only to Master channels.