Porting the MODBUS stack

Porting the MODBUS stack is a necessary step for every platform. The basic procedure is as following
  1. Create a new directory for your port from a skeleton.
  2. Implement the platform abstraction functions for enabling/disabling interrupts.
  3. Implement the timer functions.
  4. In case of RTU/ASCII you have to implement a serial abstraction layer.
  5. In case of TCP you have to implement the functions for handling TCP connections.
  6. In case you have an RTOS you can implement an event queue for better performance.

We start by creating a new directory demo/PLATFORM. In the next step the necessary files from the directory demo/BARE should be copied into this directory following the directory structure from below.

demo/PLATFORM/mbmdemo-ser.c (If you use the MODBUS master and RTU/ASCII)
demo/PLATFORM/mbmdemo-tcp.c (If you use the MODBUS master and TCP)
demo/PLATFORM/port/mbport.h
demo/PLATFORM/port/mbmconfig.h (If you use the MODBUS master stack)
demo/PLATFORM/port/mbsconfig.h (If you use the MODBUS slave stack)
demo/PLATFORM/port/mbportevent.c
demo/PLATFORM/port/mbportother.c
demo/PLATFORM/port/mbporttimer.c
demo/platform/port/mbportserial.c (If you use RTU/ASCII)
demo/platform/port/mbporttcp.c (If you use RTU/ASCII)

Build system

We do not provide any additional information for the build process but the following settings work fine.

Platform specifics (mbport.h)

You should verify the file mbport.h if the data types matches you platform. You can also place additional include files at the top of this file. This file is included by every module of the MODBUS stack.

Implementation of timers (mbporttimer.c)

The MODBUS stack needs timer to detect the end of frame or to signal errors to the use when a slave (or master) does not respond. An example implementation is already provided in mbporttimer.c which needs only very little customization. The only thing which has to be added is the actual initialization of the timer in the function eMBPTimerInit. In addition the prototype for the ISR handler prvvTimerISR should be checked for compiler specifics. The timer itself should be initialized as following:

Implementation of serial functions (mbportserial.c)

For RTU/ASCII you need to implement serial communication functions. An example is already provided in mbportserial.c. The things which need to be added are:

Event queue (mbportevent.c)

The skeleton is already provided but in case of an RTOS the functions should use a real event queue. You can take a look at the provided Linux ports for an example on how to do this.

Customizing the stack

You can enable or disable most functions from the stack. Default values are provided in the file mbmaster/include/mbmiconfig.h or /mbslave/include/mbiconfig.h. If you want to override any of these settings simply add them to your project local mbmconfig.h and mbsconfig.h file. Do not change any files within the MODBUS stack. You can find a detailed description of these options in this doxygen configuration.

(C) 2007 Embedded Solutions. Last updated on 23 Aug 2014.