LabWindows/CVI User Group Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

Modbus RTU programming over RS232 in CVI

Overview

As several users post questions on handling devices using Modbus, I decided to create this sample based on my experience in interfacing with such devices over the years. It is mainly based on programming examples and documentation found on Modicon site and on devices vendor sites; I collected and adapted all this material for use in CVI environment. 

 

Description

The framework includes function to create CRC tables and add CRC to messages to send to the device, as well as a function to check the CRC received from the device. Functions to read/write to the device are also included together with some useful function to handle int/float data type in communications, especially in case the slave device expects a different byte order with respect to PC memory. Some error checking is added: RS232 errors are reported from low-level functions to caller; no-return-message or wrong CRC received from the device are also decoded to the appropriate RS232 error constants.

 

This framework implements Modbus RTU, which is quite similar but not exactly the same as Modbus ASCII. Based on the attached Modicon reference, though, converting this framework to handle Modbus ASCII should not be too difficult. It also implements communications via RS232 port only: I have had no experience on Modbus over Ethernet till now so I couldn't cover this case.

 

This code is to be intended as a communications framework only: it helps you writing a message or reading an answer from a device. Memory addresses where to write/read, what to write and how to decode device answers depend on how every producer has organized its device. Device documentation should clarify all this.

 

Hardware and Software Requirements

No special hardware is needed to run this code, nevertheless it makes use of serial communications so at least one serial port must be present in the system. 

ModbusFramework is pure code intended to be integrated in an existing project; it uses only CVI standard functions: I have been using it from several years and in every single CVI release from 6.0 on.

The example program ModbusTest is developed in CVI2009 SP1. A downgraded release for CVI7.1 is added too (it is actually the same code but the UIR file was saved for that version)

 

Steps to Implement or Execute Code

The example program ModbusTest can be executed as-is: choose the communications speed and the serial port to use to start the application; at this point you can set the emory address and number of registers to query and press Query slave button to send the message and wait for the response: both message and response are shown in the boxes at the bottom of the window listed in hex format.

To simply send a message to attached unit use Write to slave button.

 

ModbusFramework files can be added to a project in order to use the included functions. No additional library is needed.

 

Additional Information or References

The attached document includes:

  • PI_MBUS_300.pdf    The Modbus Protocol Reference Guide downloaded from Modicon site
  • ModbusFramework   The low level functions used to communicate with the devices
  • ModbusTest    A sample project that uses ModbusFramework to communicate


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Download All
Comments
Etesam23
Member
Member
on

Hi  

 

RobertoBozzolo
Proven Zealot
Proven Zealot
on

This error derives from CallCtrlCallback function not being recognized by CVI. That function is included with the Programmer's Toolbox, a library that ships with CVI and that I use extensively in my activity. You can either load it as an instrument (in the Instrument menu) or as a library (Library >> Customize menu function). The tool is located in <CVI folder>\toolslib\toolbox\toolbox.fp.

If I may add, the toolbox includes several useful function that can save you a lot of work: I suggest you take some time to look at the instrument tree and its functions.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?