News:

Calling all communications systems experts - please share your knowledge here!

Main Menu

Modbus VB and VBA code examples

Started by Dave Loucks, May 23, 2012, 10:31:46 AM

Previous topic - Next topic

Dave Loucks

If you are interested in developing a Modbus Master application, here are some programs that I wrote that you can extract code from.

Note that VB 6.0 is obsolete, but you might still have a compiler.

VB2010 Express is available as a free download (for evaluation purposes) from Microsoft.  VB2013 is also available.

VB 6.0
http://pps2.com/smf/index.php/topic,12.0.html
This link takes you to a topic discussing the Encorp GPC controller and how I wrote some code to program that controller using Modbus rather than using the Lonworks protocol (which required a converter box).  The VB 6.0 source code is included in that topic.

VB 2010 Express
http://pps2.com/smf/index.php/topic,4.0.html
Here's an example located elsewhere on this board of a program that I wrote to demonstrate INCOM pass-through using Modbus RTU.  Embedded within this code is an example of how to write Modbus commands to read and write tables of registers.  This topic also includes  a link where you can download the Modbus source code I used to create a stand-alone Eaton Automatic Transfer Switch setpoint editor.  Note that while this example is somewhat complex (I wrote code that embedded INCOM messages within Modbus packets which were decoded by a Modbus MINT and transmitted as INCOM messages to an ATC-600 or 800 transfer switch controller), it shows how to write VB2010 code to send and receive Modbus RTU messages via the COM serial ports.

Visual Basic for Applications - VBA (Excel 2010)



http://pps2.com/smf/index.php/topic,29.msg39.html#msg39
VB and VBA have different syntax. Neither of the VB programs above could be imported into Excel's VBA scripting language and work.  So I wrote an Excel 2010 VBA Modbus program pretty much from scratch (including a new CRC-16 algorithm and a new way of interfacing with the PC hardware using Windows API calls -- didn't want to have to load any special driver -- just wanted to use "stock" Excel 2010).  The application I was developing was a tool to support programming Eaton's Modbus MINT (mMINT - a simple protocol converter that translates from the legacy Westinghouse INCOM/IMPACC protocol to Modbus RTU on 485).  That converter had a variety of configuration registers and people needed to resort to using something like MODSCAN to read and write them.  A real pain.  I wrote this to collect the data and write into Excel cells.  From there it was easy to read and edit.  Once done, you would press another button to write the data back to the mMINT.  So if you wanted a template on how to get Excel to talk to the serial ports on your computer just open this VBA program and you'll see how I got VBA talking Modbus!