Click here to Skip to main content
Click here to Skip to main content

Modbus TCP class

By , 5 Nov 2006
 

Sample screenshot

Introduction

This article explains a Modbus TCP common driver class. This class implements a modbus TCP master driver.

It supports the following commands:

  • Read coils
  • Read discrete inputs
  • Write single coil
  • Write multiple cooils
  • Read holding register
  • Read input register
  • Write single register
  • Write multiple register

Background

I haven’t explained how the modbus protocol works in detail, because there is plenty of information around.

Using the code

All commands can be sent in synchronous or asynchronous mode. If a value is accessed in synchronous mode, the program will stop and wait for the slave to response. If the slave doesn't answer within a specified time, a timeout exception is called. The class uses multi-threading for both synchronous and asynchronous access. The class creates two communication lines for each slave. This is necessary because the synchronous thread has to wait for a previous command to finish. This would block the asynchonous connection if we don't use a separate line. The class directory contains a help file (ModbusClassTCP\doc) with detailed information for every function.

There is an example application included that shows the basic features. It contains all functions in asynchronous mode. The synchronous functions are similar. The sample has some additional code to make the results and handling more comfortable. It has a timer included that generates a watchdog telegram for the Modbus slave I use for testing. You can disable this watchdog, or modify it for your needs.

Points of Interest

Remember that a lot of Modbus clients use a watchdog telegram to make sure the master is still alive. If you don’t call this watchdog within a specified time, the slave will no longer set any output.

History

  • Version 1.00

    This is the first release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Stephan Stricker
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionModbus says error: Illegal function!memberxeroxvrey6 Feb '12 - 5:57 
Hello, I get this error: (Modbus says error: Illegal function!) When working with a simulator called "Modbus Slave", but if I taste with another called "mod_RSsim" works perfectly, the same error I get when I connect to a device physical Modbus TCP. Is it a compatibility issue with some other Modbus protocol?
AnswerRe: Modbus says error: Illegal function!memberStephan Stricker6 Feb '12 - 6:07 
The error means that you try to call a function code that is not supported by the slave or you try to call a function to an address that does not allow this function. This error comes from the slave.
GeneralRe: Modbus says error: Illegal function!memberxeroxvrey6 Feb '12 - 10:27 
Thanks for the reply, I am using the sample application (Sample Modbus Common), without modification, then I don't think the error is to call a function or bad register, because the application properly written on the register configured by pressing the "Write multiple register - Write single register - Write multiple coils - Write single Coil" but no function of reading, in any of them I get this error. As I mentioned before this only happens to me with the simulator (Modbus Slave[^]) and the actual device, with other simulator (mod_RSsim) all functions are working properly. What can be?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 6 Nov 2006
Article Copyright 2006 by Stephan Stricker
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid