Porting the MODBUS stack is a necessary step for every platform. The basic procedure is as following
Create a new directory for your port from a skeleton.
Implement the platform abstraction functions for enabling/disabling interrupts.
Implement the timer functions.
In case of RTU/ASCII you have to implement a serial abstraction layer.
In case of TCP you have to implement the functions for handling TCP connections.
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.
We do not provide any additional information for the build process but the following settings work fine.
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/mbmconfig.h (If you use the MODBUS master stack)
demo/PLATFORM/port/mbsconfig.h (If you use the MODBUS slave stack)
demo/platform/port/mbportserial.c (If you use RTU/ASCII)
demo/platform/port/mbporttcp.c (If you use RTU/ASCII)
You should verify the file
The preprocessor include directories should at least the path
It is recommend to create your project file in the
You can to include all the relevant C files from the
mbmaster subdirectory into your build process.
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.
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:
For RTU/ASCII you need to implement serial communication functions. An example is already provided in
A periodic timer with a period of approx. 1ms
A interrupt associated with the timer overflow which calls the interrupt handler prvvTimerISR.
mbportserial.c. The things which need to be added are:
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.
You can enable or disable most functions from the stack. Default values are provided in the file
Create the real initialization code in the function eMBPSerialInit.
Create the shutdown code in eMBPSerialClose if you need it.
Implement enabling/disabling of the transmitter in eMBPSerialTxEnable.
Implement enabling/disabling of the receiver in eMBPSerialRxEnable.
Adapt or change the interrupt functions prrvUSARTTxISR and prrvUSARTRxISR.
/mbslave/include/mbiconfig.h. If you want to override any of these settings simply add them to your project local
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 14 Jun 2015.