Click here to Skip to main content
13,357,363 members (61,770 online)
Click here to Skip to main content
Add your own
alternative version


148 bookmarked
Posted 6 Nov 2006

Modbus TCP class

, 23 May 2014
Rate this:
Please Sign up or sign in to vote.
A Modbus TCP class.


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


I don't want to explain how the modbus protocol works in detail, because there is plenty of information around.

Using the code

All commands are sent in synchronous or asynchronous mode. If a value is accessed in synchronous mode, the program will stop and wait for the slave to respond. If the slave does not answer within a specified time, a timeout exception is called. The class uses multi-threading for both synchronous and asynchronous access. The class opens two communication channels for each slave. This is necessary because the synchronous thread has to wait for a command to finish. This would block the asynchronous connection. The class directory contains a help file (ModbusClassTCP\doc) with detailed information for every function.

A sample application is included in the package that shows the basic features. It uses asynchronous mode for all function calls. The sample has some additional code to make the result handling more comfortable.

Points of Interest

Remember that a lot of Modbus clients use a watchdog telegram to make sure the master is still active. If you don’t call this watchdog within a specified time, the slave will close the connection. This feature is used to avoid unused open ports when the connection is interrupted.


Version 3.3 (6/14/2013)
- Fixed: ID high and low byte swapped
- Added: Modbus paramater UNIT added to function calls
Version 3.2
First release


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


About the Author

Stephan Stricker
Systems Engineer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionNo Documentation Pin
Member 1102212224-Apr-15 3:21
memberMember 1102212224-Apr-15 3:21 
AnswerRe: No Documentation Pin
Stephan Stricker19-May-15 11:40
professionalStephan Stricker19-May-15 11:40 
QuestionModify project to make it continuous reading Pin
tonzurcode18-Apr-15 6:12
membertonzurcode18-Apr-15 6:12 
AnswerRe: Modify project to make it continuous reading Pin
Stephan Stricker19-May-15 11:32
professionalStephan Stricker19-May-15 11:32 
QuestionVielen Danke fur die Programm! Pin
fullman20089-Apr-15 12:03
memberfullman20089-Apr-15 12:03 
QuestionVery good and very useful article...but unable to see data Pin
Jaywant.Kulkarni27-Feb-15 1:51
memberJaywant.Kulkarni27-Feb-15 1:51 
QuestionReading registers Pin
Member 97548706-Nov-14 12:15
memberMember 97548706-Nov-14 12:15 
AnswerRe: Reading registers Pin
Garth J Lancaster6-Nov-14 17:21
memberGarth J Lancaster6-Nov-14 17:21 
AnswerRe: Reading registers Pin
Member 975487017-Nov-14 8:42
memberMember 975487017-Nov-14 8:42 
Questionerror in connection after a period of time Pin
Member 104508328-Oct-14 21:26
memberMember 104508328-Oct-14 21:26 
QuestionRe: error in connection after a period of time Pin
Member 1194587016-Nov-15 22:12
memberMember 1194587016-Nov-15 22:12 
Suggestion5 Star project, thanks. Pin
zeytin7-Sep-14 7:01
memberzeytin7-Sep-14 7:01 
QuestionHow to communicate with many devices at the same time? Pin
Member 109289987-Aug-14 23:31
memberMember 109289987-Aug-14 23:31 
QuestionHow Can I give command to any hardware unit using modbus ??? Pin
Pdeveloper29-Jul-14 2:04
memberPdeveloper29-Jul-14 2:04 
QuestionBug in CreateReadHeader code Pin
Member 861764024-Jun-14 12:38
memberMember 861764024-Jun-14 12:38 
QuestionAsync timeout ,byte swapping and a Modbus slave project Pin
Member 861764024-Jun-14 8:29
memberMember 861764024-Jun-14 8:29 
QuestionIssues with using this code in C++ project Pin
Mike Grove19-Jun-14 1:19
memberMike Grove19-Jun-14 1:19 
QuestionMessage Transaction Id Pin
khalprin27-May-14 5:17
memberkhalprin27-May-14 5:17 
BugHow to wait the end of ModBus readcoil function Pin
Rhyzko3-Apr-14 6:37
memberRhyzko3-Apr-14 6:37 
GeneralRe: How to wait the end of ModBus readcoil function Pin
Stephan Stricker23-May-14 4:57
professionalStephan Stricker23-May-14 4:57 
QuestionModbus Slave application Pin
romasimeiz26-Feb-14 4:25
memberromasimeiz26-Feb-14 4:25 
AnswerRe: Modbus Slave application Pin
Stephan Stricker27-Feb-14 8:59
memberStephan Stricker27-Feb-14 8:59 
GeneralRe: Modbus Slave application Pin
nasher21927-Jul-14 23:31
membernasher21927-Jul-14 23:31 
QuestionHow to download??? Pin
rquirch10-Jan-14 11:40
memberrquirch10-Jan-14 11:40 
QuestionModbus TCP Class for 2008 Pin
The Real John Richards10-Jan-14 1:02
memberThe Real John Richards10-Jan-14 1:02 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 23 May 2014
Article Copyright 2006 by Stephan Stricker
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid