MODBUS Serial RTU + TCP/IP Simulator

Introduction

Simulator for Modbus slaves over RS232/RS485 and TCP/IP amongst other protocols.

Compiles with Visual C++ 6.0, VS 9.0 and runs on Windows 98/NT4/2000/XP and 95/Me, too.



Click here for a larger image.

Purpose

This simulator was originally written to allow the testing of a MODBUS RTU serial driver without having to get 254 little MODBUS PLCs into one office. It, however, grew and is growing to support other protocols (open-mbus TCP/IP, Allen-Bradly DF1 slave+master) as well. It is not, by any means, a full implementation of the protocol(s) implemented; For Modbus I have all the commonly used functions and some advanced ones.

Homepage

This project's own homepage is now www.plcsimulator.org.

History

This simulator actually originated while I was trying to write a stable socket wrapper of the Windows Sockets API, and the Texas Instruments TCP/IP protocol, but that is another story. It then was re-written for Modbus RTU, and I added TCP/IP according to the OpenMBUS spec. Next, I added Allen-Bradley DF1, and have a specialized modification of a DF1 MASTER, called JOY, which is highly specific.

This program was initially written over a few weekends, and now that I no longer work much with comms., it has become a labor of love. I Still fix bugs if you explain them nicely. Although testing is difficult because I have no equipment, I even add features from time to time.

Modbus surpasses the limitations in other programs I have found in the past by:

  • Allowing it to simulate more than one RTU at once.
  • Allowing the editing and displaying of all the registers.
  • Allowing the scanning of registers not actually in the display.
  • Running on >1 comm ports. (You have to run two copies of the program.) You need to run the first copy; then, when you run it again, it defaults to use the next free RS-232 port.
  • A way to disable just certain stations. (Is done now; just click on the boxes at the bottom.)
  • Load and save values you type into each register (the file is saved in the same folder) as well as simulate changes in the values using scripting!
  • A comm debugger screen (Hotkey Ctrl+C.).
  • Show statuses for all stations, not just the first couple.

Missing

  • Nothing really. I would have liked to have written this thing in a more generic fashion (time does not permit) to support as many protocols as I liked.
  • Maybe MODBUS ASCII protocol support.
  • Any suggestions, please send.

Compiling It

I'm pretty sure it does compile nicely. Linking is a bit hairy if you are missing the lib-file "mylib.lib". Please download it as a separate zip file below, and compile. Then, set the linker options appropriately to point to it. Not all of this code is production code, and as such is not fully tested. Use it at your own risk.

Code updates are available in my Sourceforge subversion repository.

svn co https://modrssim.svn.sourceforge.net/svnroot/modrssim modrssim

Modbus Compatibility Classes

To get a really high score when it comes to compatibility with different devices, you want lots of functionality, but minimal code—especially if RAM is limited.

Because the source for this project is open, I imagine it will get many bug-reports and fixes at low cost, and add stability as it ages.

Function Code Meaning
Class 0: the bare minumum
03 hex Read Multiple Registers
10 hex Write Multiple Registers
Class 1: ideal
01 hex Read Coils
02 hex Read Inputs
04 hex Read Input Registers
05 hex Write Coil
06 hex Write Single Register
07 hex Read Exception Status
Class 2: almost overkill
0F hex Force Multiple Coils
16 hex Mask-write Register
17 hex Read,Write Multiple registers

Nuggets

There are 13 new classes and a dozen useful utility routines in here.

Running It

I am not going to give a full feature and User-Interface run-down because it has built-in/linked-in help; just press F1. Here are the basics, which most of us will get in five minutes anyway.

  • Launch the app; it should open the first free RS-232 port. To select Ethernet, click in the drop-list to the right. Click on the Port Settings..." button to change any settings. Settings are saved to the Registry.



    Click here for a larger image.

    The "responsiveness" setting changes the delay in the program before it responds to a valid request.
  • Hook up a cable (just a 2, 3, and 7 crossed; 4 and 5 looped; and 6, 8, and 20 looped should be fine; hardware handshaking is not my strong side). A diagram is in the help file.
  • Click on Animation Settings... to change the color of the cute indicators at the bottom, and set up some simple simulation data changes.
  • The cute boxes at the bottom work like indicators to show which stations are active; the gray outline of each box will change color (they turn off after five seconds), and clicking them disables each station (only 60 are shown at once; please scroll). The green background turns red to show disabled.
  • Double-click on the list-view over a register to edit its value. Double-clicking a digital I/O just toggles it.
  • Zero values will clear all values (also, the digitals become 0).
  • Load will not work unless you have saved first.
  • Save will save all the register values to a flat binary file (don't edit it; I can't) called 'modbus.dat', in the current folder. This is nice to allow you to fill the registers with values, and then come back later to the same setup. If you use different file versions or sizes, you will have a big problem, so do not bother me with this. Create a fix for it and mail the code to me.
  • Switch between the Hex and Decimal views. (Addresses also come in hex; this is just me fiddling.)
  • New option for starting with all devices offline added (by request).
  • Report any problems only after trying for at least a day to figure out what you got wrong because this program does work!
  • The program has a demo mode. Just post on the article forum below, or send a message, and I will send you a free unlock code.

