UM14001 Emmodbus
UM14001 Emmodbus
UM14001 Emmodbus
CPU independent
Modbus stack for
embedded applications
Document: UM14001
Software version: 1.00
Revision: 2
Date: July 1, 2014
www.segger.com
2
Disclaimer
Specifications written in this document are believed to be accurate, but are not guar-
anteed to be entirely free of error. The information in this manual is subject to
change for functional or performance improvements without notice. Please make sure
your manual is the latest edition. While the information herein is assumed to be
accurate, SEGGER Microcontroller GmbH & Co. KG (SEGGER) assumes no responsibil-
ity for any errors or omissions. SEGGER makes and you receive no warranties or con-
ditions, express, implied, statutory or in any communication with you. SEGGER
specifically disclaims any implied warranty of merchantability or fitness for a particu-
lar purpose.
Copyright notice
You may not extract portions of this manual or modify the PDF file in any way without
the prior written permission of SEGGER. The software described in this document is
furnished under a license and may only be used or copied in accordance with the
terms of such a license.
© 2014 SEGGER Microcontroller GmbH & Co. KG, Hilden / Germany
Trademarks
Names mentioned in this manual may be trademarks of their respective companies.
Brand and product names are trademarks or registered trademarks of their respec-
tive holders.
Contact address
SEGGER Microcontroller GmbH & Co. KG
In den Weiden 11
D-40721 Hilden
Germany
Tel.+49 2103-2878-0
Fax.+49 2103-2878-28
E-mail: [email protected]
Internet: http://www.segger.com
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
3
Manual versions
This manual describes the current software version. If any error occurs, inform us
and we will try to assist you as soon as possible.
Contact us for further information on topics or routines not yet specified.
Print date: July 1, 2014
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
4
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
5
UM14001 User & Reference Guide for emModbus © 2014 SEGGER SEGGER Microcontroller GmbH & Co. KG
6
emFile
File system
emFile is an embedded file system with
FAT12, FAT16 and FAT32 support. Vari-
ous Device drivers, e.g. for NAND and
NOR flashes, SD/MMC and Compact-
Flash cards, are available.
USB-Stack
USB device/host stack
A USB stack designed to work on any
embedded system with a USB control-
ler. Bulk communication and most stan-
dard device classes are supported.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
7
Table of Contents
2 Getting Started...............................................................................................................21
2.1 Installation ............................................................................................. 22
2.2 Upgrade a trial version ............................................................................. 23
2.3 Upgrade an embOS start project................................................................ 24
2.4 Create a project from scratch .................................................................... 29
4 Core functions................................................................................................................35
4.1 API functions .......................................................................................... 36
4.2 emModbus data structures ........................................................................ 64
4.3 Error codes ............................................................................................. 69
5 Configuring emModbus..................................................................................................71
5.1 Compile-time configuration ....................................................................... 72
6 Debugging......................................................................................................................75
6.1 Message output ....................................................................................... 76
6.2 Using a network sniffer to analyse ethernet communication problems ............. 81
6.3 Testing emModbus applications ................................................................. 82
7 OS Integration................................................................................................................83
7.1 General information ................................................................................. 84
7.2 OS layer API functions.............................................................................. 85
8 Resource usage.............................................................................................................97
8.1 Memory footprint..................................................................................... 98
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
8
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
9
Chapter 1
Introduction to emModbus
This chapter provides an introduction to using emModbus. It explains the basic con-
cepts behind emModbus.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
10 CHAPTER 1 Introduction to emModbus
Protocol Description
Original Modbus standard. Binary data is sent via serial connec-
RTU
tions such as RS-232 or similar.
ASCII Similar to RTU. Instead of raw binary, data is encoded in ASCII.
Binary data is encapsulated in a TCP frame and sent via net-
Modbus/TCP work connections such as Ethernet. This variant can also be
used with UDP instead of TCP and is then called Modbus/UDP.
Table 1.1: Standard-compliant variants of Modbus message frames
When using ASCII frames or RTU frames via serial connection, parameters such as
baud rate and parity bits must be set correctly for all connected devices.
When using Modbus/TCP, setting these parameters is not required, but correct IP
address and port number are required instead. The standard port number for Mod-
bus/TCP is port 502.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
11
Field 1 includes the address of a slave device, either indicating the slave that is des-
ignated to receive the message from its master, or indicating the slave that sent the
message towards its master. This address, which is refered to as "unit ID" or "slave
address", is a number from 1 to 247 and is uniquely assigned to a single slave
device, allowing these devices to listen for messages containing their specific ID.
Additionaly, ID 0 is used to send broadcasts and ID 255 usually is reserved for com-
munications with a Modbus gateway.
Field 2 includes a function code, which, when sent by a master, indicates the instruc-
tion a slave is asked to carry out. When sent by a slave, on the other hand, the func-
tion code indicates the instruction the slave is responding to.
Field 3 contains variable amounts of data, e.g. certain data addresses a master wants
a slave to read, or the data a slave is reporting towards its master.
In field 4 Modbus messages carry a checksum to allow their respective recipients to
determine wether a message has arrived completely.
RTU message frames
When using RTU frames, each byte contained in a message is sent as binary data.
The main advantage of this mode is its greater density, allowing better data through-
put for the same baud rate when compared to ASCII frames. To indicate the start of
an RTU frame, the ADU is preceded by a silent interval of at least 3.5 Byte times,
hence the length of that interval depends on the configuration of the devices in use.
To indicate the end of a frame, another silent interval of 3.5 Byte times succeeds the
ADU. Note that one single interval of silence can, at the same time, indicate the end
of one frame and the beginning of another frame. RTU frames use Cyclic Redundancy
Checks (CRC).
A complete RTU frame can be depicted as shown below:
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
12 CHAPTER 1 Introduction to emModbus
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
13
For referencing data, Modbus uses a concept of data tables, which are arrays or
blocks of memory used to store data. This data can then be referenced by using data
table addresses, represented by simple integer values between 0 and 65,535. While
it is fully standard-compliant to implement up to 65,536 addresses for each data
type, the number of addresses implemented in a particular device usually is much
lower. Therefore, Modbus implementations might even assign specific address ranges
of a single table to each type of data. While the Modbus standard itself does not
specify distinct address ranges, typical Modbus implementations utilize the following
assignments:
• 0xxxx-ranged addresses store coils.
• 1xxxx-ranged addresses store discrete inputs.
• 3xxxx-ranged addresses store input registers.
• 4xxxx-ranged addresses store holding registers.
Modbus uses a big-endian representation for data table addresses as well as for the
actual data itself. Therefore, the most significant byte is sent first when a numerical
quantity larger than a single byte is transmitted. For example
• (16-bits) 0x1234 gets send as 0x12 0x34 and
• (32-bits) 0x12345678 gets send as 0x12 0x34 0x56 0x78.
In addition to single bit data types (e.g. representing boolean values) and 16-bit data
types (e.g. representing integers), it is also possible to use large data types such as
long integers, floating point numbers and strings by splitting them over several
addresses. However, the Modbus standard does not stipulate this, hence it is up to
the individual user to split and store data accordingly.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
14 CHAPTER 1 Introduction to emModbus
1.2 emModbus
emModbus is written in ANSI C and can be used on virtually any CPU. It combines a
maximum of performance with a small memory footprint and comes with all features
typically required by embedded systems. RAM usage has been kept to a minimum by
smart buffer handling.
Directory Content
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
15
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
16 CHAPTER 1 Introduction to emModbus
Interrupt (ISR)
emModbus slave (ASCII / RTU)
Task
Routine / Action
Rx
MB_SLAVE_
Application tasks
Task
MB stack
Driver
MB_SLAVE_
MB_OnRx()
Exec()
App. App.
task 1 ... task n
Evaluat e Store
fram e byt e
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
17
Interrupt (ISR)
emModbus slave (ASCII / RTU)
Task
Routine / Action
Rx
Application tasks
MB stack
Driver
MB_SLAVE_
MB_OnRx()
Exec()
App. App.
task 1 ... task n
Evaluat e Store
fram e byt e
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
18 CHAPTER 1 Introduction to emModbus
Interrupt (ISR)
emModbus slave (TCP / UDP)
Task
Routine / Action
MB_SLAVE_ MB_SLAVE_
Application tasks
Task PollChannel
MB stack
Driver
MB_SLAVE_
Exec()
App. App.
task 1 ... task n
Evaluat e Read
fram e frame
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
19
Interrupt (ISR)
emModbus slave (TCP / UDP)
Task
Routine / Action
MB_SLAVE_
Application tasks
PollChannel()
MB stack
Driver
MB_SLAVE_
1.) Exec()
App. App.
task 1 ... task n
2.)
Evaluat e Read
fram e frame
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
20 CHAPTER 1 Introduction to emModbus
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
21
Chapter 2
Getting Started
The first step in getting started with emModbus is to compile it for and run it on the
target system. This chapter explains how to do this.
In this document the IAR Embedded Workbench® IDE is used for all examples and
screenshots, but every other ANSI C toolchain can be used as well. It is also possible
to use makefiles; in this case, “add to the project” translates into “add to the make-
file”.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
22 CHAPTER 2 Getting Started
2.1 Installation
emModbus is typically shipped as a .zip file in electronic form. In order to install
emModbus, extract it to any folder of your choice, preserving the directory structure
of the .zip file.
To create a running emModbus project, there are 3 different ways available:
• Upgrade a trial version by adding source code.
• Upgrade an embOS start project.
• Create a project from scratch.
The following example procedures describe each of these ways. They focus on inte-
grating an emModbus slave device using Modbus/TCP frames, but any other emMod-
bus project can be created as well by following the same steps.
emModbus via TCP is optimized to be used with embOS/IP, SEGGER’s TCP/IP stack.
However, emModbus can be used with any other TCP/IP stack as well. Note that when
using ASCII frames or RTU frames, the integration of a TCP/IP stack is not required
and should be omitted for smaller code size. Similarly, if no real-time operating sys-
tem is required, the integration of an RTOS should be omitted as well.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
23
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
24 CHAPTER 2 Getting Started
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
25
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
26 CHAPTER 2 Getting Started
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
27
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
28 CHAPTER 2 Getting Started
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
29
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
30 CHAPTER 2 Getting Started
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
31
Chapter 3
Example applications
In this chapter, you will find a description of the emModbus example applications that
are delivered together with the emModbus shipment.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
32 CHAPTER 3 Example applications
3.1 Overview
Example applications for emModbus are supplied in source code in the Application
folder. These can be used for testing the correct installation and proper function of
the device running emModbus.
The following start application files are provided:
File Description
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
33
3.1.1 OS_IP_MB_MasterTCP.c
This sample demonstrates emModbus master functionalities using the Modbus/TCP
protocol. It opens a channel and tries to establish a TCP connection to a Modbus
slave device, which is known to the master by the slave’s IP address as defined at the
beginning of the file. The master also uses a given port for this connection, which is
defined at the beginning of the file, too (e.g. port 502, the standard port for Modbus
communications). When a connection is established, the master repeatedly sends
queries to the slave, asking it to perform the function “write coil”, and waits for
appropiate responses.
3.1.2 OS_IP_MB_SlaveTCP.c
This sample demonstrates emModbus slave functionalities using the Modbus/TCP pro-
tocol. It opens a channel and waits for incoming TCP connections on a given port,
which is known to the slave as defined at the beginning of the file. When an incoming
connection from a Modbus master device has been established, the slave reacts to
queries it receives from the master. When ordered to write a coil (like the associated
Modbus master sample does), the slave will toggle LEDs to signal its new status.
3.1.3 OS_MB_MasterASCII.c
This emModbus sample demonstrates emModbus master functionalities using ASCII
frames. It opens a channel and repeatedly sends queries to a Modbus slave device
(specified by its slave ID as defined at the beginning of the file), asking it to perform
the function “write coil”, and waits for appropiate responses.
3.1.4 OS_MB_MasterRTU.c
This emModbus sample demonstrates emModbus master functionalities using RTU
frames. It opens a channel and repeatedly sends queries to a Modbus slave device
(specified by its slave ID as defined at the beginning of the file), asking it to perform
the function “write coil”, and waits for appropiate responses.
3.1.5 OS_MB_SlaveASCII.c
This sample demonstrates emModbus slave functionalities using ASCII frames. It
opens a channel and waits for incoming queries from a Modbus master device. When
ordered to write a coil (like the associated Modbus master sample does), the slave
will toggle LEDs to signal its new status.
3.1.6 OS_MB_SlaveRTU.c
This sample demonstrates emModbus slave functionalities using RTU frames. It opens
a channel and waits for incoming queries from a Modbus master device. When
ordered to write a coil (like the associated Modbus master sample does), the slave
will toggle LEDs to signal its new status.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
34 CHAPTER 3 Example applications
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
35
Chapter 4
Core functions
In this chapter, you will find a description of each emModbus core function.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
36 CHAPTER 4 Core functions
Function Description
Channel specific core functions
MB_CHANNEL_Disconnect() Disconnects a connected channel.
Master specific core functions
Adds a master channel that uses ASCII
MB_MASTER_AddASCIIChannel()
frames via serial connection.
Adds a master channel that uses TCP
MB_MASTER_AddIPChannel()
frames via network.
Adds a master channel that uses RTU
MB_MASTER_AddRTUChannel()
frames via serial connection.
De-Initializes master resources and
MB_MASTER_DeInit() channels and removes the master end-
point from the global endpoint list.
Initializes master resources and adds
MB_MASTER_Init()
master endpoint to global endpoint list.
Master instruction set
MB_MASTER_ReadCoils() Reads Coils from a slave.
MB_MASTER_ReadDI() Reads Discrete Inputs from a slave.
MB_MASTER_ReadHR() Reads Holding Registers from a slave.
MB_MASTER_ReadIR() Reads Input Registers from a slave.
MB_MASTER_WriteCoil() Writes a single coil to a slave.
MB_MASTER_WriteCoils() Writes multiple coils to a slave.
MB_MASTER_WriteReg() Writes a single register to a slave.
MB_MASTER_WriteRegs() Writes multiple registers to a slave.
Slave specific core functions
Adds a slave channel that uses ASCII
MB_SLAVE_AddASCIIChannel()
frames via serial connection.
Adds a slave channel that uses TCP
MB_SLAVE_AddIPChannel()
frames via network.
Adds a slave channel that uses RTU
MB_SLAVE_AddRTUChannel()
frames via serial connection.
De-Initializes the slave resources and
MB_SLAVE_DeInit() channels and removes the slave endpoint
from the global endpoint list.
Loops over all slave channels once and
MB_SLAVE_Exec() processes data when channel has been
signalled ready.
Initializes slave resources and adds slave
MB_SLAVE_Init()
endpoint to global endpoint list.
Polled periodically for each slave channel
MB_SLAVE_PollChannel() that requires requesting data instead of
getting it via interrupt.
Wrapper function that runs
MB_SLAVE_Task()
MB_SLAVE_Exec() in a task.
Other core functions
Function allowing to set a user defined
MB_ConfigTimerFreq() timer frequency instead of the default
frequency of 1kHz.
Table 4.1: emModbus API function overview
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
37
Function Description
Function called by byte oriented trans-
MB_OnRx() mission channels that receive an inter-
rupt for new data received.
Function called by byte oriented trans-
MB_OnTx() mission channels once a Tx complete
interrupt has been received.
Function called on each timer interrupt to
MB_TimerTick() manage internal RTU timeout with serial
channels using the RTU protocol.
Table 4.1: emModbus API function overview (Continued)
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
38 CHAPTER 4 Core functions
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL.
Table 4.2: MB_CHANNEL_Disconnect() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
39
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL that is added to linked list.
pConfig [IN] Pointer to element of MB_IFACE_CONFIG_UART.
[IN] Pointer to element of MB_IFACE_UART_API used to read/write
pIFaceAPI
from/to interface.
Timeout [IN] Timeout [in ms] to wait for answer.
SlaveAddr [IN] Slave address to access.
Baudrate [IN] Desired baudrate in UART protocol.
DataBits [IN] Number of data bits used in UART protocol.
Parity [IN] Parity used in UART protocol.
StopBits [IN] Number of stop bits used in UART protocol.
Port [IN] UART port number used by this channel.
Table 4.3: MB_MASTER_AddASCIIChannel() parameter list
Example
//
// Static declarations
//
static MB_CHANNEL _MBChannel;
static MB_IFACE_CONFIG_UART _MBConfig;
static const MB_IFACE_UART_API _IFaceAPI = {
_SendByte, _Init, _DeInit, NULL, NULL, NULL, NULL, NULL, NULL
};
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel(&_MBChannel, &_MBConfig, &_IFaceAPI, 3000, 1,
38400, 8, 0, 1, 0); // Add master channel
do { ... } // e.g. master/slave communications
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
40 CHAPTER 4 Core functions
4.1.2.2 MB_MASTER_AddIPChannel()
Description
Adds a master channel that uses TCP frames via network.
Prototype
void MB_MASTER_AddIPChannel ( MB_CHANNEL *pChannel,
MB_IFACE_CONFIG_IP *pConfig,
const MB_IFACE_IP_API *pIFaceAPI,
U32 Timeout,
U8 SlaveAddr,
U32 IPAddr,
U8 Port );
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL that is added to linked list.
pConfig [IN] Pointer to element of MB_IFACE_CONFIG_IP.
[IN] Pointer to element of MB_IFACE_IP_API used to read/write
pIFaceAPI
from/to interface.
Timeout [IN] Timeout [in ms] to wait for answer.
SlaveAddr [IN] Slave address to access.
IPAddr [IN] IP address of slave.
Port [IN] Slave port to connect to.
Table 4.4: MB_MASTER_AddIPChannel() parameter list
Example
//
// Static declarations
//
static MB_CHANNEL _MBChannel;
static MB_IFACE_CONFIG_UART _MBConfig;
static const MB_IFACE_IP_API _IFaceAPI = {
NULL, NULL, NULL, _Send, _Recv, _Connect, _Disconnect, NULL, NULL
};
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddIPChannel(&_MBChannel, &_MBConfig, &_IFaceAPI, 3000, 1,
IP_BYTES2ADDR(192,168,1,80), 502); // Add master channel
do { ... } // e.g. master/slave communications
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
41
4.1.2.3 MB_MASTER_AddRTUChannel()
Description
Adds a master channel that uses RTU frames via serial connection.
Prototype
void MB_MASTER_AddRTUChannel ( MB_CHANNEL *pChannel,
MB_IFACE_CONFIG_UART *pConfig,
const MB_IFACE_UART_API *pIFaceAPI,
U32 Timeout,
U8 SlaveAddr,
U32 Baudrate,
U8 DataBits,
U8 Parity,
U8 StopBits,
U8 Port );
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL that is added to linked list.
pConfig [IN] Pointer to element of MB_IFACE_CONFIG_UART.
[IN] Pointer to element of MB_IFACE_IP_API used to read/write
pIFaceAPI
from/to interface.
Timeout [IN] Timeout [in ms] to wait for answer.
SlaveAddr [IN] Slave address to access.
Baudrate [IN] Desired baudrate in UART protocol.
DataBits [IN] Number of data bits used in UART protocol.
Parity [IN] Parity used in UART protocol.
StopBits [IN] Number of stop bits used in UART protocol.
Port [IN] UART port number used by this channel.
Table 4.5: MB_MASTER_AddRTUChannel() parameter list
Example
//
// Static declarations
//
static MB_CHANNEL _MBChannel;
static MB_IFACE_CONFIG_UART _MBConfig;
static const MB_IFACE_UART_API _IFaceAPI = {
_SendByte, _Init, _DeInit, NULL, NULL, NULL, NULL, _InitTimer, _DeInitTimer
};
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddRTUChannel(&_MBChannel, &_MBConfig, &_IFaceAPI, 3000, 1,
38400, 8, 0, 1, 0); // Add master channel
do { ... } // e.g. master/slave communications
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
42 CHAPTER 4 Core functions
4.1.2.4 MB_MASTER_DeInit()
Description
De-Initializes the master resources and channels and removes the master endpoint
from the global endpoint list.
Prototype
void MB_MASTER_DeInit ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
43
4.1.2.5 MB_MASTER_Init()
Description
Initializes the master resources and adds the master endpoint to the global endpoint
list.
Prototype
void MB_MASTER_Init ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
44 CHAPTER 4 Core functions
Parameter
Parameter Description
pChannel [IN] Pointer to channel configured to interface a slave.
pData [OUT] Pointer to application buffer where to store the read data.
Addr [IN] Address in slave where to find the coils to access.
NumItems [IN] Number of items to read.
Table 4.6: MB_MASTER_ReadCoils() parameter list
Return value
<0: Error
0: OK
Example
//
// Static declarations
//
static int _result;
static MB_CHANNEL _MBChannel;
static U8 *_pData;
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel( ... ); // Add master channel
_result = MB_MASTER_ReadCoils(&_MBChannel, &_pData, 1000, 2);// Read Coils
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
45
4.1.3.2 MB_MASTER_ReadDI()
Description
Reads Discrete Inputs from a slave.
Prototype
int MB_MASTER_ReadDI ( MB_CHANNEL *pChannel,
U8 *pData,
U16 Addr,
U16 NumItems );
Parameter
Parameter Description
pChannel [IN] Pointer to channel configured to interface a slave.
pData [OUT] Pointer to application buffer where to store the read data.
Addr [IN] Address in slave where to find the inputs to access.
NumItems [IN] Number of items to read.
Table 4.7: MB_MASTER_ReadDI() parameter list
Return value
<0: Error
0: OK
Example
//
// Static declarations
//
static int _result;
static MB_CHANNEL _MBChannel;
static U8 *_pData;
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel( ... ); // Add master channel
_result = MB_MASTER_ReadDI(&_MBChannel, &_pData, 1000, 2); // Read Discrete Input
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
46 CHAPTER 4 Core functions
4.1.3.3 MB_MASTER_ReadHR()
Description
Reads Holding Registers from a slave.
Prototype
int MB_MASTER_ReadHR ( MB_CHANNEL *pChannel,
U8 *pData,
U16 Addr,
U16 NumItems );
Parameter
Parameter Description
pChannel [IN] Pointer to channel configured to interface a slave.
pData [OUT] Pointer to application buffer where to store the read data.
Addr [IN] Address in slave where to find the registers to access.
NumItems [IN] Number of items to read.
Table 4.8: MB_MASTER_ReadHR() parameter list
Return value
<0: Error
0: OK
Example
//
// Static declarations
//
static int _result;
static MB_CHANNEL _MBChannel;
static U8 *_pData;
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel( ... ); // Add master channel
_result = MB_MASTER_ReadHR(&_MBChannel, &_pData, 1000, 2);// Read Holding Register
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
47
4.1.3.4 MB_MASTER_ReadIR()
Description
Reads Input Registers from a slave.
Prototype
int MB_MASTER_ReadIR ( MB_CHANNEL *pChannel,
U8 *pData,
U16 Addr,
U16 NumItems );
Parameter
Parameter Description
pChannel [IN] Pointer to channel configured to interface a slave.
pData [OUT] Pointer to application buffer where to store the read data.
Addr [IN] Address in slave where to find the registers to access.
NumItems [IN] Number of items to read.
Table 4.9: MB_MASTER_ReadIR() parameter list
Return value
<0: Error
0: OK
Example
//
// Static declarations
//
static int _result;
static MB_CHANNEL _MBChannel;
static U8 *_pData;
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel( ... ); // Add master channel
_result = MB_MASTER_ReadIR(&_MBChannel, &_pData, 1000, 2);// Read Input Register
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
48 CHAPTER 4 Core functions
4.1.3.5 MB_MASTER_WriteCoil()
Description
Writes a single coil to a slave.
Prototype
int MB_MASTER_WriteCoil ( MB_CHANNEL *pChannel,
U16 Addr,
U8 OnOff );
Parameter
Parameter Description
pChannel [IN] Pointer to channel configured to interface a slave.
Addr [IN] Address in slave where to find the coil to access.
OnOff [IN] Write Coil to 0: Off or 1: On.
Table 4.10: MB_MASTER_WriteCoil() parameter list
Return value
<0: Error
0: OK
Example
//
// Static declarations
//
static int _result;
static MB_CHANNEL _MBChannel;
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel( ... ); // Add master channel
_result = MB_MASTER_WriteCoil(&_MBChannel, 1000, 1); // Write Coil
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
49
4.1.3.6 MB_MASTER_WriteCoils()
Description
Writes multiple coils to a slave
Prototype
int MB_MASTER_WriteCoils ( MB_CHANNEL *pChannel,
U8 *pData,
U16 Addr,
U16 NumItems );
Parameter
Parameter Description
pChannel [IN] Pointer to channel configured to interface a slave.
[IN] Pointer to application buffer where the values to write are
pData
stored.
Addr [IN] Address in slave where to find the coils to access.
NumItems [IN] Number of items to write.
Table 4.11: MB_MASTER_WriteCoils() parameter list
Return value
<0: Error
0: OK
Example
//
// Static declarations
//
static int _result;
static MB_CHANNEL _MBChannel;
static U8 _data;
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel( ... ); // Add master channel
_result = MB_MASTER_WriteCoils(&_MBChannel, &_data, 1000, 2);// Write Coils
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
50 CHAPTER 4 Core functions
4.1.3.7 MB_MASTER_WriteReg()
Description
Writes a single register to a slave.
Prototype
int MB_MASTER_WriteReg ( MB_CHANNEL *pChannel,
U16 Data,
U16 Addr );
Parameter
Parameter Description
pChannel [IN] Pointer to channel configured to interface a slave.
Data [IN] 16-bit data to write to register.
Addr [IN] Address in slave where to find the register to access.
Table 4.12: MB_MASTER_WriteReg() parameter list
Return value
<0: Error
0: OK
Example
//
// Static declarations
//
static int _result;
static MB_CHANNEL _MBChannel;
static U16 _data;
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel( ... ); // Add master channel
_result = MB_MASTER_WriteReg(&_MBChannel, _data, 1000); // Write Register
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
51
4.1.3.8 MB_MASTER_WriteRegs()
Description
Writes multiple registers to a slave.
Prototype
int MB_MASTER_WriteRegs ( MB_CHANNEL *pChannel,
U16 *pData,
U16 Addr,
U16 NumItems );
Parameter
Parameter Description
pChannel [IN] Pointer to channel configured to interface a slave.
[IN] Pointer to application buffer where the values to write are
pData
stored.
Addr [IN] Address in slave where to find the registers to access.
NumItems [IN] Number of items to write.
Table 4.13: MB_MASTER_WriteRegs() parameter list
Return value
<0: Error
0: OK
Example
//
// Static declarations
//
static volatile int _result;
static MB_CHANNEL _MBChannel;
static U16 _data;
//
// Code running in its own task
//
static void _MasterTask(void) {
MB_MASTER_Init(); // Init master
MB_MASTER_AddASCIIChannel( ... ); // Add master channel
_result = MB_MASTER_WriteRegs(&_MBChannel, &_data, 1000, 2);// Write Registers
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
52 CHAPTER 4 Core functions
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL that is added to linked list.
pConfig [IN] Pointer to element of MB_IFACE_CONFIG_UART.
[IN] Pointer to elemtent of MB_SLAVE_API used to read/write
pSlaveAPI
from/to target.
[IN] Pointer to element of MB_IFACE_UART_API used to read/write
pIFaceAPI
from/to interface.
SlaveAddr [IN] Slave address to listen on.
DisableWrite [IN] Disable write access on this channel.
Baudrate [IN] Desired baudrate in UART protocol.
DataBits [IN] Number of data bits used in UART protocol.
Parity [IN] Parity used in UART protocol.
StopBits [IN] Number of stop bits used in UART protocol.
Port [IN] UART port number used by this channel.
Table 4.14: MB_SLAVE_AddASCIIChannel() parameter list
Example
//
// Static declarations
//
static MB_CHANNEL _MBChannel;
static MB_IFACE_CONFIG_UART _MBConfig;
static const MB_IFACE_UART_API _IFaceAPI = {
_SendByte, _Init, _DeInit, NULL, NULL, NULL, NULL, NULL, NULL
};
static const MB_SLAVE_API _SlaveAPI = {
_WriteCoil, _ReadCoil, _ReadDI, _WriteReg, _ReadHR, _ReadIR
};
//
// Code running in main task
//
void MainTask(void) {
MB_SLAVE_Init(); // Init slave
MB_SLAVE_AddASCIIChannel(&_MBChannel, &_MBConfig, &_SlaveAPI, &_IFaceAPI, 1, 0,
38400, 8, 0, 1, 0); // Add slave channel
OS_CREATETASK( ... ); // Start slave task
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
53
4.1.4.2 MB_SLAVE_AddIPChannel()
Description
Adds a slave channel that uses TCP frames via network.
Prototype
void MB_SLAVE_AddIPChannel ( MB_CHANNEL *pChannel,
MB_IFACE_CONFIG_IP *pConfig,
const MB_IFACE_IP_API *pIFaceAPI,
U8 SlaveAddr,
U32 DisableWrite,
U32 IPAddr,
U8 Port );
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL that is added to linked list.
pConfig [IN] Pointer to element of MB_IFACE_CONFIG_IP.
[IN] Pointer to elemtent of MB_SLAVE_API used to read/write
pSlaveAPI
from/to target.
[IN] Pointer to element of MB_IFACE_IP_API used to read/write
pIFaceAPI
from/to interface.
SlaveAddr [IN] Slave address to access.
DisableWrite [IN] Disable write access on this channel.
[IN] Filter address. If set, only connections on this address
IPAddr
should be accepted.
Port [IN] Port that accepts connections for this channel.
Table 4.15: MB_SLAVE_AddIPChannel() parameter list
Example
//
// Static declarations
//
static MB_CHANNEL _MBChannel;
static MB_IFACE_CONFIG_UART _MBConfig;
static const MB_IFACE_IP_API _IFaceAPI = {
NULL, _Init, _DeInit, _Send, _Recv, _Connect, _Disconnect, NULL, NULL
};
static const MB_SLAVE_API _SlaveAPI = {
_WriteCoil, _ReadCoil, _ReadDI, _WriteReg, _ReadHR, _ReadIR
};
//
// Code running in main task
//
void MainTask(void) {
MB_SLAVE_Init(); // Init slave
OS_CREATETASK( ... ); // Start slave task
MB_SLAVE_AddIPChannel(&_MBChannel, &_MBConfig, &_SlaveAPI, &_IFaceAPI, 1, 0,
0, 502); // Add slave channel
OS_CREATETASK( ... ); // Start polling task for this channel
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
54 CHAPTER 4 Core functions
4.1.4.3 MB_SLAVE_AddRTUChannel()
Description
Adds a slave channel that uses RTU frames via serial connection.
Prototype
void MB_SLAVE_AddASCIIChannel ( MB_CHANNEL *pChannel,
MB_IFACE_CONFIG_UART *pConfig,
const MB_SLAVE_API *pSlaveAPI,
const MB_IFACE_UART_API *pIFaceAPI,
U8 SlaveAddr,
U8 DisableWrite,
U32 Baudrate,
U8 DataBits,
U8 Parity,
U8 StopBits,
U8 Port );
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL that is added to linked list.
pConfig [IN] Pointer to element of MB_IFACE_CONFIG_UART.
[IN] Pointer to elemtent of MB_SLAVE_API used to read/write
pSlaveAPI
from/to target.
[IN] Pointer to element of MB_IFACE_UART_API used to read/write
pIFaceAPI
from/to interface.
SlaveAddr [IN] Slave address to listen on.
DisableWrite [IN] Disable write access on this channel.
Baudrate [IN] Desired baudrate in UART protocol.
DataBits [IN] Number of data bits used in UART protocol.
Parity [IN] Parity used in UART protocol.
StopBits [IN] Number of stop bits used in UART protocol.
Port [IN] UART port number used by this channel.
Table 4.16: MB_SLAVE_AddRTUChannel() parameter list
Example
//
// Static declarations
//
static MB_CHANNEL _MBChannel;
static MB_IFACE_CONFIG_UART _MBConfig;
static const MB_IFACE_UART_API _IFaceAPI = {
_SendByte, _Init, _DeInit, NULL, NULL, NULL, NULL, _InitTimer, _DeInitTimer
};
static const MB_SLAVE_API _SlaveAPI = {
_WriteCoil, _ReadCoil, _ReadDI, _WriteReg, _ReadHR, _ReadIR
};
//
// Code running in main task
//
void MainTask(void) {
MB_SLAVE_Init(); // Init slave
MB_SLAVE_AddRTUChannel(&_MBChannel, &_MBConfig, &_SlaveAPI, &_IFaceAPI, 1, 0,
38400, 8, 0, 1, 0); // Add slave channel
OS_CREATETASK( ... ); // Start slave task}
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
55
4.1.4.4 MB_SLAVE_DeInit()
Description
De-Initializes the slave resources and channels and removes the slave endpoint from
the global endpoint list.
Prototype
void MB_SLAVE_DeInit ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
56 CHAPTER 4 Core functions
4.1.4.5 MB_SLAVE_Exec()
Description
Loops once over all slave channels, processes data when the channel has been sig-
nalled ready.
Prototype
void MB_SLAVE_Exec ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
57
4.1.4.6 MB_SLAVE_Init()
Description
Initializes the slave resources and adds the slave endpoint to the global endpoint list.
Prototype
void MB_SLAVE_Init ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
58 CHAPTER 4 Core functions
4.1.4.7 MB_SLAVE_PollChannel()
Description
Function that has to be periodically polled for each slave channel that requires
requesting data instead of getting it via interrupt.
Prototype
void MB_SLAVE_PollChannel ( MB_Channel *pChannel );
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL.
Table 4.17: MB_SLAVE_PollChannel() parameter list
Return value
<0: Error
0: No complete Modbus message signaled.
1: Complete Modbus message signaled.
Example
//
// Polling a slave channel in a task, allowing it to sleep when possible.
//
static void _PollSlaveChannelTask(void *pChannel) {
while (1) {
MB_SLAVE_PollChannel((MB_CHANNEL*)pChannel);
}
}
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
59
4.1.4.8 MB_SLAVE_Task()
Description
Wrapper function that runs MB_SLAVE_Exec() in a task.
Prototype
void MB_SLAVE_Task ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
60 CHAPTER 4 Core functions
Parameter
Parameter Description
[IN] Timer frequency that shall be used for all all channels to cal-
Freq
culate the RTU timeout.
Table 4.18: MB_ConfigTimerFreq() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
61
4.1.5.2 MB_OnRx()
Description
Function called by byte oriented transmission channels that receive an interrupt for
new data received.
Prototype
void MB_OnRx ( MB_CHANNEL *pChannel,
U8 Data );
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL.
Data [IN] Received character.
Table 4.19: MB_OnRx() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
62 CHAPTER 4 Core functions
4.1.5.3 MB_OnTx()
Description
Function called by byte oriented transmission channels once a Tx complete interrupt
has been received to send the next byte or report back that there is no more to send.
Prototype
int MB_OnTx ( MB_CHANNEL *pChannel );
Parameter
Parameter Description
pChannel [IN] Pointer to element of MB_CHANNEL.
Table 4.20: MB_OnTx() parameter list
Return value
<0: Error
0: More data sent
1: No more data to send
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
63
4.1.5.4 MB_TimerTick()
Description
Function called on each timer interrupt to manage internal RTU timeout with serial
channels using the RTU protocol.
Prototype
void MB_TimerTick ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
64 CHAPTER 4 Core functions
Member Description
Sock Socket used for send and receive.
ListenSock Socket used by TCP for listen() and accept(). Not needed for UDP.
Master: Addr. to connect to.
IPAddr Slave: Filter address. If set only connections on this address should
be accepted.
Master: Port to connect to.
Port
Slave: Port that accepts connections for this channel.
Master: Transaction ID that is incremented for each send.
xID
Slave: Ignored.
Table 4.21: Structure MB_IFACE_CONFIG_IP member list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
65
Member Description
Cnt RTU timeout countdown.
CntReload RTU countdown reload value.
Baudrate Baudrate to use.
DataBits Number of data bits.
Parity Parity as interpreted by application.
StopBits Number of stop bits.
Port Interface index.
Table 4.22: Structure MB_IFACE_CONFIG_UART member list
Additional information
MB_IFACE_CONFIG is of type MB_IFACE_CONFIG_UART.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
66 CHAPTER 4 Core functions
Member Description
Send first byte. Every next byte will be sent via MB_OnTx() from
pfSendByte interrupt.
NULL if stream oriented interface, as pfSendByte gets used instead.
Init IP and get listen socket and bring it in listen state if needed.
pfInit
NULL if not needed.
Close listen socket and de-init IP.
pfDeInit
NULL if not needed.
Send data for stream oriented interface.
pfSend
NULL if byte oriented interface is used, as pfSend gets used instead.
pfRecv Request more data.
Master: Connect to slave.
pfConnect Slave: Accept connection if needed.
NULL if not needed.
Master: Disconnect from slave.
pfDisconnect Slave: Close connection if needed.
NULL if not needed.
pfInitTimer NULL.
pfDeInitTimer NULL.
Table 4.23: Structure MB_IFACE_IP_API member list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
67
Member Description
Send first byte. Every next byte will be sent via MB_OnTx() from
pfSendByte interrupt.
NULL if stream oriented interface, as pfSend gets used instead.
Init hardware.
pfInit
NULL if not needed.
De-Init hardware.
pfDeInit
NULL if not needed.
Send data for stream oriented interface.
pfSend
NULL if byte oriented interface, as pfSendByte gets used instead.
Typically data is received via MB_OnRx() from interrupt.
pfRecv
NULL if not using polling mode.
pfConnect NULL.
pfDisconnect NULL.
Typically needed for RTU interfaces only. Initializes a timer needed
pfInitTimer for RTU timeout.
NULL if not needed.
De-initialize RTU timer.
pfDeInitTimer
NULL if not needed.
Table 4.24: Structure MB_IFACE_UART_API member list
Additional information
MB_IFACE_API is of type MB_IFACE_UART_API.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
68 CHAPTER 4 Core functions
Member Description
pfReadCoil Read coil status.
pfReadDI Read discrete input registers.
pfReadHR Read holding register.
pfReadIR Read input register.
pfWriteCoil Write coil.
pfWriteReg Write register.
Table 4.25: Structure MB_SLAVE_API member list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
69
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
70 CHAPTER 4 Core functions
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
71
Chapter 5
Configuring emModbus
emModbus can be used without changing any of the compile-time flags. All compile-
time configuration flags are preconfigured with valid values, which match the
requirements of most applications.
The default configuration of emModbus can be changed via compile-time flags which
can be added to MB_Conf.h. MB_Conf.h is the main configuration file for the emMod-
bus stack.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
72 CHAPTER 5 Configuring emModbus
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
73
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
74 CHAPTER 5 Configuring emModbus
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
75
Chapter 6
Debugging
emModbus comes with debugging options including optional warning and log outputs.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
76 CHAPTER 6 Debugging
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
77
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
78 CHAPTER 6 Debugging
6.1.1.1 MB_Log()
Description
This function is called by the stack in debug builds with log output. In a release build,
this function may not be linked in.
Prototype
void MB_Log ( const char *s );
Parameter
Parameter Description
s [IN] String to output.
Table 6.2: MB_Log() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
79
6.1.1.2 MB_Panic()
Description
This function is called if the stack encounters a critical situation. In a release build,
this function may not be linked in.
Prototype
void MB_Panic ( const char *s );
Parameter
Parameter Description
s [IN] String to output before running into endless loop.
Table 6.3: MB_Panic() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
80 CHAPTER 6 Debugging
6.1.1.3 MB_Warn()
Description
This function is called by the stack in debug builds with warning output. In a release
build, this function may not be linked in.
Prototype
void MB_Warn ( const char *s );
Parameter
Parameter Description
s [IN] String to output.
Table 6.4: MB_Warn() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
81
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
82 CHAPTER 6 Debugging
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
83
Chapter 7
OS Integration
emModbus is designed to be used in a multitasking environment. The interface to the
operating system is encapsulated in a single file, the MB/OS interface. For embOS, all
functions required for this MB/OS interface are implemented in a single file which
comes with emModbus.
This chapter provides descriptions of the functions required to fully support emMod-
bus in multitasking environments.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
84 CHAPTER 7 OS Integration
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
85
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
86 CHAPTER 7 OS Integration
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
87
7.2.1.2 MB_X_OS_DeInitSlave()
Description
De-Initializes (removes) all objects required for task syncronisation of the slave.
Prototype
void MB_X_OS_DeInitSlave ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
88 CHAPTER 7 OS Integration
7.2.1.3 MB_X_OS_DisableInterrupt()
Description
Disables interrupts before critical operations.
Prototype
void MB_X_OS_DisableInterrupt ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
89
7.2.1.4 MB_X_OS_EnableInterrupt()
Description
Enables interrupts after critical operations.
Prototype
void MB_X_OS_EnableInterrupt ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
90 CHAPTER 7 OS Integration
7.2.1.5 MB_X_OS_GetTime()
Description
Returns the current system time in ms. The value will wrap around after approxi-
mately 49.7 days. This is taken into account by the stack.
Prototype
U32 MB_X_OS_GetTime32 ( void );
Return value
System time in ms.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
91
7.2.1.6 MB_X_OS_InitMaster()
Description
Initializes (creates) all objects required for task syncronisation of the master. This is
one semaphore for protection of critical code, which may not be executed from multi-
ple tasks at the same time, and a hook in case a task currently executing Modbus
master API is terminated.
Prototype
void MB_X_OS_InitMaster ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
92 CHAPTER 7 OS Integration
7.2.1.7 MB_X_OS_InitSlave()
Description
Initializes (creates) all objects required for task synchronisation and signalling of the
slave.
Prototype
void MB_X_OS_InitSlave ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
93
Parameter
Parameter Description
pWaitItem [IN] Pointer to item a task is waiting for.
Table 7.2: MB_X_OS_SignalItem() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
94 CHAPTER 7 OS Integration
7.2.2.2 MB_X_OS_SignalNetEvent()
Description
Wakes tasks waiting for a NET-event or timeout in the function
MB_OS_WaitNetEvent().
Prototype
void MB_X_OS_SignalNetEvent ( void );
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
95
7.2.2.3 MB_X_OS_WaitItemTimed()
Description
Suspends a task which needs to wait for an object. This object is identified by a
pointer to it and can be of any type, e.g. channel.
Prototype
void MB_X_OS_WaitItemTimed ( void *pWaitItem,
unsigned Timeout );
Parameter
Parameter Description
[IN] Pointer to item a task shall wait for until signalled or timeout
pWaitItem
occurs.
Timeout [IN] Timeout [in ms] to wait for item to be signalled.
Table 7.3: MB_X_OS_WaitItemTimed() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
96 CHAPTER 7 OS Integration
7.2.2.4 MB_X_OS_WaitNetEvent()
Description
Called from MB_Task() only. Blocks until a NET-event occurs, meaning
MB_OS_SignalNetEvent() is called from another task or ISR.
Prototype
void MB_X_OS_WaitNetEvent ( unsigned ms );
Parameter
Parameter Description
ms [IN] Time to wait for a NET-event to occur [in ms]. 0 for infinite.
Table 7.4: MB_X_OS_WaitNetEvent() parameter list
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
97
Chapter 8
Resource usage
This chapter covers the resource usage of emModbus. It contains information about
the memory requirements in typical systems, which can be used to obtain sufficient
estimates for most target systems.
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
98 CHAPTER 8 Resource usage
Detail Description
CPU ARM7
Tool chain IAR Embedded Workbench for ARM V6.30.6
Model ARM7, Thumb instructions; interwork;
Compiler options Highest size optimization;
Table 8.1: ARM7 sample configuration
Description ROM
master using ASCII approx. 1.5 Kbytes
master using TCP approx. 0.9 Kbytes
master using RTU approx. 2.1 Kbytes
slave using ASCII approx. 2.0 Kbytes
slave using TCP approx. 1.6 Kbytes
slave using RTU approx. 2.6 Kbytes
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
Index 99
P
Index Port number (Modbus/TCP) .................. 10
Protocol Data Unit (PDU) ...................... 11
Protocol ID (Modbus/TCP) .................... 12
Q
Query ................................................ 10
S
A Schneider Electric SA ........................... 10
Application Data Unit (ADU) ..................11 SEGGER Microcontroller GmbH & Co. KG .. 6
Slave (device) .................................... 10
C Syntax, conventions used ....................... 5
Coil ....................................................13
Compiler, required compliance ...............15 T
Compile-time flags ...............................71 TCP/IP stack ....................................... 14
Configuration ......................................71 Transaction ID (Modbus/TCP) ................ 12
Contact address ................................... 2
Core functions .....................................36
Cyclic Redundancy Check ......................11 U
Unit ID .............................................. 11
D
Data table ..........................................13
Data types, primary .............................13
Debug functions ..................................77
Discrete Input .....................................13
E
Endianess ...........................................13
F
Frames, Modbus-compliant variants .......10
Function code ......................................11
Function replacements .........................72
H
Holding Register ..................................13
I
Input Register .....................................13
L
Log output ..........................................78
Longitudinal Redundancy Check .............12
M
Master (device) ...................................10
Memory requirement ............................97
Modbus Application Header (Modbus/TCP) 12
Modbus Organization ............................10
Modbus, standard protocol ....................10
Modicon .............................................10
Multi tasking .......................................14
N
Network sniffer ....................................81
O
OS integration functions .......................85
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG
100 Index
UM14001 User & Reference Guide for emModbus © 2014 SEGGER Microcontroller GmbH & Co. KG