Click here to Skip to main content
11,432,427 members (66,866 online)
Click here to Skip to main content

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 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 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 asynchonous connection. The class directory contains a help file (ModbusClassTCP\doc) with detailed information for every function.

An example application that shows the basic features is included in the package. It demos all functions in asynchronous mode. The synchronous functions are very similar. The sample has some additional code to make the results and handling more comfortable.

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.


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
Follow on   LinkedIn

Comments and Discussions

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 [modified] 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 and ModbusTCP class Pin
enfrost15-Jul-13 9:57
memberenfrost15-Jul-13 9:57 
AnswerRe: and ModbusTCP class Pin
Stephan Stricker15-Jul-13 12:10
memberStephan Stricker15-Jul-13 12:10 
QuestionMore simple sample, please!) Pin
Member 101198262-Jul-13 2:57
memberMember 101198262-Jul-13 2:57 
NewsModbus TCP Sample ( [modified] Pin
hadihidayat2-Jul-13 1:36
memberhadihidayat2-Jul-13 1:36 
QuestionNot able to read from Modbus TCP simulator Pin
MayurShah.in28-Jun-13 22:51
memberMayurShah.in28-Jun-13 22:51 
AnswerRe: Not able to read from Modbus TCP simulator Pin
Stephan Stricker29-Jun-13 21:55
memberStephan Stricker29-Jun-13 21:55 
QuestionModbus RTU over TCP Pin
JosepLluis17-Jun-13 11:18
memberJosepLluis17-Jun-13 11:18 
AnswerRe: Modbus RTU over TCP Pin
Stephan Stricker17-Jun-13 14:33
memberStephan Stricker17-Jun-13 14:33 
GeneralRe: Modbus RTU over TCP Pin
JosepLluis18-Jun-13 3:31
memberJosepLluis18-Jun-13 3:31 
GeneralRe: Modbus RTU over TCP Pin
kselvia@eprod.com22-Jun-13 0:21
memberkselvia@eprod.com22-Jun-13 0:21 
QuestionSlave_ID and Slave_Address Pin
SBerube13-Jun-13 3:04
memberSBerube13-Jun-13 3:04 
AnswerRe: Slave_ID and Slave_Address Pin
Stephan Stricker14-Jun-13 4:51
memberStephan Stricker14-Jun-13 4:51 
Questionvalues of message Pin
monkar22-May-13 8:08
membermonkar22-May-13 8:08 
AnswerRe: values of message Pin
Stephan Stricker22-May-13 8:23
memberStephan Stricker22-May-13 8:23 
GeneralRe: values of message Pin
monkar23-May-13 0:57
membermonkar23-May-13 0:57 
GeneralRe: values of message Pin
Stephan Stricker23-May-13 3:18
memberStephan Stricker23-May-13 3:18 
GeneralRe: values of message Pin
monkar23-May-13 7:02
membermonkar23-May-13 7:02 
GeneralRe: values of message Pin
Stephan Stricker24-May-13 9:54
memberStephan Stricker24-May-13 9:54 
QuestionRe: values of message [modified] Pin
vinnie_j26-Jul-13 4:11
membervinnie_j26-Jul-13 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web01 | 2.8.150428.2 | Last Updated 23 May 2014
Article Copyright 2006 by Stephan Stricker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid