A201602e - Connect National Instruments LabVIEW Via ModbusTCP
A201602e - Connect National Instruments LabVIEW Via ModbusTCP
Application note
A201602, English
Version 1.1.1
2 • General
Technical Support
Phone: +49 (0) 571/8 87 – 5 55
Fax: +49 (0) 571/8 87 – 85 55
E-Mail: [email protected]
Every conceivable measure has been taken to ensure the correctness and com-
pleteness of this documentation. However, as errors can never be fully ex-
cluded we would appreciate any information or ideas at any time.
We wish to point out that the software and hardware terms as well as the
trademarks of companies used and/or mentioned in the present manual are
generally trademark or patent protected.
Application note
A201602
Table of Contents •3
TABLE OF CONTENTS
2 Description.................................................................................................. 5
5 Appendix................................................................................................... 14
5.1 The process image ............................................................................... 14
5.2 The MODBUS TCP DLL “MBT.dll”.................................................. 15
5.3 Common MODBUS functions............................................................. 16
5.4 Use of the MODBUS functions ........................................................... 18
5.4.1 Function code FC1 (Read Coils) ......................................................... 19
5.4.2 Function code FC2 (Read Discrete Inputs) ......................................... 20
5.4.3 Function code FC3 (Read multiple registers) ...................................... 21
5.4.4 Function code FC4 (Read input registers) ........................................... 22
5.4.5 Function code FC15 (Force Multiple Coils)........................................ 23
5.4.6 Function code FC16 (Write multiple registers) ................................... 24
5.4.7 Function code FC23 (Read/Write multiple registers).......................... 25
Application note
A201602
4• Important comments
1 Important comments
To ensure fast installation and start-up of the units described in this manual,
we strongly recommend that the following information and explanation is
carefully read and adhered to.
Application note
A201602
Description •5
2 Description
The purpose of this document is to provide step-by-step procedures for con-
figuring National Instruments LabVIEW for direct communications with
WAGO Ethernet I/O. The procedures that follow illustrate solutions for a di-
rect communication between LabVIEW and the WAGO-I/O without additional
protocols or proxis (like OPC, described in appnote “A201601”) using the
open standard ModbusTCP.
Both ways have its advantages, that mean the comfort supplied by the
“MBT.dll” is payed by an increased CPU-usage, but both solutions are up to
20 times faster then an OPC based solution.
Adapting these examples, you need to know on how the buscoupler or control-
lers build the process image and how to access the process image with the
proper Modbus function code and address locations.
Application note
A201602
6• Description
The procedures in this document have been tested with the following hard-
ware/software configurations:
Under www.ethereal.com you find the free network protocol analyser “ethe-
real”. This software helpful to understand messages on the network.
Application note
A201602
Calling the “MBT.dll” from LabVIEW •7
The subvi bundels the calls of the two function MBTInit() and MBTCon-
nect().
Application note
A201602
8• Calling the “MBT.dll” from LabVIEW
In this example Modbus address 1 cycles through to read one word of data.
Address 1 correspond to the second channel of the thermocouples module.
WAGO starts at Word Address 0
Application note
A201602
Calling the “MBT.dll” from LabVIEW •9
Application note
A201602
10 • Build a socket based application
Take into account that this example has no modbus error handling.
Application note
A201602
Build a socket based application • 11
The block diagram shows a hard coded functioncode 23 that reads two (input)
words from address 4 and writes two words to the (output) address 4.
The application runs a while loop and executes the following list of function-
codes in each loop.
Application note
A201602
12 • Build a socket based application
Application note
A201602
Build a socket based application • 13
The INI-file of this example can have any name, only the file extension “.ini”
is mandatory. The content of the file starts with the section “WAGO-module”
followed by a key for each module. The values are section names, where de-
tailed information for each module is defined.
The section “MyCoupler” has a different structure than the rest, this describes
the head station and contains the network parameter like Ip-address and port
number.
To work with a module not listet, identify the “Type”, “Channels” and “Bit-
Count” for this module and create a new section.
Application note
A201602
14 • Appendix
5 Appendix
5.1 The process image
The powered-up controller recognizes all I/O modules connected.
The controller generates an internal local process image from the data width
and type of I/O modules, as well as the position of the I/O modules in the
node. This image is divided into an input data area and an output data area.
The data of the digital I/O modules are bit-based (i.e., the data exchange is-
made by bits). The analog I/O and most specialty modules (e.g., counter mod-
ules, encoder modules, and communication modules) are byte-based, in which
the data exchange is made by bytes.
The process image is divided into an input data area and an output data area.
Each I/O module is assigned a location in the process image, based on the data
exchange type (i.e., bit-based or byte-based) and their position after the con-
troller.
All of the byte-based I/O modules are filled in the process image first, then the
bit-based modules. The bits of the digital modules are grouped into a word.
Once the number of digital I/Os exceeds 16 bits, the controller automatically
starts another word.
Changing the physical layout of a node will result in a new structure of the
process image. Also, the addresses of the process data will change.
When adding or removing modules, the process data must be verified.
The process image for physical input and output data is stored in the first 256
words of memory (word 0 to 255). This memory actually consists of a sepa-
rate area for the input and output data.
Application note
A201602
Appendix • 15
The DLL supports synchronous and asynchronous reading and writing of val-
ues. TCP or UDP can be selected optionally as a transport protocol. The dll
can be used by nearly all programming languages for microsoft operating sys-
tems like C, C++, C#, Delphi, LabVIEW, VBA, VB6, vb.net and more.
For an actual list of examples contact [email protected] or order 759-312
for the “WAGO Modbus TCP DLL CD”.
The complied manual can also be found in the internet.under:
http://www.wago.com/wagoweb/documentation/759/eng_manu/312/m931200
e.pdf
Visual Basic and LabVIEW allow only synchronous function calls of the
DLL.
This library only supports the commands FC1, FC2, FC3, FC4, FC7, FC15
and FC16 from Open Modbus/TCP protocol V1.0.
All functions of the MBT library have return values corresponding to the
HRESULT format. The functions of the socket APIs do not return any return
values of this format. The MBT library converts these return values by means
of the macro HRESULT_FROM_WIN32. In the following description this is
indicated by means of "HR from". The following functions are contained in
the Modbus/TCP.DLL:
- MBTInit()
- MBTExit()
- MBTConnect()
- MBTDisconnect()
- MBTReadRegisters()
- MBTReadCoils()
- MBTReadExeptionStatus()
- MBTReadCompleted()
- MBTWriteRegisters()
- MBTWriteCoils()
- MBTWriteCompleted()
- MBTSwapWord()
- MBTSwapDWord()
The DLL was developed with the Microsoft Visual C++ 6.0 development en-
vironment. All modules of the DLL are translated as ASCII components with
a statically linked C runtime.
Application note
A201602
16 • Appendix
These functions allow digital or analog input and output data to be set or di-
rectly read out of the fieldbus node.
FC7: 0x07 read exception status Reading of the first 8 input bits
FC23 0x17 read/write multiple Reading and writing of several output registers
registers
Tab. 5-1: List of the MODBUS functions in the fieldbus coupler and controller
To execute a desired function, specify the respective function code and the
address of the selected input or output channel.
Attention
The examples listed use the hexadecimal system (i.e.: 0x0000) as their nu-
merical format. Addressing begins with 0.
The format and beginning of the addressing may vary according to the soft-
ware and the control system. All addresses then need to be converted accord-
ingly.
All MODBUS functions in the WAGO ETHERNET fieldbus coupler and con-
troller are executed as follows:
When a function code is entered, the MODBUS master (i.e. PC) makes a re-
quest to the coupler/controller of the fieldbus node.
Application note
A201602
Appendix • 17
The exception code contained in the exception has the following meaning:
Note
In the case of the read functions (FC1 – FC 4) the outputs can be additionally
written and read back by adding an offset of 200hex (0x0200) to the MODBUS
address.
Application note
A201602
18 • Appendix
LINK
TxD/RxD
ERROR
I/O
Application note
A201602
Appendix • 19
Request
The request determines the starting address and the number of bits to be read.
Example: An inquiry, with which the bits 0 to 7 are to be read.
Byte Field name Example
Byte 0, 1 Transaction identifier 0x0000
Byte 2, 3 protocol identifier 0x0000
Byte 4, 5 length field 0x0006
Byte 6 unit identifier 0x01 not used
Byte 7 MODBUS function code 0x01
Byte 8, 9 reference number 0x0000
Byte 10, 11 Bit count 0x0008
Response
The current values of the inquired bits are packed in the data field. A 1 corre-
sponds to the ON status and a 0 to the OFF status. The lowest value bit of the
first data byte contains the first bit of the inquiry. The others follow in ascend-
ing order. If the number of inputs is not a multiple of 8, the remaining bits of
the last data byte are filled with zeroes (truncated).
Byte Field name Example
.....
Byte 7 MODBUS function code 0x01
Byte 8 Byte count 0x01
Byte 9 Bit values 0x12
The status of the inputs 7 to 0 is shown as byte value 0x12 or binary 0001
0010.
Input 7 is the bit having the highest significance of this byte and input 0 the
lowest value.
The assignment is thus made from 7 to 0 with OFF-OFF-OFF-ON-OFF-OFF-
ON-OFF.
Exception
Byte Field name Example
.....
Byte 7 MODBUS function code 0x81
Byte 8 Exception code 0x01 or 0x02
Application note
A201602
20 • Appendix
Requests
The request determines the starting address and the number of bits to be read.
Example: An inquiry with which the bits 0 to 7 are to be read:
Byte Field name Example
Byte 0, 1 Transaction identifier 0x0000
Byte 2, 3 protocol identifier 0x0000
Byte 4, 5 Length field 0x0006
Byte 6 unit identifier 0x01 not used
Byte 7 MODBUS function code 0x02
Byte 8, 9 reference number 0x0000
Byte 10, 11 Bit count 0x0008
Response
The current value of the inquired bit is packed into the data field. A 1 corre-
sponds to the ON status and a 0 the OFF status. The lowest value bit of the
first data byte contains the first bit of the inquiry. The others follow in an as-
cending order. If the number of inputs is not a multiple of 8, the remaining bits
of the last data byte are filled with zeroes (truncated).
Byte Field name Example
.....
Byte 7 MODBUS function code 0x02
Byte 8 Byte count 0x01
Byte 9 Bit values 0x12
The status of the inputs 7 to 0 is shown as a byte value 0x12 or binary 0001
0010.
Input 7 is the bit having the highest significance of this byte and input 0 the
lowest value.
The assignment is thus made from 7 to 0 with OFF-OFF-OFF-ON-OFF-OFF-
ON-OFF.
Exception
Byte Field name Example
.....
Byte 7 MODBUS function code 0x82
Byte 8 Exception code 0x01 or 0x02
Application note
A201602
Appendix • 21
Request
The request determines the start word address (start register) and the number
the register to be read. The addressing starts with 0.
Example: An inquiry of the registers 0 and 1:
Response
The reply register data is packed as 2 bytes per register. The first byte contains
the higher value bits, the second the lower values.
Exception
Application note
A201602
22 • Appendix
Request
The request determines the address of the start word (start register) and the
quantity of the registers to be read. Addressing starts with 0.
Response
The register data of the answer is packed as 2 bytes per register. The first byte
has the higher value bits, the second the lower values.
Exception
Application note
A201602
Appendix • 23
Request
The first point is addressed with 0.
The inquiry message specifies the bits to be set. The requested 1 or 0 states are
determined by the contents of the inquiry data field.
In this example 16 bits are set, starting with the address 0. The inquiry con-
tains 2 bytes with the value 0xA5F0 or 1010 0101 1111 0000 in binary format.
The first byte transmits the 0xA5 to the addresses 7 to 0, whereby 0 is the low-
est value bit. The next byte transmits 0xF0 to the addresses 15 to 8, whereby
the lowest value bit is 8.
Response
Byte Field name Example
.....
Byte 7 MODBUS function code 0x0F
Byte 8, 9 Reference number 0x0000
Byte 10, 11 Bit Count 0x0010
Exception
Byte Field name Example
.....
Byte 7 MODBUS function code 0x8F
Byte 8 Exception code 0x01 or 0x02
Application note
A201602
24 • Appendix
Request
The inquiry message determines the registers to be set. The data is sent as 2
bytes per register.
The example shows how data is set in the two registers 0 and 1:
Response
Exception
Application note
A201602
Appendix • 25
Request
The inquiry message determines the registers to be read and set. The data is
sent as 2 bytes per register.
Example: The data in register 3 is set to value 0x0123, and values 0x0004 and
0x5678 are read out of the two registers 0 and 1.
Response
Exception
Application note
A201602
WAGO Kontakttechnik GmbH
Postfach 2880 • D-32385 Minden
Hansastraße 27 • D-32423 Minden
Phone: 05 71/8 87 – 0
Telefax: 05 71/8 87 – 1 69
E-Mail: [email protected]
Internet: http://www.wago.com