Example Code

Read Input Registers Using the Modbus Library for LabVIEW.

Code and Documents

Attachment

Overview


This example shows how to use the Modbus library for LabVIEW to read analog inputs from a Modbus slave device. The Slave device we are communicating with as an example is the Watlow MLS300 Controller that is connected via serial port.


Introduction to the Modbus Library for LabVIEW.

National Instruments provides a Modbus Library for LabVIEW that is designed to facilitate communication to a device that can use the Modbus protocol. Since Modbus is a serial communications protocol, the VISA driver can be used to send data over the serial port to communicate with a Modbus device. The Modbus library for LabVIEW is a set of VIs that take Modbus specific inputs and converts those to the appropriate VISA commands.

Using the Modbus Library for LabVIEW.

In this example, you will see that communicating with a Modbus device will involve initializing the port we are using, then using the query command, and finally closing the Port. This example can be modified very easily to write any outputs or read any inputs that the Modbus device makes available. The main parameters that will need to be changed are the Function Code and Starting Address. These 2 inputs to the Query VI are the inputs that tell LabVIEW which registers we are accessing and whether we are writing or reading values.

Determining parameters to use in this example for the Watlow MLS300.

The most challenging part of communicating with a Modbus device is figuring out what address to use and figuring out what to use for some of the other parameters. Generally, to figure this out, you will need to use the documentation for your Modbus device to figure this out. When using the Watlow MLS300, most of this information is located in either the MLS3XX Communications Specification or user manual. Both of these documents can be found on the Watlow web site

For example, if you wanted to read analog input 1 on the Watlow MLS300 controller, you would first have to make sure you are using the correct baud rate and slave address for the Watlow controller. To set parameters or determine the current values for parameters on the Watlow MLS300, you have to navigate through the menus on the controller interface. Information on how to navigate to these settings is located in the setup chapter (chapter 4) of the Watlow MLS300 user guide. For this example, we will use a baud rate of 19200 and a slave address of 1. These parameters have to be set on the controller and entered on the front panel of this example program.

Next you will have to enter information about the register you are trying to access. Since we are trying to read analog input 1 in this case, we need to find out the starting address for the analog inputs. In the Watlow MLS300 communication specification, under the Modbus communication chapter we can find the table shown below. The analog inputs are called "Process Variables". We can see that the starting address for process variables are 016B in Hex, which is 363 in decimal.

In the LV example program, on the front panel, we will enter the following:

Function code: Read Input Registers - Because we are reading analog input values
Starting Address: 363 or 016B - From the table in the communications specification
Quantity: 1 - Because we are reading 1 register.
Discrete Inputs and Data Inputs are going to be left blank because we are reading registers and not writing to them. If we were writing to analog outputs, we would put the data to write in the "Data Inputs".

Now when the example program is run, the reading of analog input 1 should be displayed in the "Register Outputs" array.

Notice:

In most cases, it is more efficient and easier to use OPC (OLE for process control) to communicate to these devices.  OPC is a tool that helps manager channels and communication with PLC's.  This includes communication with Modbus devices.  For more information on OPC, visit NI's web site and search for OPC.  

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors