Example Code

Modbus Communication Reference Example for cRIO

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Hardware

  • CompactRIO Controller
  • NI-9870

Code and Documents

Attachment

Description

Overview


This example ports the existing Modbus Library for LabVIEW to an FPGA-based implementation for use with the cRIO serial modules.

Note: As of NI-RIO 3.6 and NI-Serial 3.7 with LabVIEW 2010, Scan Mode is supported for the NI 9870 and NI 9871 modules. This allows you to access the serial ports directly through VISA calls on the RT controller and use the standard Modbus Library for LabVIEW without the modifications found in this example. See KB 5M7DRSQE: Configuring the NI 987x Modules in Scan Mode for information on how to set up the cRIO for this preferred method.

For NI-RIO 3.5 & NI-Serial 3.6 or earlier

This example, based on the Modbus Library for LabVIEW, permits communication with Modbus devices via C-series serial modules.  Since these modules are not exposed to the controller as COM ports but instead interface directly to the FPGA on cRIO systems, the standard VISA serial commands are not available.  This example replaces all VISA calls in the Modbus library with custom wrappers around FPGA interface code to replicate the same functionality.  The example  includes complete FPGA code to implement a multi-port serial driver personality on the FPGA as well as a sample RT host implementation to demonstrate the usage of the modified library VIs.  

The code can be easily expanded to accommodate more than a single 4-port RS 232 serial module by adding additional RS-232 or RS-485 modules to the project and adding the appropriate cases and I/O nodes to the Read, Write, Bytes at Port, and Config command implementations in the FPGA code.

Please note that this implementation consumes 2 of the 3 available DMA channels on the cRIO device.

The minimum software set required to use this example is:

  • LabVIEW 8.6
  • LabVIEW FPGA Module 8.6
  • LabVIEW Real Time Module 8.6
  • NI-RIO 3.1

The example is written for the following set of hardware:

  • cRIO 9014 RT controller
  • cRIO 9103 FPGA backplane
  • NI 9870 RS-232 serial module

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