You are on page 1of 10

Application Note

MODBUS RTU Master/Slave for XC Controls

03/04 AN2700K24G V1.0 All brand and product names are trademarks or
© Moeller GmbH, Bonn registered trademarks of the owner concerned.
Autor: O. Weiß
All rights reserved, including those of the translation.
No part of this application note may be reproduced in
any form (printed, photocopy, microfilm or any other
process) or processed, duplicated or distributed by
means of electronic systems without the expressed
written permission of Moeller GmbH, Bonn.
Status: April 2001
Subject to modification
MODBUS RTU Master/Slave for XC Controls

Contents

1 General points................................................................................................................................................................... 3
1.1 Function ......................................................................................................................................................................... 3
1.2 Field of application ......................................................................................................................................................... 3
1.3 Hardware requirements and possible COM ports.......................................................................................................... 4
1.4 Software requirements................................................................................................................................................... 4

2 Design............................................................................................................................................................................... 4
2.1 Structure ........................................................................................................................................................................ 4
2.2 Functions blocks and parameters.................................................................................................................................. 5
2.2.1 MODBUS Master ........................................................................................................................................................ 5
2.2.2 MODBUS Slave .......................................................................................................................................................... 6

3 Commissioning ................................................................................................................................................................. 7
3.1 Integration of the MODBUS connection......................................................................................................................... 7
3.2 Method of function and operation of the MODBUS connection ..................................................................................... 7
3.2.1 MODBUS master ........................................................................................................................................................ 7
3.2.2 MODBUS slave........................................................................................................................................................... 8
3.3 Exception codes............................................................................................................................................................. 9
3.4 Examples ....................................................................................................................................................................... 9

4 Annex .............................................................................................................................................................................. 10
4.1 Connector and connections of the controllers ............................................................................................................. 10
4.1.1 XC100 programming port ......................................................................................................................................... 10
4.1.2 XIOC-SER................................................................................................................................................................. 10
4.1.3 COM port of XVC100, HPG and XC600 ................................................................................................................... 10

Moeller GmbH AN2700K24G 2


MODBUS RTU Master/Slave for XC Controls

1 General points

The MODBUS is a centrally polled bus system where the master controls the entire data transfer on the bus and where
no cross transfer between the slave stations is possible. Each exchange of data is initiated by the master on request. A
slave cannot initiate data transfer as it can only respond on demand.
For the development of the function block, the definitions of the MODBUS Protocol Reference Guide, PI-MBUS-300 Rev.
B, Issue January 1985, Gould Inc. Andover, Massachusetts, USA have been considered.

1.1 Function

The application note provides the MODBUS-RTU master and slave interface for all controls of the X series with the
following options.

Function Codes:

1 : Read multiple coil status - Reading of bit variables (Coils)


*)
2 : Read multiple input status – Reading of bit variables (“Inputs”)
3 : Read multiple holding registers – Reading of word variables (Register)
*)
4 : Read multiple input registers – Reading of word variables (“Inputs”)
5 : Force single coil – Writing of a bit variable
6 : Force single register – Writing of a word variable
8 : Loop back diagnostic test (00: return query data) – Connection test
15 : Force multiple coils – Writing multiple bit variables (Coils)
16 : Force multiple registers – Writing multiple word variables (Register)

*)
The “Inputs” are realised as Coils and Holding registers via data fields. The functions do not directly access
physical inputs.

Master:

Max. 120 (90 with XC100 via PRG-Port) Registers / 1200 Coils per job

Slave:

Freely configurable number of Modbus data via global variable field. With every message a max. of 120 (90 with XC100
via PRG-Port) Registers / 1200 Coils can be exchanged.

The hardware interface used with the MODBUS is COM1 (PRG-Port) or depending on the control type the XIOC-SER
serial module. No additional software is required here as the MODBUS function block also supports the required
functions of the COM interface.
The application note also supports broadcast access for the function codes 05, 06, 15 and 16 with master and with slave
implementation. The MODBUS master addresses the MODBUS slave “0” for this purpose.

1.2 Field of application

The application note is intended for use where the connection to a PLC system or a PLC via MODBUS RTU is required.

Note: The reaction and response times are dependant on the PLC cycle time. It is very important to take this factor into
consideration with the timing demands involved with MODBUS interfacing.

Moeller GmbH AN2700K24G 3


MODBUS RTU Master/Slave for XC Controls

