Modbus is an application layer messaging protocol for client/server communication between devices connected on a bus or network. Modbus can be implemented using asynchronous serial transmission to communicate with touch screens, PLCs, and gateways to support other types of industrial buses.
The Modbus Serial Line protocol is based on a master/slave architecture. An address, ranging from 1 to 247, is assigned to each slave device. Only one master is connected to the bus at any given time. Slave devices do not transmit information unless a request is made by the master device, and slave devices cannot communicate to other slave devices.
Information is passed between master and slave devices by reading and writing to registers located on the slave device. The Modbus specification distinguishes the use of four register tables, each capable of 65,536 items and differentiated by register type and read-write access. Register address tables do not overlap in this implementation of LabVIEW.
LabVIEW includes the Class 0 and Class 1 functions of the Modbus Specification. A master device can use these requests to access the Modbus register tables.
Read Multiple Registers
Write Multiple Registers
Read Input Discretes
Read Input Registers
Write Single Register
Read Exception Status
For more information about the Modbus specification, visit www.modbus.org.
You must have the following hardware and software installed to perform the procedures outlined in this document:
NI provides LabVIEW VIs you can use to format and write values to an internal Modbus register. The VIs you use to interface with a Modbus device are included in the NIModbus.llb. This library includes the Modbus Serial Slave Demon VI, which you use to run in the background to monitor the serial port for commands issued by a remote master device. When the Modbus Server receives a Modbus request, it performs the specified action, which is to read or write to the Modbus registers, and sends a response to the master.
Complete the following steps to set up a slave device, such as an NI CVS-145x device or PC, to use LabVIEW to communicate with a Modbus master device, such as a touch screen:
At the start of a Modbus application, the four local register tables must be created in memory by LabVIEW. Initialize these registers using the MB Registers Manager VI. This VI is a multipurpose VI you can use to initialize, read, and write to any of the registers.
Complete the following steps to configure the MB Registers Manager VI:
Tip: The other inputs are not necessary for initializing the registers.
Use the MB Serial Init VI to specify which COM port the master device is connected to and to set the serial communication settings.
Complete the following steps to configure the MB Serial Init VI:
Note: These serial settings must match the serial settings of the master device.Note: Select RTU mode for the QTERM-G70 and EZTouch Panel. The RTU (remote terminal unit) transmission mode is available with all Modbus devices.
The Modbus Serial Slave Demon VI works as the server between the master device and the local Modbus registers. The VI waits for incoming commands from the master device on the COM port and processes them. This VI can be launched dynamically from the main VI to run in the background.
Complete the following steps to configure the Modbus Serial Slave VI :
Note: When targeting a CVS-145x device, upload the NIModbus.llb, which contains the MB Serial Slave Demon VI, to the CVS-145x device flash. In the Open VI Reference, specify the path of the MB Serial Slave Demon VI on the CVS-145x device.
Note: When building a Modbus slave application into an executable, you must add the MB Serial Slave Demon.vi as a dynamic VI in the Source Files tab in the LabVIEW Application Builder. NI suggests you specify a relative path for the MB Serial Slave Demon VI where the path is relative to the calling VI.
Use invoke nodes to set values for the MB Serial Slave Demon VI with the action Set Control Value [Variant].
Complete the following steps to set values for MB Serial Slave Demon VI:
Tip: When you repeat Step 2 after you add the first Invoke Node function, wire the reference output and error out outputs from the initial Invoke Node to the subsequent Invoke Nodes.
Tip: You do not need to use the Build Array function for the Slave Address item because it is not an array of values.
Add the global variable, Slave Demon Running, to the block diagram and initialize the global variable to True. The global variable is used later to stop the MB Serial Slave Demon VI remotely.
Complete the following steps to add the global variable:
Note: You must initialize the local Modbus registers before you run the slave demon.
Use the MB Registers Manager VI to read from and write to the local Modbus registers. Only U16 words can be written to and read from the holding and input registers. If data types other than U16 are necessary, you must programmatically format the other data types to fit the U16 register structure. Refer to the LabVIEW documentation for information about formatting data types.
You can write and read only Boolean values from the discrete inputs and coils registers.
In most Modbus device configuration software, you must enter a name for the register you want to use. Per Modbus convention, the register address of the slave device, which is the PC or CVS-145x device in this case, is calculated by subtracting 1 from the register name that you specify in the master device configuration software. When you are reading from or writing to a register on a Modbus device, be aware that LabVIEW expects register addresses rather than register names. When you enter the register address to read from in LabVIEW, the value you enter must be the register name you defined in the Modbus device configuration software minus 1.
The QTERM-G70 uses this naming convention. For example, a holding register name defined as 2 for the QTERM-G70 translates to register address 1 in the LabVIEW Holding Registers table.
Some Modbus devices use register tables and addresses as shown in the following illustration.
Because the register name and address tables do not overlap in LabVIEW, ignore the first digit, which indicates the register table. In LabVIEW, you select the register table by name from a drop-down list. To determine the start address in LabVIEW, enter the number after the register table number. For example, a register name defined as 0x40000 in a Modbus configuration device translates to register address 0 in the LabVIEW Holding Registers table.
Some devices, such as the EZTouch Panel, require both of these concepts. You must subtract 1 from the register name that you specify in the master device configuration software and ignore the first digit of the start address to ensure proper register addressing.
For example, a register name defined as 0x40008 in the EZTouch Panel translates to register address 7 in the LabVIEW Holding Registers table.
Complete the following steps to set up the MB Registers Manager VI to read from the local Modbus registers:
Complete the following steps to set up the MB Registers Manager VI to write to the local Modbus registers:
The final steps of your Modbus application must properly close all resources that were opened.
Complete the following steps to close resources:
The master device in a Modbus system does not contain any Modbus registers. The Modbus device sends read and write commands to the slave devices on the bus.
Complete the following steps to set up a master device, such as a CVS-145x device or PC, to use LabVIEW to communicate with Modbus slave devices on the same serial bus:
Note: These serial settings must match the slave device you are connected to.
Use the MB Serial Master Query VI to send both read and write commands to slave devices over the serial bus.
Complete the following steps to set up the MB Serial Master Query VI to read commands from the Modbus device:
Complete the following steps to close the VISA session opened by the MB Serial Init VI:
The NIModbus.llb includes two example programs: MB Serial Example Master and MB Serial Example Slave. If two COM ports are available on your PC, you can run both VIs to implement a Modbus master device and Modbus slave device simultaneously on the same computer. Use a null modem serial cable to connect the two COM ports.
The MB Serial Example Slave continuously reads the first four registers of its local coil and holding register table. The VI writes Boolean and U16 values to the first four registers of its local discrete register table and input register table, respectively.
The MB Serial Example Slave VI contains functions to open the MB Serial Example Slave VI and open a VI reference, which are wired into a series of sequence structures, and a while loop that are used to achieve its functionality.
The outer sequence structure handles the following functions:
The MB Serial Example Master VI writes Boolean and U16 values to the first four registers of the slave coil table and holding table, respectively. The VI continuously reads the first four registers of the slave discrete register table and input register table.
The MB Serial Example Master VI contains a series of sequence structures and a while loop that are used to achieve its functionality.
The MB Serial Example Slave VI reads and writes to local registers, and the MB Serial Example Master VI polls the local registers on the slave device. When the MB Serial Example Master VI is reading registers, it polls the registers on the slave device and displays the data on the master device. The MB Serial Example Master VI also writes to registers on the slave device.
Complete the following steps to run the MB Serial Example Slave VI and MB Serial Example Slave VI:
Collaborate with other users in our discussion forums
A valid service agreement may be required, and support options vary by country.