Click here to Skip to main content
11,935,031 members (53,811 online)
Click here to Skip to main content
Add your own
alternative version


132 bookmarked

Modbus TCP class

, 23 May 2014 CPOL
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
Product Manager
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionConnection lost Problem Pin
megha_p14-Aug-15 1:29
membermegha_p14-Aug-15 1:29 
AnswerRe: Connection lost Problem Pin
Stephan Stricker17-Sep-15 4:02
professionalStephan Stricker17-Sep-15 4:02 
GeneralThanks Pin
Member 92786916-Aug-15 23:58
memberMember 92786916-Aug-15 23:58 
QuestionPolling Specific Address Pin
Member 1097569324-Jul-15 21:56
memberMember 1097569324-Jul-15 21:56 
AnswerRe: Polling Specific Address Pin
Stephan Stricker17-Sep-15 4:04
professionalStephan Stricker17-Sep-15 4:04 
GeneralMy vote of 5 Pin
Bootking9-Jun-15 9:01
memberBootking9-Jun-15 9:01 
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 
AnswerRe: Modbus TCP Class for 2008 Pin
Stephan Stricker7-Feb-14 9:46
memberStephan Stricker7-Feb-14 9:46 
QuestionThanks! Pin
Member 92730099-Jan-14 5:04
memberMember 92730099-Jan-14 5:04 
QuestionSimple way to run class from console Pin
TadejP8122-Nov-13 12:41
memberTadejP8122-Nov-13 12:41 
QuestionVS ?? Pin
Member 103773933-Nov-13 6:09
memberMember 103773933-Nov-13 6:09 
AnswerRe: VS ?? Pin
Flavi114-Nov-13 6:44
memberFlavi114-Nov-13 6:44 
NewsModbus Framework for .NET Pin
Flavi126-Oct-13 2:29
memberFlavi126-Oct-13 2:29 
GeneralRe: Modbus Framework for .NET Pin
Member 399685331-Oct-13 7:41
memberMember 399685331-Oct-13 7:41 
GeneralRe: Modbus Framework for .NET Pin
Flavi18-Nov-13 2:51
memberFlavi18-Nov-13 2:51 
QuestionIllegal Data Address Pin
softwaremonkey3-Oct-13 4:43
membersoftwaremonkey3-Oct-13 4:43 
AnswerRe: Illegal Data Address Pin
softwaremonkey3-Oct-13 5:16
membersoftwaremonkey3-Oct-13 5:16 
GeneralRe: Illegal Data Address Pin
mehmet_alixx16-Jan-14 8:53
membermehmet_alixx16-Jan-14 8:53 
Questionmodbus message response is not receiving in same order Pin
Uma Mahes17-Sep-13 21:10
memberUma Mahes17-Sep-13 21:10 
AnswerRe: modbus message response is not receiving in same order Pin
Stephan Stricker18-Sep-13 4:30
memberStephan Stricker18-Sep-13 4:30 
GeneralRe: modbus message response is not receiving in same order Pin
Uma Mahes21-Jan-14 2:48
memberUma Mahes21-Jan-14 2:48 
SuggestionCan not read/write Address more than 32767. Edited conversion Pin
hadihidayat9-Sep-13 16:52
memberhadihidayat9-Sep-13 16:52 
GeneralMy vote of 5 Pin
陳志杰25-Aug-13 18:49
member陳志杰25-Aug-13 18:49 
SuggestionSome improvements necessary Pin
muharrem27-Jul-13 14:17
membermuharrem27-Jul-13 14:17 
GeneralRe: Some improvements necessary Pin
Stephan Stricker29-Jul-13 8:10
memberStephan Stricker29-Jul-13 8:10 
GeneralRe: Some improvements necessary Pin
muharrem30-Jul-13 5:51
membermuharrem30-Jul-13 5:51 

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.

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