1.3 Hardware requirements and possible COM ports

XC100/XV100 from HW02: COM1 (PRG-Port)


COM2,3 (XIOC-SER)
XC200/XV200 only via XIOC-SER: COM2, 3, 4, 5 (XIOC-SER)
XVC100 COM1 (PRG-Port)
HPG COM1
XC600 (RTS ab V2.3): COM1

1.4 Software requirements

XSoft from V2.3 (from V2.3.2.6 for XC200), for XIOC-SER related targets

2 Design

2.1 Structure

The ModbusMaster and ModbusSlave function blocks are now provided in the libraries with the same name:

ModbusMaster.Lib
ModbusSlave.Lib

Note: The library must be imported from the respective control-specific subdirectory.

If MODBUS slave interfacing is to be implemented, the MODBUSSLAVE_DATASIZE.EXP export file must also be
imported. The Modbus data fields of the slave are dimensioned here.

Moeller GmbH AN2700K24G 4


MODBUS RTU Master/Slave for XC Controls

2.2 Functions blocks and parameters

2.2.1 MODBUS Master

ModbusMaster

BOOL xEnable xComPortActiv BOOL


USINT usiComPort
UINT uiBaudrate xMODtransActive BOOL
USINT usiParity xMODtransOk BOOL
USINT usiStopbits usiMODslaveAdrDone USINT
usiMODfcDone USINT
USINT usiMODSlaveAdr uiMODdatOffDone UINT
USINT usiMODfc uiMODdatNrDone UINT
UINT uiMODdatOff usiMODfailCode USINT
UINT uiMODdatNr
TIME tMODtimeOut
BOOL xMODfcStrobe

ARRAY[1..120] OF WORD warMODRegister


ARRAY[1..1200] OF BOOL xarMODCoil

VAR_INPUT
xEnable Enable of the MODBUS connection
usiComPort COM-Port: XC100->1,2,3; XC200->2,3,4,5; XVC100/HPG/XC600->1
uiBaudrate Baud rate of the COM port (4800, 9600, 19200, 38400, 57600)
usiParity Character parity format: none -> 0, odd -> 1, even -> 2
usiStopBits Stop bit character format: 1, 2
usiMODSlaveAdr Address of the MODBUS slaves to be accessed
usiMODfc Function Code: 1, 2, 3, 4, 5, 6, 8, 15, 16 provided they are supported by the
slave
uiMODdatOff Data offset in the slave for the data to be transferred
uiMODdatNr Number of data packages for transfer (maximum is the possible telegram
length)
tMODtimeOut Timeout for slave response
xMODfcStrobe Start (FC) job

VAR_IN_OUT
warMODRegister MODBUS data: Buffer for receive or send register (Holding/Input)
xarMODCoil MODBUS data: Buffer for receive or send BOOL variables (Coils/Inputs)

VAR_OUTPUT
xComPortActiv COM port has been successfully opened, MODBUS connection established
xMODtransActive MODBUS job active
xMODtransOk MODBUS job successfully completed
usiMODslaveAdrDone Slave last contacted
usiMODfcDone Function code of the last executed job
uiMODdatOffDone Slave data offset for the last job executed
uiMODdatNrDone Number of MODBUS data packages sent with the last job
usiMODfailCode Exception code of the last job executed

Moeller GmbH AN2700K24G 5


MODBUS RTU Master/Slave for XC Controls

2.2.2 MODBUS Slave

ModbusSlave

BOOL xEnable xComPortActiv BOOL


USINT usiComPort xFrameReceived BOOL
UINT uiBaudrate
USINT usiParity usiMODfc USINT
USINT usiStopbits uiMODdatOff UINT
uiMODdatNr UINT
USINT usiMODSlaveAdr usiMODfailCode USINT

*)
ARRAY[1..a ] OF WORD warHoldingRegister
*)
ARRAY[1..b ] OF WORD warInputRegister
*)
ARRAY [1..c ] OF BOOL xarCoil
*)
ARRAY [1..d ] OF BOOL xarInput

*)
Maximum number of MODBUS data packages, configured in the MODBUSSLAVE_DATASIZE.EXP global variable
list to be imported:
a: ModbusSlave_uiMaxHoldingRegister :UINT
b: ModbusSlave_uiMaxInputRegister : UINT
c: ModbusSlave_uiMaxCoil : UINT
d: ModbusSlave_uiMaxInput : UINT

