Click here to Skip to main content
13,291,503 members (69,515 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
Product Manager
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionQuestion On "No Response" Pin
Member 1270979225-Nov-17 17:56
memberMember 1270979225-Nov-17 17:56 
QuestionNo connection could be made... Pin
Andrw_S16-Nov-17 6:40
memberAndrw_S16-Nov-17 6:40 
AnswerRe: No connection could be made... Pin
Member 1270979226-Nov-17 15:57
memberMember 1270979226-Nov-17 15:57 
QuestionAccess SQL Database and send to Modbus class Pin
ganesh199616-Aug-17 5:12
memberganesh199616-Aug-17 5:12 
QuestionEnd Modbus connection Pin
ganesh199616-Aug-17 1:15
memberganesh199616-Aug-17 1:15 
QuestionWriteSyncData method problem. Pin
mustafa_ozkn1-Aug-17 3:50
membermustafa_ozkn1-Aug-17 3:50 
QuestionRegarding Main function Pin
Member 1325668213-Jun-17 3:21
memberMember 1325668213-Jun-17 3:21 
QuestionDELTA PLC Pin
Member 1268699815-Aug-16 13:54
memberMember 1268699815-Aug-16 13:54 
Questionwho to assigned the one address? Pin
Member 1264600227-Jul-16 3:42
memberMember 1264600227-Jul-16 3:42 
Questionmoddbus says errror ; illegal data adress! Pin
Member 125196883-Jun-16 1:54
memberMember 125196883-Jun-16 1:54 
AnswerRe: moddbus says errror ; illegal data adress! Pin
Garth J Lancaster3-Jun-16 2:26
professionalGarth J Lancaster3-Jun-16 2:26 
GeneralRe: moddbus says errror ; illegal data adress! Pin
Member 125196883-Jun-16 8:07
memberMember 125196883-Jun-16 8:07 
QuestionModbus Connection Issue, where to look? Pin
bwr53920-Apr-16 10:35
memberbwr53920-Apr-16 10:35 
AnswerSolved: Re: Modbus Connection Issue, where to look? Pin
bwr53920-Apr-16 14:54
memberbwr53920-Apr-16 14:54 
Questionobtaining values with a comma Pin
Member 1233685318-Feb-16 9:11
memberMember 1233685318-Feb-16 9:11 
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 
AnswerRe: Connection lost Problem Pin
Robert McCullough9-Jan-16 6:06
memberRobert McCullough9-Jan-16 6:06 
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 

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
Web03 | 2.8.171207.1 | Last Updated 23 May 2014
Article Copyright 2006 by Stephan Stricker
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid