Labview Modbus Serial Example

JavaScript is disabled. Details

  1. ModBus RTU Master, NI LabView-(RS-232)- (RS-232)-RF416 - RF416-(CSI/O)-Modbus RTU Slave, CR1000. Each device in this application needs to be configured appropriately. Each device is configured to match the configuration of the device it is attached to. For example, the RS-232 baud rate of the RF416 needs to match the RS-232 baud rate of the.
  2. The original Modbus Protocol specification, published in 1979, describes Serial Communications where data is transmitted one bit at a time. A later update to the standard, called Modbus TCP, describes how to use Modbus in TCP/IP networks. The rest of this article describes the Modbus protocol is more detail: Modbus Data Types.

The master uses this VI to read the slaves' holding registers. This corresponds to a public function code of 3 in the MODBUS protocol.

Serial Parameters are parameters that modify the way the MODBUS frame is structured. Refer to the MODBUS specification for more information about the MODBUS Frame. The Serial Parameters in LabVIEW is a cluster containing an enum (Mode) and an integer (Slave Address).

Mode

RTUData is represented in binary format.

ASCII
Data is represented in ASCII, so it is human readable.

Slave Address

This is the address of the slave you are communicating with. This property is usually valid for RS-485 networks, which can have multiple MODBUS devices connected to the same network.
Note: Consult your device documentation to find out how to validate/change the address of your device.

VISA resource name specifies the resource to be opened. This control also specifies the session and class. For more information about VISA resource names, refer to the NI-VISA Help.
Starting Address is the first address location of the holding register to read. This address is sometimes referred to as offset. Refer to your device's documentation to find the holding register address mapping.
Be sure to specify the correct address in the MODBUS device configuration software for the register you want to use in LabVIEW. 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 is calculated by subtracting 1 from the register name that you specify in the master device configuration software. The MODBUS LabVIEW library expects register addresses, not register names, so you may need to subtract 1 from the address you defined in the MODBUS device configuration software. For example, a register name defined as 2 in a MODBUS configuration device translates to register address 1 in the Holding Registers table of the LabVIEW MODBUS library, as shown below.
MODBUS DeviceHolding Register Name = 2
LabVIEWHolding Register Address = 1
Quantity represents how many holding registers to read from the slave. The VI returns the holding register at Starting Address and each following holding register up to Quantity.For example, if Starting Address is 0 and Quantity is 4, the VI reads holding registers from the slave at address 0, 1, 2, and 3.
Timeout specifies the maximum time period, in milliseconds, that the VI waits for the slave's response before throwing an error.
error in describes error conditions that occur before this VI or function runs. The default is no error. If an error occurred before this VI or function runs, the VI or function passes the error in value to error out. If an error occurs while this VI or function runs, the VI or function runs normally and sets its own error status in error out. Use the Simple Error Handler or General Error Handler VIs to display the description of the error code. Use error in and error out to check errors and to specify execution order by wiring error out from one node to error in of the next node.
status is TRUE (X) if an error occurred before this VI or function ran or FALSE (checkmark) to indicate a warning or that no error occurred before this VI or function ran. The default is FALSE.
code is the error or warning code. The default is 0. If status is TRUE, code is a negative error code. If status is FALSE, code is 0 or a warning code.
source identifies where an error occurred. The source string includes the name of the VI that produced the error, what inputs are in error, and how to eliminate the error.
VISA resource name out is the resource to which a VISA session is opened and its class. The class matches that of the VISA resource name input. For more information about VISA resource names, refer to the NI-VISA Help.
Holding Registers represents the data read from the slave (array of U16).
Exception Code is nonzero if a MODBUS error has occurred. The exception code mapping is as follows:
0No error
1Illegal function
2Illegal data address
3Illegal data value
4Failure in associated device
5Acknowledge
6Busy, rejected message
7NAK—Negative acknowledge
For more information about these error codes, refer to your device's documentation.
error out contains error information. If error in indicates that an error occurred before this VI or function ran, error out contains the same error information. Otherwise, it describes the error status that this VI or function produces. Right-click the error out front panel indicator and select Explain Error from the shortcut menu for more information about the error.
status is TRUE (X) if an error occurred or FALSE (checkmark) to indicate a warning or that no error occurred.
code is the error or warning code. If status is TRUE, code is a nonzero error code. If status is FALSE, code is 0 or a warning code.
source describes the origin of the error or warning and is, in most cases, the name of the VI or function that produced the error or warning.
Labview Modbus Serial Example

Oct 10, 2019 The MODBUS library is a free, downloadable set of Virtual Instruments (VIs) that provide Modbus communication from any standard Ethernet or serial port. The LabVIEW library implements the Modbus protocol in software and offers both master and slave functionality.

In this post, I will try to describe an approach of LabVIEW/Arduino communication using Modbus protocol. The motivation is to read temperatures from multiple Ds18b20 temperature sensors, however be able to modify easily the code to control relays, read analog inputs etc. I like Ds18b20 sensors from several reason: sufficient accuracy and time constant for many applications, Onewire bus (for one Pt100 or thermocouple is needed a dedicated channel or multiplexer) and good price.

The easiest approach from the point of view of Arduino programming is usage of Serial monitor and read the data with LabVIEW VI Visa node, since Arduino/Ds18b20 measurement is well documented. A tutorial on the topic of LabVIEW/Arduino data transfer through serial is published on Instructables [1]. I experiences some issues with reading data from serial monitor, LabVIEW sometimes read a blank row and I had to add some buffer and data filter and the VI started to expand to a messy mixture of nodes.

Second approach can be based on LabVIEW plugin LINX [2]. With LINX you can very simply read or write values on digital or analog pins. LINX add-on allows you also to include Arduino libraries using Custom commands, but I found this approach quit complicated even though reading data from Ds18b20 was discussed in this post [3], where is also example of the LabVIEW VI.

After some experiments with mentioned approaches, I have started to test the communication via serial protocol called Modbus. Modbus is a serial communications protocol originally published by Modicon in 1979 for use with its programmable logic controllers (PLCs). The data can be transferred through serial lines RS-485, RS-232-C, RS-422, etc with Modbus RTU or via ethernet using Modbus TCP, but there are several other versions. On Arduino, the USB port can be used for data transfer with Modbus RTU. Modbus devices can be master or slave and on one line can be up to 32 slaves. The data stream is controlled by master device, but the information can be transferred both ways: from slave to master or from master to slave.

There can be found several Modbus libraries for Arduino and I selected this one [4]. After installling the library to Arduino IDE, the library can be imported to a new sketch by following code

I will demonstrate the use of modbus library for reading temperature from 3 Ds18b20 sensors. I have them connected to pin 4. To be able to read the data from the sensors, two another libraries is necesseary to import

Modbus rtu example

Full code

LabVIEW side:

I am using Modbus API library [5], possible to install via VI Package Manager. Reading data from Arduino through USB is then pretty simple, only two blocks are necessary: Create Serial Master and Read Holding Registers. Full LabVIEW code is in following image.

Modbus Programming Examples

References

Labview Modbus Library

Note: Since free WordPress.com version does not allow you to install plugins, I used online code highlitghter hilite.me.