VAR_INPUT
xEnable Enable of the MODBUS connection
usiComPort COM-Port: XC100->1,2,3; XC200->2,3,4,5; XVC100_HPG_XC600->1
uiBaudrate Baud rate of the COM port (4800, 9600, 19200, 38400, 57600)
usiParity Character parity format: none -> 0, odd -> 1, even -> 2
usiStopBits Stop bit character format: 1, 2
usiMODSlaveAdr Address of the MODBUS slaves

VAR_IN_OUT
warHoldingRegister MODBUS data: Holding register
warInputRegister MODBUS data: Input register
xarCoil MODBUS data: Coils
xarInput MODBUS data: Inputs

VAR_OUTPUT
xComPortActiv COM port has been successfully opened, MODBUS connection enabled
xFrameReceived MODBUS job received (pulse)
usiMODfc Function code of the last executed job
uiMODdatOff Data offset for the last job executed
uiMODdatNr Number of MODBUS data packages to be sent with the last job
usiMODfailCode Exception code of the last job

VAR__GLOBAL Constants in the MODBUSSLAVE_DATASIZE.EXP export file:


CONSTANT Dimension of the data fields of the slave
ModbusSlave_ Number of the MODBUS holding register of the slave
uiMaxHoldingRegister Default: 100, field (FC) is not used: 1

Moeller GmbH AN2700K24G 6


MODBUS RTU Master/Slave for XC Controls

ModbusSlave_ Number of the MODBUS input register of the slave


uiMaxInputRegister Default: 100, field (FC) is not used: 1
ModbusSlave_ Number of the MODBUS Coils of the slave
uiMaxCoil Default: 100, field (FC) is not used: 1
ModbusSlave_ Number of the MODBUS inputs of the slave
uiMaxInput Default: 100, field (FC) is not used: 1

3 Commissioning

3.1 Integration of the MODBUS connection

For the master connection, the ModbusMaster.Lib library must be imported from the respective control-specific
subdirectory and the FB ModbusMaster function block of the same name is to be accessed and parameterized.

For the slave connection the ModbusSlave.Lib library must be imported from the respective control-specific
subdirectory and the FB ModbusSlave function block of the same name is to be accessed and parameterized.
Furthermore, the MODBUSSLAVE_DATASIZE.EXP export file must also be imported for connection of the slave. In the
project, the ModbusSlave_DataSize global variable list is created with the import. The sizes of the required MODBUS
data field should be entered as constants in the list. The fields are sized by default for 100 elements. If a field is not
required, the constant 1 should be entered in order to reduce unnecessary loading on the data memory.
The slave only provides the master with the elements configured here !

Note:
The Internal Functions which are also available in the library are used exclusively for internal purposes and should not be
accessed by the user !
Only a single instance is permitted per COM port, regardless of it is a slave or master !

The control-specific COM library is automatically integrated during import of the libraries. These are required for
actuation of the serial interface.

The MODBUS function block should be executed cyclically in order to avoid any possible time-outs with the
communication parameters.

3.2 Method of function and operation of the MODBUS connection

The parameterized COM port (input usiComPort) is opened with a rising edge on xEnable and is initialised with the
uiBaudrate, usiParity and usiStopbits parameters. The data capacity is fixed at 8 bits (RTU Binary). The serial interface
remains open as long as the xEnable signal remains high. The COM Port closes again when the signal goes low.
If the signal was established via the programming port, the serial interface is only available again to the programming
device after the COM Port has been closed. Open COM ports are also automatically closed when the operating mode of
the control changes from RUN to STOP.

A High signal on the xComPortActiv output signals successful opening of the COM Port.
The MODBUS telegram recognition occurs automatically.

3.2.1 MODBUS master

Every data transfer on the bus is initiated and controlled by the master.
A high level on the xMODfcStrobe input starts the job whose function is determined by the function code entered at the
usiMODfc input. Communication with the MODBUS station whose address has been parameterized on the
usiMODslaveAdr input is undertaken.
The MODBUS input parameters are interpreted differently and depend on the function code:

Moeller GmbH AN2700K24G 7


MODBUS RTU Master/Slave for XC Controls

uiMODdatOff uiMODdatNr warMODRegister xarMODCoil


