CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Internet & Networking >> Network Protocols >> Serial Communications


MODBUS Serial RTU + TCP/IP Simulator
Rating:

Conrad Braam (view profile)
July 26, 2007

Environment:  Win32, VC6–98/NT4/Win2K/XP, should run on '95 and others, too.

Go to page: 1  2  Next

Introduction

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


(continued)



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.
Compiles with Visual C++ 6.0, and runs on Windows 98/NT4/2000/XP and probably 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) that it implements; it is only the basic or commonly used functions.

History

This simulator actually originated whiile 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.

I am working on a new comm. library intended to give a better starting point for cut-and-pasters; at the moment, you have to get past a lot of hard-wiring in the program. I hope to create a cleaner layered and wrappered version with a simpler demo application, free, stru!

Modbus Compatibility Classes

To get a really high score when it comes to compatibility with different devices, you want lots of functionality, but miminal 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.

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.

About the Author
A die-hard C++ guru, Conrad has no exciting outdoor hobbies, does not jump out of aeroplanes, nor look good. Studied in Elect. Engineering and dissrespectfull of anything that cannot be dissasembled (x86 was my first language). Currently hard at work in the process industry, I am looking to turn my hand to some espionage. Communications is my game. Web www.plcsimulator.org Blog www.zaphodikusrealm.blogspot.com CURRENTLY HUNTING FOR WORK IN CAMBRIDGE!

Go to page: 1  2  Next

Downloads

  • Legacymod_simSource.zip - Origional source (really old version)
  • mod_RSsim76exe.zip - Demo executable v7.6
  • mod_RSsim7_4b.zip - mode_RsSim V 7.4b source
  • mylib.zip - Source : (library needed 2 compile)

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed







  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    Writing 1 coil with 0xF function actually changes 2 coils - Alex_S (10/13/2008)
    Exception Responses Error - C266 (05/13/2008)
    Modbus - Stig Arild Jensen (02/12/2008)
    Help me out - vinaik353 (01/31/2008)
    Modbus - TxOs (12/06/2007)

    View All Comments
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Whitepapers and eBooks

    Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
    IBM Solutions Brief: Go Green With IBM System xTM And Intel
    HP eBook: Simplifying SQL Server Management
    IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
    Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
    Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
    Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
    Intel PDF: Quad-Core Impacts More Than the Data Center
    Intel PDF: Virtualization Delivers Data Center Efficiency
    Go Parallel Article: PDC 2008 in Review
    Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
    Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
    Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
      PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
    Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
    Go Parallel Article: Q&A with a TBB Junkie
    IBM Whitepaper: Innovative Collaboration to Advance Your Business
    Internet.com eBook: Real Life Rails
    IBM eBook: The Pros and Cons of Outsourcing
    Internet.com eBook: Best Practices for Developing a Web Site
    IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
    Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
    IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
    Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
    HP eBook: Guide to Storage Networking
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES