Click here to Skip to main content
13,595,908 members
Click here to Skip to main content
Add your own
alternative version


150 bookmarked
Posted 5 Nov 2006
Licenced CPOL

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

QuestionQuestion on Modbus TCP Pin
shwethasp4-Apr-18 5:50
membershwethasp4-Apr-18 5:50 
QuestionModBus TCP class for C++ or C Pin
Member 1361809311-Jan-18 2:40
memberMember 1361809311-Jan-18 2:40 
AnswerRe: ModBus TCP class for C++ or C Pin
Stephan Stricker12-Jan-18 19:16
memberStephan Stricker12-Jan-18 19:16 
QuestionQuestion On "No Response" Pin
Member 1270979225-Nov-17 16:56
memberMember 1270979225-Nov-17 16:56 
AnswerRe: Question On "No Response" Pin
Stephan Stricker8-Jan-18 21:58
memberStephan Stricker8-Jan-18 21:58 
GeneralRe: Question On "No Response" Pin
Member 1270979218-Feb-18 14:05
memberMember 1270979218-Feb-18 14:05 
QuestionNo connection could be made... Pin
Andrw_S16-Nov-17 5:40
memberAndrw_S16-Nov-17 5:40 
AnswerRe: No connection could be made... Pin
Member 1270979226-Nov-17 14:57
memberMember 1270979226-Nov-17 14:57 
QuestionAccess SQL Database and send to Modbus class Pin
ganesh199616-Aug-17 4:12
memberganesh199616-Aug-17 4:12 
QuestionEnd Modbus connection Pin
ganesh199616-Aug-17 0:15
memberganesh199616-Aug-17 0:15 
QuestionWriteSyncData method problem. Pin
mustafa_ozkn1-Aug-17 2:50
membermustafa_ozkn1-Aug-17 2:50 
QuestionRegarding Main function Pin
Member 1325668213-Jun-17 2:21
memberMember 1325668213-Jun-17 2:21 
QuestionDELTA PLC Pin
Member 1268699815-Aug-16 12:54
memberMember 1268699815-Aug-16 12:54 
Questionwho to assigned the one address? Pin
Member 1264600227-Jul-16 2:42
memberMember 1264600227-Jul-16 2:42 
Questionmoddbus says errror ; illegal data adress! Pin
Member 125196883-Jun-16 0:54
memberMember 125196883-Jun-16 0:54 
AnswerRe: moddbus says errror ; illegal data adress! Pin
Garth J Lancaster3-Jun-16 1:26
professionalGarth J Lancaster3-Jun-16 1:26 
GeneralRe: moddbus says errror ; illegal data adress! Pin
Member 125196883-Jun-16 7:07
memberMember 125196883-Jun-16 7:07 
QuestionModbus Connection Issue, where to look? Pin
bwr53920-Apr-16 9:35
memberbwr53920-Apr-16 9:35 
AnswerSolved: Re: Modbus Connection Issue, where to look? Pin
bwr53920-Apr-16 13:54
memberbwr53920-Apr-16 13:54 
Questionobtaining values with a comma Pin
Member 1233685318-Feb-16 8:11
memberMember 1233685318-Feb-16 8:11 
QuestionConnection lost Problem Pin
megha_p14-Aug-15 0:29
membermegha_p14-Aug-15 0:29 
AnswerRe: Connection lost Problem Pin
Stephan Stricker17-Sep-15 3:02
professionalStephan Stricker17-Sep-15 3:02 
AnswerRe: Connection lost Problem Pin
Robert McCullough9-Jan-16 5:06
memberRobert McCullough9-Jan-16 5:06 
GeneralThanks Pin
Member 92786916-Aug-15 22:58
memberMember 92786916-Aug-15 22:58 
QuestionPolling Specific Address Pin
Member 1097569324-Jul-15 20:56
memberMember 1097569324-Jul-15 20:56 

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 | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 23 May 2014
Article Copyright 2006 by Stephan Stricker
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid