TF6250 TC3 Modbus TCP EN
TF6250 TC3 Modbus TCP EN
TF6250 TC3 Modbus TCP EN
TF6250
TwinCAT 3 | Modbus TCP
Table of contents
1 Foreword .................................................................................................................................................... 5
1.1 Notes on the documentation.............................................................................................................. 5
1.2 Safety instructions ............................................................................................................................. 6
1.3 Notes on information security ............................................................................................................ 7
2 Overview..................................................................................................................................................... 8
3 Installation.................................................................................................................................................. 9
3.1 System Requirements ....................................................................................................................... 9
3.2 Installation ......................................................................................................................................... 9
3.3 Installation Windows CE .................................................................................................................. 12
3.4 Installing the TwinCAT/BSD ............................................................................................................ 14
3.5 Licensing ......................................................................................................................................... 14
4 Configuration ........................................................................................................................................... 17
4.1 Overview.......................................................................................................................................... 17
4.2 TwinCAT Modbus TCP Configurator ............................................................................................... 17
4.3 Default Configuration ....................................................................................................................... 19
5 Diagnosis.................................................................................................................................................. 20
5.1 Modbus ADS Diagnosis Interface.................................................................................................... 20
7 Samples .................................................................................................................................................... 54
7.1 Sample: Digital IO access ............................................................................................................... 54
7.2 Sample: Multiple register access ..................................................................................................... 55
8 Appendix .................................................................................................................................................. 56
8.1 Overview.......................................................................................................................................... 56
8.2 ADS Return Codes .......................................................................................................................... 56
1 Foreword
The responsible staff must ensure that the application or use of the products described satisfy all the
requirements for safety, including all the relevant laws, regulations, guidelines and standards.
Disclaimer
The documentation has been prepared with care. The products described are, however, constantly under
development.
We reserve the right to revise and change the documentation at any time and without prior announcement.
No claims for the modification of products that have already been supplied may be made on the basis of the
data, diagrams and descriptions in this documentation.
Trademarks
Beckhoff®, TwinCAT®, TwinCAT/BSD®, TC/BSD®, EtherCAT®, EtherCAT G®, EtherCAT G10®, EtherCAT P®,
Safety over EtherCAT®, TwinSAFE®, XFC®, XTS® and XPlanar® are registered trademarks of and licensed by
Beckhoff Automation GmbH.
Other designations used in this publication may be trademarks whose use by third parties for their own
purposes could violate the rights of the owners.
Patent Pending
The EtherCAT Technology is covered, including but not limited to the following patent applications and
patents:
EP1590927, EP1789857, EP1456722, EP2137893, DE102015105702
with corresponding applications or registrations in various other countries.
EtherCAT® is a registered trademark and patented technology, licensed by Beckhoff Automation GmbH,
Germany
Copyright
Exclusion of liability
All the components are supplied in particular hardware and software configurations appropriate for the
application. Modifications to hardware or software configurations other than those described in the
documentation are not permitted, and nullify the liability of Beckhoff Automation GmbH & Co. KG.
Personnel qualification
This description is only intended for trained specialists in control, automation and drive engineering who are
familiar with the applicable national standards.
Description of symbols
In this documentation the following symbols are used with an accompanying safety instruction or note. The
safety instructions must be read carefully and followed without fail!
DANGER
Serious risk of injury!
Failure to follow the safety instructions associated with this symbol directly endangers the life and health of
persons.
WARNING
Risk of injury!
Failure to follow the safety instructions associated with this symbol endangers the life and health of per-
sons.
CAUTION
Personal injuries!
Failure to follow the safety instructions associated with this symbol can lead to injuries to persons.
NOTE
Damage to the environment or devices
Failure to follow the instructions associated with this symbol can lead to damage to the environment or
equipment.
Tip or pointer
This symbol indicates information that contributes to better understanding.
In addition, the recommendations from Beckhoff regarding appropriate protective measures should be
observed. Further information regarding information security and industrial security can be found in our
https://www.beckhoff.com/secguide.
Beckhoff products and solutions undergo continuous further development. This also applies to security
functions. In light of this continuous further development, Beckhoff expressly recommends that the products
are kept up to date at all times and that updates are installed for the products once they have been made
available. Using outdated or unsupported product versions can increase the risk of cyber threats.
To stay informed about information security for Beckhoff products, subscribe to the RSS feed at https://
www.beckhoff.com/secinfo.
2 Overview
The TwinCAT Modbus TCP server enables to communicate over a network connection (TCP/IP) with the
Modbus protocol.
Modbus is an open standard in industrial communication which will be maintained by the independent
Modbus Organization.
The protocol is based on a client/server-architecture. Therefore the product can be used as client or as
server:
(1) The TwinCAT Modbus TCP server enables to access the TwinCAT PLC. The Modbus register and I/O's
are then mapped to TwinCAT PLC areas.
(2) The supplied PLC-library allows to communicate with other Modbus devices to request data (e.g.
measured values, states) and control them.
3 Installation
Requirements
3.2 Installation
The following section describes how to install the TwinCAT 3 Function for Windows-based operating
systems.
ü The TwinCAT 3 Function setup file was downloaded from the Beckhoff website.
1. Run the setup file as administrator. To do this, select the command Run as administrator in the context
menu of the file.
ð The installation dialog opens.
2. Accept the end user licensing agreement and click Next.
4. If you want to install the full version of the TwinCAT 3 Function, select Complete as installation type. If
you want to install the TwinCAT 3 Function components separately, select Custom.
ð A dialog box informs you that the TwinCAT system must be stopped to proceed with the installation.
6. Confirm the dialog with Yes.
ð The TwinCAT 3 Function has been successfully installed and can be licensed (see Licensing [} 14]).
If an older TFxxx version is already installed on the Windows CE device, it can be updated:
• Software upgrade [} 13]
The CAB installation file for Windows CE is part of the TFxxx setup. This is made available on the Beckhoff
website www.beckhoff.com and automatically contains all versions for Windows XP, Windows 7 and
Windows CE (x86 and ARM).
Download the TFxxx setup file and install the TwinCAT 3 function as described in the Installation [} 9]
section.
After the installation, the installation folder contains three directories (one directory per hardware platform):
• CE-ARM: ARM-based Embedded PCs running Windows CE, e.g. CX8090, CX9020
• CE-X86: X86-based Embedded PCs running Windows CE, e.g. CX50xx, CX20x0
• Win32: Embedded PCs running Windows XP, Windows 7 or Windows Embedded Standard
The CE-ARM and CE-X86 directories contain the CAB files of the TwinCAT 3 function for Windows CE in
relation to the respective hardware platform of the Windows CE device.
Further information can be found in the Beckhoff Information System in the "Operating Systems"
documentation (Embedded PC > Operating Systems > CE).
After transferring the CAB file to the Windows CE device, double-click the file there. Confirm the installation
dialog with OK. Then restart the Windows CE device.
After restarting the device, the files of the TwinCAT 3 function (TFxxxx) are automatically loaded in the
background and are then available.
Software upgrade
If an older version of the TwinCAT 3 function is already installed on the Windows CE device, carry out the
following steps on the Windows CE device to upgrade to a new version:
1. Open the CE Explorer by clicking Start > Run and entering "Explorer".
2. Navigate to \Hard Disk\TwinCAT\Functions\TFxxx\xxxx.
3. Rename the file Tc*.exe to Tc*.old.
4. Restart the Windows CE device.
5. Transfer the new CAB file to the Windows CE device.
6. Run the CAB file on the Windows CE device and install the new version.
7. Delete the file Tc*.old.
8. Restart the Windows CE device.
ð The new version is active after the restart.
Further information about the Package Server can be found in the TwinCAT/BSD manual.
The installation stores the TwinCAT Modbus TCP Server and its default configuration file
TcModbusSrv.xml in the following directory:
ls /usr/local/etc/TwinCAT/Functions/TF6250-Modbus-TCP
After a restart of the system or restart of TwinCAT, the Modbus TCP Server is started and the configuration
from TcModbusSrv.xml is taken over.
To adapt the configuration file, different text editors are available under TwinCAT/BSD.
Alternatively, you can use a remote access to replace TcModbusSrv.xml with an existing configuration file.
In order for the changes to TcModbusSrv.xml to be adopted by the TwinCAT Modbus Server, the TwinCAT
system must be stopped and restarted. This can be achieved via the following command:
doas service TcSystemService restart
3.5 Licensing
The TwinCAT 3 function can be activated as a full version or as a 7-day test version. Both license types can
be activated via the TwinCAT 3 development environment (XAE).
A description of the procedure to license a full version can be found in the Beckhoff Information System in
the documentation "TwinCAT 3 Licensing".
ð A dialog box opens, prompting you to enter the security code displayed in the dialog.
4 Configuration
4.1 Overview
The server can receive Modbus functions via TCP/IP.
Modbus-areas
After the installation the modbus areas are mapped to the PLC areas. Check the article about the default-
mapping [} 19].
The TwinCAT Modbus TCP/IP server configurator [} 17] is used for configuring this mapping.
ADS-Access
If you want to access the specific modbus areas, you have to add these global variables to your PLC project.
VAR_GLOBAL
mb_Input_Coils : ARRAY [0..255] OF BOOL;
mb_Output_Coils : ARRAY [0..255] OF BOOL;
mb_Input_Registers : ARRAY [0..255] OF WORD;
mb_Output_Registers : ARRAY [0..255] OF WORD;
END_VAR
Requirements
IP Address: IP of the server. If no address is set, the local one is used (default) .
Set Default Configuration: Reset to default-settings (use local ip, Port = 502, and default mapping [} 19]).
TwinCAT must be stopped if you want to use the configurator, which will be done by the tool.
Export configuration
The configuration is XML-based and can be changed by a text editor. With "Export Configuration" the actual
configuration can be stored local as XML-file.
Import Mapping-Informations
Windows CE
The server maps the individuals ADS areas and enables the access to the physical process image and maps
the PLC data area.
The mapping can be adjusted by the TwinCAT Modbus TCP Configurator [} 17].
5 Diagnosis
AMSNetID: AMSNetID of the system. If the local system is used leave empty.
6 PLC libraries
6.1 Overview
The defined modbus functions are implemented in the PLC library TcModbusSrv.lib.
Requirements
This function is used for reading 1 to 2048 digital outputs (coils). One digital output corresponds to one bit of
the read data bytes.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pDestAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity : Number of digital inputs (data bits) to be read. The value of nQuantity must be > 0.
cbLength : Contains the max. byte size of the destination buffer into which the data are to be read. The
minimum buffer byte size must be: (nQuantity + 7) / 8.
pDestAddr : Contains the address of the destination buffer into which the data are to be read. The buffer
can be a single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the ReadCoils command, the content of digital
outputs 6 - 15 is written into the arrData array:
Requirements
This function is used for reading 1 to 2048 digital inputs. One digital input corresponds to one bit of the read
data bytes.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity: Number of digital inputs (data bits) to be read. The value of nQuantity must be > 0.
cbLength: Contains the max. byte size of the destination buffer. The minimum buffer byte size must be:
(nQuantity + 7) / 8.
pDestAddr: Contains the address of the destination buffer into which the data are to be read. The buffer can
be a single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the ReadInputs command, the content of digital
inputs 30 - 49 is written into the arrData array:
Requirements
This function is used for reading 1 to 128 output registers (16 bit). The first byte contains the lower eight bits
and the second byte the upper eight bits.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pDestAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity: Number of output registers (data words) to be read. The value of nQuantity must be > 0.
cbLength: Contains the max. byte size of the destination buffer. The minimum buffer byte size must be:
nQuantity * 2.
pDestAddr: Contains the address of the destination buffer into which the data are to be read. The buffer can
be a single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the ReadRegs command, the content of
registers 25 and 26 is located in the arrData array:
Requirements
This function is used for reading 1 to 128 input registers (16 bit). Observe the byte-order little endian.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pDestAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity: Number of input registers (data words) to be read. The value of nQuantity must be > 0.
cbLength: Contains the max. byte size of the destination buffer. The minimum buffer byte size must be:
nQuantity * 2.
pDestAddr: Contains the address of the destination buffer into which the data are to be read. The buffer can
be a single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the ReadRegs command, the content of
registers 3-5 is located in the arrData array:
Requirements
This function is used for writing a single digital output (coil). Bit access is used.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nMBAddr : WORD;
nValue : WORD;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nValue: Value to be written into the digital output. The value 16#FF00 switches the output on, 16#0000
switches it off.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the WriteSingleCoil command, digital output 4
is switched on.
Requirements
This function is used for writing an individual output register. 16 bit access is used.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nMBAddr : WORD;
nValue : WORD;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the WriteSingleReg command, the value
16#1234 is written into register 5.
Requirements
This function is used for writing 1 to 2048 digital outputs (coils). One digital output corresponds to one bit of
the write data bytes.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pSrcAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity: Number of digital outputs to be written (data bits). nQuantity must be > 0.
cbLength: Contains the max. byte size of the source buffer containing the data to be written. The minimum
buffer byte size must be: (nQuantity + 7) / 8.
pSrcAddr: Contains the address of the source buffer containing the data to be written. The buffer can be a
single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the ReadCoils command, the content of the
arrData array is written to digital outputs 15 - 24:
Bit 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 1
Outp 22 21 20 19 18 17 16 15 X X X X X X 24 23
ut
Requirements
This function is used for writing 1 to 128 output registers (16 bit).
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pSrcAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
cbLength: Contains the max. byte size of the source buffer. The minimum buffer byte size must be:
nQuantity * 2.
pSrcAddr: Contains the address of the source buffer containing the data to be written. The buffer can be a
single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
bBusy: When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError: If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId: Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the ReadRegs command, the content of the
arrData array is written to registers 5-7.
Requirements
This function first reads 1 to 128 output registers (16 bit) and then writes 1 to 128 output registers (16 bit).
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nReadQuantity : WORD;
nMBReadAddr : WORD;
nWriteQuantity : WORD;
nMBWriteAddr : WORD;
cbDestLength : UDINT;
pDestAddr : POINTER OF BYTE;
cbSrcLength : UDINT;
pSrcAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nReadQuantity : Number of output registers (data words) to be read. The value ofnReadQuantitymust be >
0.
nWriteQuantity : Number of output registers (data words) to be written. The value ofnWriteQuantitymust be
> 0.
cbDestLength: Contains the max. byte size of the destination buffer. The minimum destination buffer byte
size must benReadQuantity * 2.
pDestAddr : Contains the address of the destination buffer into which the data are to be read. The buffer
can be a single variable, an array or a structure, whose address can be found with the ADR operator.
cbSrcLength: Contains the max. byte size of the source buffer. The minimum source buffer byte size must
benWriteQuantity * 2.
pSrcAddr : Contains the address of the source buffer containing the data to be written. The buffer can be a
single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy: When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError: If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId: Supplies the ADS error number [} 56] when the bError output is set.
After a rising edge of "bExecute" and successful execution of the ReadWriteRegs command, arrRdData
contains the read register data, and the data from arrWrData are written to the registers.
Requirements
The diagnosis function provides a series of tests for checking the communication system between the
master and the slave and for examining a variety of internal error states within the slave.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nSubFnc : WORD;
nWriteData : WORD;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
nReadData : WORD;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
After rising edge of "bExecute" and successful execution of the diagnosis command, nReadData contains
the read data word.
Requirements
6.2.11 UDP
This function is used for reading 1 to 2048 digital outputs (coils). One digital output corresponds to one bit of
the read data bytes.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pDestAddr : UDINT;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity : Number of digital inputs (data bits) to be read. The value of nQuantity must be > 0.
cbLength : Contains the max. byte size of the destination buffer into which the data are to be read. The
minimum buffer byte size must be: (nQuantity + 7) / 8.
pDestAddr : Contains the address of the destination buffer into which the data are to be read. The buffer
can be a single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
This function is used for reading 1 to 2048 digital inputs. One digital input corresponds to one bit of the read
data bytes.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pDestAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity: Number of digital inputs (data bits) to be read. The value of nQuantity must be > 0.
cbLength: Contains the max. byte size of the destination buffer. The minimum buffer byte size must be:
(nQuantity + 7) / 8.
pDestAddr: Contains the address of the destination buffer into which the data are to be read. The buffer can
be a single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
This function is used for reading 1 to 128 output registers (16 bit). The first byte contains the lower eight bits
and the second byte the upper eight bits.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pDestAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity: Number of output registers (data words) to be read. The value of nQuantity must be > 0.
cbLength: Contains the max. byte size of the destination buffer. The minimum buffer byte size must be:
nQuantity * 2.
pDestAddr: Contains the address of the destination buffer into which the data are to be read. The buffer can
be a single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
This function is used for reading 1 to 128 input registers (16 bit). Endian
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pDestAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity: Number of input registers (data words) to be read. The value of nQuantity must be > 0.
cbLength: Contains the max. byte size of the destination buffer. The minimum buffer byte size must be:
nQuantity * 2.
pDestAddr: Contains the address of the destination buffer into which the data are to be read. The buffer can
be a single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
This function is used for writing a single digital output (coil). Bit access is used.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nMBAddr : WORD;
nValue : WORD;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nValue: Value to be written into the digital output. The value 16#FF00 switches the output on, 16#0000
switches it off.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
This function is used for writing an individual output register. 16 bit access is used.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nMBAddr : WORD;
nValue : WORD;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
This function is used for writing 1 to 2048 digital outputs (coils). One digital output corresponds to one bit of
the write data bytes.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pSrcAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nQuantity: Number of digital outputs to be written (data bits). nQuantity must be > 0.
cbLength: Contains the max. byte size of the source buffer containing the data to be written. The minimum
buffer byte size must be: (nQuantity + 7) / 8.
pSrcAddr: Contains the address of the source buffer containing the data to be written. The buffer can be a
single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
This function is used for writing 1 to 128 output registers (16 bit).
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nQuantity : WORD;
nMBAddr : WORD;
cbLength : UDINT;
pSrcAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
cbLength: Contains the max. byte size of the source buffer. The minimum buffer byte size must be:
nQuantity * 2.
pSrcAddr: Contains the address of the source buffer containing the data to be written. The buffer can be a
single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
bBusy: When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError: If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId: Supplies the ADS error number [} 56] when the bError output is set.
Requirements
This function first reads 1 to 128 output registers (16 bit) and then writes 1 to 128 output registers (16 bit).
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nReadQuantity : WORD;
nMBReadAddr : WORD;
nWriteQuantity : WORD;
nMBWriteAddr : WORD;
cbDestLength : UDINT;
pDestAddr : POINTER OF BYTE;
cbSrcLength : UDINT;
pSrcAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
nReadQuantity : Number of output registers (data words) to be read. The value of nReadQuantity must be >
0.
nWriteQuantity : Number of output registers (data words) to be written. The value of nWriteQuantity must be
> 0.
cbDestLength : Contains the max. byte size of the destination buffer. The minimum destination buffer byte
size must be nReadQuantity * 2.
pDestAddr : Contains the address of the destination buffer into which the data are to be read. The buffer
can be a single variable, an array or a structure, whose address can be found with the ADR operator.
cbSrcLength : Contains the max. byte size of the source buffer. The minimum source buffer byte size must
be nWriteQuantity * 2.
pSrcAddr : Contains the address of the source buffer containing the data to be written. The buffer can be a
single variable, an array or a structure, whose address can be found with the ADR operator.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
The diagnosis function provides a series of tests for checking the communication system between the
master and the slave and for examining a variety of internal error states within the slave.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nSubFnc : WORD;
nWriteData : WORD;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP,
this value must be 16#FF.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
nReadData : WORD;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is
received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the
bBusy output is reset.
nErrId : Supplies the ADS error number [} 56] when the bError output is set.
Requirements
Global_Version
VAR_GLOBAL CONSTANT
stLibVersion_Tc2_ModbusSrv : ST_LibVersion;
END_VAR
To compare the existing version to a required version the function F_CmpLibVersion (defined in Tc2_System
library) is offered.
All other possibilities known from TwinCAT2 libraries to query a library version are obsolete!
7 Samples
The default mapping [} 19] of the TwinCAT Modbus TCP mapps the digital output (coils) to the physical
outputs of the PLC.
PROGRAM MAIN
VAR
Q00 AT%QX0.0 : BOOL;
Q01 AT%QX0.1 : BOOL;
Q02 AT%QX0.2 : BOOL;
Q03 AT%QX0.3 : BOOL;
Q04 AT%QX0.4 : BOOL;
Q05 AT%QX0.5 : BOOL;
Q06 AT%QX0.6 : BOOL;
Q07 AT%QX0.7 : BOOL;
fbWriteCoils : FB_MBWriteCoils;
bWrite : BOOL;
nValue : INT;
END_VAR
bWrite := TRUE;
fbWriteCoils.nQuantity := 8;
fbWriteCoils.cbLength := SIZEOF(nValue);
fbWriteCoils.pSrcAddr := ADR(nValue);
fbWriteCoils.tTimeout := T#5s;
fbWriteCoils(bExecute:=TRUE);
The counter nValue will be written to physical outputs of the plc (Q00-Q07) by a rising edge of bWrite.
Requirements
The Modbusaddress 0x3000 is mapped by the default-configuration to the memory area of the plc (ADS-
Indexgroup 0x4020)
PROGRAM MAIN
VAR
ipAddr : STRING(15) := '';
M0 AT%MB0 : ARRAY [0..3] OF WORD;
nValue : ARRAY [0..3] OF WORD;
fbWriteRegs : FB_MBWriteRegs;
bWriteRegs : BOOL;
END_VAR
IF NOT bWriteRegs THEN
nValue[0]:= nValue[0]+1;
nValue[1]:= nValue[1]+1;
nValue[2]:= nValue[2]+1;
nValue[3]:= nValue[3]+1;
bWriteRegs :=TRUE;
fbWriteRegs.sIPAddr :=ipAddr;
fbWriteRegs.nQuantity := 4;
fbWriteRegs.nMBAddr := 16#3000;
fbWriteRegs.cbLength := SIZEOF(nValue);
fbWriteRegs.pSrcAddr := ADR(nValue);
fbWriteRegs.tTimeout := T#5s;
fbWriteRegs(bExecute:=TRUE);
ELSE
IF NOT fbWriteRegs.bBUSY THEN
bWriteRegs :=FALSE;
END_IF
fbWriteRegs(bExecute:=FALSE);
END_IF
The array arrValue will be written to the memory area of the plc (M0) by a rising edge on bWriteRegs.
Requirements
8 Appendix
8.1 Overview
TwinCAT ADS return code
Requirements
0x501 1281 0x9811 0501 ROUTERERR_RESIZEMEMORY The router memory size could not be changed.
0x502 1282 0x9811 0502 ROUTERERR_MAILBOXFULL The mailbox has reached the maximum number of
possible messages.
0x503 1283 0x9811 0503 ROUTERERR_DEBUGBOXFULL The Debug mailbox has reached the maximum
number of possible messages.
0x100D 4109 0x9811 100D RTERR_EXTIRQALREADYDEF An external synchronization interrupt is already ap-
plied.
0x100E 4110 0x9811 100E RTERR_EXTIRQNOTDEF No external sync interrupt applied.
0x100F 4111 0x9811 100F RTERR_EXTIRQINSTALLFAILED Application of the external synchronization interrupt
has failed.
0x1010 4112 0x9811 1010 RTERR_IRQLNOTLESSOREQUAL Call of a service function in the wrong context
0x1017 4119 0x9811 1017 RTERR_VMXNOTSUPPORTED Intel VT-x extension is not supported.
0x1018 4120 0x9811 1018 RTERR_VMXDISABLED Intel VT-x extension is not enabled in the BIOS.
0x1019 4121 0x9811 1019 RTERR_VMXCONTROLSMISSING Missing function in Intel VT-x extension.
0x101A 4122 0x9811 101A RTERR_VMXENABLEFAILS Activation of Intel VT-x fails.