In the words of Paul DiLascia: If this code works, it was written by Conrad Braam. If not, I don't know who wrote it.
I get lots of people asking, How do I select TCP/IP? Well, that is easy; find the drop-list control near the right, and pick TCP/IP from that. All toolbar buttons change their functions as a result.

See also the project website: www.plcsimulator.org

MODBUS Serial RTU + TCP/IP Simulator

The Library Within

The project got totally huge in Version 5 and I had to break it up into two parts; primarily to make maintenance easier, and make the code easier to understand. The Program together with the Utility library is too big for one CodeGuru up-load; therefore, I split it into two ZIPs, so remember to download both sources!

The library includes the following classes and functions:

  • Some classes in the library are not used here, but this is the kind of GP lib we all need to use; unused stuff is in italics
  • CADODatabase—Database class by Carlos Antollini
  • CADOCommand—Database util class ..
  • CADOFieldInfo—Database util class ..
  • CADOCommand—Database util class ..
  • CADOParameter—Database util class ..
  • CADORecordset—Database util class ..
  • CDragSizerBmp—Transparent BMP, misc sources
  • CResizer—Window size class by Dmitry Kochin <dco@mail.ru>
  • CStaticLink—Web-link Control by Paul DiLascia
  • CColorStatic—Control by misc sources
  • DRegKey—Registry class by Conrad Braam
  • CSampler—Color-display box by Conrad

Functions by Conrad Braam; here are just some of them:

  • CalcCRC
  • GetLongComPortName (UNC names for ports if > COM9)
  • FillSerialCBox
  • PortInUse

Some classes and so forth in the app include: enumerate your machine's Serial ports, Easter-egg screen, also showing owner-data ListView Control using a callback; this is the only way to get display performance. Yep, there is more; I also show Alpha-blending. Right-click on the title-bar (the top blue bit), and the program will fade, allowing you to float it over other windows. If the menu does not show something like "Alpha blend," you are not running a O/S that supports that API.

Other nifty things include a scrolling credits screen in the About box, and a splash screen, as well as a very simple timed-demo/license that any programmer can work out the key for, or just mail me and I will send you a free key, or just recompile the checking away yourself!

More (FAQs)

  • Q: What is a MODBUS RTU?
    A: It's a PLC. If that does not make sense, visit www.modicon.com.
  • Please use all of my source code. It is not the best, but it works for me, and is well commented (hence the size). And, please copy the unmodified executable as often as you like. Don't just re-compile it with your name on it, silly.
  • I also have an Ethernet TCP/IP version of this program (it is an earlier and less fancy version). This app supercedes it.
  • This is also a example of using VBA scripting to automate your application (read my online help file), and that is why the simulator requires Internet Explorer 6.0.
  • I also show how to put HTML and GIFs into resources, which the program writes out to disk temp files at run-time (when you press F1). This means no external files are needed for a help file. The technique hurts load times a bit, but definitely cuts out install problems; in other words, no install needed.
  • I was a programmer at Adroit Technologies South Africa when it started. This compiled program is not copyrighted and can be freely distributed as long as you stick to the CodeGuru site source re-use guidelines. Mainly, the source is a combination of an Open SDK for drivers released by Adroit, my own weekend hacking, and various credited sources; please use it, but do not republish it (source) without my permission.
  • This code leverages a lot of IP (Intellectual Property), and some of the code is years old, with thousands of users in 24/7/365-days environments, so some (but not all) of this is industrial-strength already.
  • Visit the place I worked, http://www.adroit.co.za, or add a post below, and I will get back to you. My e-mail is hotmail, so the adroit.co.za address has stopped working.

Credits

Some thanks are also deserved to people who sent me fixes, or analysed bugs.

  • HF: Hirofumi Fudoudou
  • Patrick SOFORCADA
  • BMG: Brett Ginsberg
  • AP: Alvaro Palma
  • Scott Alexander: NModbus in C#
  • ...and more.

News

You may need to log in to the site to download version 8.12 from the links below. If you do not want to, but are prepared to wait until I get the request, try sending mail to my hotmail.com address, which is zaphodikus (at domain-name.com), but you may wait a day.

I have a customizable GUI now, it is not a GUI, It utilizes your browser, HTML skills required, and whatever you can throw at it... watch this space.

Another feature up-coming on the requests list is a communications logger to log all the comms message in and out to a file.

Latest source code is available from sourceforge



About the Author

Conrad Braam

A die-hard C++ guru, Conrad has no exciting outdoor hobbies, does not jump out of airplanes, nor look good. Studied in Elect. Engineering and disrespectful of anything that cannot be disassembled in any form (x86 was my first language). Communications is my game. Web www.plcsimulator.org CURRENTLY HUNTING FOR WORK IN CAMBRIDGE!

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join Seagate Cloud …

  • Agile development principles have gone from something used only by cutting-edge teams to a mainstream approach used by teams large and small. If you're not using agile methods already though, or if you've only been exposed to agile on small projects here and there, you may wonder if agile can ever work in your environment. Read this eBook to learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds