Click here to Skip to main content
11,492,914 members (60,042 online)
Click here to Skip to main content

Modbus TCP class

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

Introduction

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

Background

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.

History

-----------------------------------------
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

License

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

Share

About the Author

Stephan Stricker
Product Manager
United States United States
No Biography provided
Follow on   LinkedIn

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 [modified] 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 
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 VB.net 2008 Pin
The Real John Richards10-Jan-14 1:02
memberThe Real John Richards10-Jan-14 1:02 
AnswerRe: Modbus TCP Class for VB.net 2008 Pin
Stephan Stricker7-Feb-14 9:46
memberStephan Stricker7-Feb-14 9:46 

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