FC 1 Coil source offset in slave Number of requested Coil target buffer in
---
coils master
FC 2 “Input” source offset in Number of requested --- “Input” target buffer in
slave “inputs” master
FC 3 Holding register source Number of requested Holding register target
---
offset in slave registers buffer in master
FC 4 “Input” register source Number of requested Input register target
---
offset in slave registers buffer in master
FC 5 Coil target offset in slave --- --- Coil value in index [1]
FC 6 Holding register target Register value in
--- ---
offset in slave index [1]
FC 8 --- --- Test value in index [1] ---
FC 15 Coil target offset in slave Number of coils to be Coil source buffer in
---
sent master
FC 16 Holding register target Number of registers to Holding register
offset in slave be sent source buffer in ---
master

The warMODRegister and xarMODCoil buffers are always read or written by the master from the first index
(MODBUS -> 1).

The MODBUS diagnostic outputs usi_MODslaveAdrDone, usiMODfcDone, uiMODdatOffDone and uiMODdatNrDone


always mirror the communication state during a malfunction, i.e. they acknowledge the respective inputs, or provide
information concerning the content of the slave response.

The xMODtransActive output is High during job execution – until the slave response is received. After the job has been
completed, the output returns to “FALSE” and xMODtransOk is set to “1” if a telegram fault, protocol fault, data fault or
other communication fault has not occurred.
During a fault xMODtransOk remains at “FALSE” and the fault is described with the usiMODfailCode exception code
(see also the “Exception code” section).
If the slave does not answer within the set tMODtimeOut timeout time, a respective error message is issued here.

The outputs are refreshed with every new job start.

3.2.2 MODBUS slave

The MODBUS telegram of the master receives a response after the COM port is opened by the function block.
The slave exhibits a totally passive behaviour on the MODBUS. It will only respond to a request from the master.

Every valid MODBUS telegram which is received on the COM port is indicated by a pulse on the xFrameReceived
output.
The usiMODfc output describes the last job sent by the master. On the uiMODdatOff and uiMODdatNr outputs you can
read the start address and the data count of the received or sent data. Both outputs are updated from function blocks for
diagnostics even with a telegram fault.
With the Function Codes 5,6 and 8 uiMODdatNr describes the written value.
usiMODfailCode describes as is the case with the master, a telegram fault, protocol fault, data fault or other
communication fault (see also the “Exception codes” section).

Moeller GmbH AN2700K24G 8


MODBUS RTU Master/Slave for XC Controls

3.3 Exception codes

usiMODfailCode:

Telegram and data fault, as well as parameterization faults

00 : Ok
01 : Invalid function code
02 : Invalid slave data address
03 : Invalid value
04 : MODBUS device fault, telegram length fault
10 : Parameter fault
11 : Fault during opening or initialisation of the COM port
12 : CRC fault
*)
13 : MODBUS timeout
*)
14 : Slave address fault
*)
15 : Slave responds with incorrect FC
*)
16 : Slave responds with false number or offset of the data

*)
Only with ModbusMaster master function block

3.4 Examples

In the \Examples subdirectory you will find program examples for the integration of the master and slave connection in
the MODBUSSLAVEEXAMPLE.EXP and MODBUSMASTEREXAMPLE.EXP export file format.
After import of the required file the respective subdirectory is created in the project. Here you will find both the example
programs ModbusSlaveExampleVis or ModbusMasterExampleVis and ModbusSlaveExampleApp or
ModbusMasterExampleApp. The program with the “Vis” ending supports the visualisation with which the complete
function block can be operated and visualised. The program with the ending “App” is used for accessing the function
blocks in the standard manner -> parameterization of various inputs via constants.
The slave program example also has the required MODBUSSLAVE_DATASIZE.EXP export file (with default settings).

The export files do not contain a control configuration !

Moeller GmbH AN2700K24G 9


MODBUS RTU Master/Slave for XC Controls

4 Annex

4.1 Connector and connections of the controllers

4.1.1 XC100 programming port


The programming port supports the RS232 physical interface (RJ45 socket):

! AWB 2724-1453, AWB 2726-1461

4.1.2 XIOC-SER
The card supports the RS232 physical interface (SUB-D 9 pin male) as well as the RS485 / RS422 (COMBICON):

! AWA 2725-2118, AWB 2725-1455

4.1.3 COM port of XVC100, HPG and XC600


The COM1 of XVC100, HPG and XC600 supports the RS232 (SUB-D 9 male):

! More information in the respective documentation

Moeller GmbH AN2700K24G 10

You might also like