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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionvalues of messagemembermonkar2hrs 54mins ago 
AnswerRe: values of messagememberStephan Stricker2hrs 39mins ago 
QuestionSiemens S1200memberAndrea Feduzzi14 May '13 - 6:09 
AnswerRe: Siemens S1200memberStephan Stricker2hrs 49mins ago 
GeneralMy vote of 5memberJongchanAhn24 Mar '13 - 15:03 
GeneralRe: My vote of 5memberAndrea Feduzzi14 May '13 - 6:10 
Questionproblem with writing to Siemens PLC holding register with MODBUS TCPmemberMember 941116121 Dec '12 - 9:14 
AnswerRe: problem with writing to Siemens PLC holding register with MODBUS TCPmemberStephan Stricker21 Dec '12 - 22:55 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCPmemberMember 94111612 Jan '13 - 9:16 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCPmemberStephan Stricker3 Jan '13 - 4:12 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCPmemberMember 94111612 Jan '13 - 12:16 
AnswerRe: problem with writing to Siemens PLC holding register with MODBUS TCPmemberAndrea Feduzzi14 May '13 - 6:12 
QuestionCan't Write CoilsmemberKitaMenang22 Nov '12 - 3:08 
AnswerRe: Can't Write CoilsmemberStephan Stricker22 Nov '12 - 3:17 
Questionconvert errormembertomasz25616 Nov '12 - 2:28 
AnswerRe: convert errormemberStephan Stricker18 Nov '12 - 8:44 
QuestionCan't open with free ms toolsmemberchris wedgwood26 Oct '12 - 4:09 
AnswerRe: Can't open with free ms toolsmemberStephan Stricker18 Nov '12 - 8:45 
GeneralRe: Can't open with free ms toolsmemberchris wedgwood18 Nov '12 - 23:11 
QuestionUnable to Read, able to writememberalexamex16 Oct '12 - 6:38 
AnswerRe: Unable to Read, able to writememberStephan Stricker16 Oct '12 - 7:15 
QuestionAny VB.Net synchronous mode examples available?memberMember 90645551 Jul '12 - 2:25 
QuestionIt worksmemberMember 862908814 May '12 - 6:06 
QuestionDisconnects and holding registers value displacementmemberharizhent16 Mar '12 - 4:31 
Hi,
Thank you for shearing this code.
I just compiled your sample code and i'm having few problems...
few seconds after connecting and reading any data from HMI(Beijer iX), program disconnects(connection is lost).
Then i try read holding registers from HMI(40001-40005), the results i get is like that..
http://imageshack.us/photo/my-images/269/deletelt.png/[^]
data is correct but it is misplaced or something like that..
 
thank you.
AnswerRe: Disconnects and holding registers value displacementmemberStephan Stricker20 Mar '12 - 2:51 

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

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