Click here to Skip to main content
11,719,753 members (92,036 online)
Click here to Skip to main content

Modbus TCP class

, 23 May 2014 CPOL 333.2K 38.7K 131
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 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.

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

You may also be interested in...

Comments and Discussions

 
QuestionReading registers Pin
Member 97548706-Nov-14 11:15
memberMember 97548706-Nov-14 11:15 
AnswerRe: Reading registers Pin
Garth J Lancaster6-Nov-14 16:21
memberGarth J Lancaster6-Nov-14 16:21 
AnswerRe: Reading registers Pin
Member 975487017-Nov-14 7:42
memberMember 975487017-Nov-14 7:42 
Questionerror in connection after a period of time Pin
Member 104508328-Oct-14 20:26
memberMember 104508328-Oct-14 20:26 
Suggestion5 Star project, thanks. Pin
zeytin7-Sep-14 6:01
memberzeytin7-Sep-14 6:01 
QuestionHow to communicate with many devices at the same time? Pin
Member 109289987-Aug-14 22:31
memberMember 109289987-Aug-14 22:31 
QuestionHow Can I give command to any hardware unit using modbus ??? Pin
Pdeveloper29-Jul-14 1:04
memberPdeveloper29-Jul-14 1:04 
QuestionBug in CreateReadHeader code Pin
Member 861764024-Jun-14 11:38
memberMember 861764024-Jun-14 11:38 
QuestionAsync timeout ,byte swapping and a Modbus slave project Pin
Member 861764024-Jun-14 7:29
memberMember 861764024-Jun-14 7:29 
QuestionIssues with using this code in C++ project Pin
Mike Grove19-Jun-14 0:19
memberMike Grove19-Jun-14 0:19 
QuestionMessage Transaction Id Pin
khalprin27-May-14 4:17
memberkhalprin27-May-14 4:17 
BugHow to wait the end of ModBus readcoil function Pin
Rhyzko3-Apr-14 5:37
memberRhyzko3-Apr-14 5:37 
GeneralRe: How to wait the end of ModBus readcoil function Pin
Stephan Stricker23-May-14 3:57
professionalStephan Stricker23-May-14 3:57 
QuestionModbus Slave application Pin
romasimeiz26-Feb-14 3:25
memberromasimeiz26-Feb-14 3:25 
AnswerRe: Modbus Slave application Pin
Stephan Stricker27-Feb-14 7:59
memberStephan Stricker27-Feb-14 7:59 
GeneralRe: Modbus Slave application Pin
nasher21927-Jul-14 22:31
membernasher21927-Jul-14 22:31 
QuestionHow to download??? Pin
rquirch10-Jan-14 10:40
memberrquirch10-Jan-14 10:40 
QuestionModbus TCP Class for VB.net 2008 Pin
The Real John Richards10-Jan-14 0:02
memberThe Real John Richards10-Jan-14 0:02 
AnswerRe: Modbus TCP Class for VB.net 2008 Pin
Stephan Stricker7-Feb-14 8:46
memberStephan Stricker7-Feb-14 8:46 
QuestionThanks! Pin
Member 92730099-Jan-14 4:04
memberMember 92730099-Jan-14 4:04 
QuestionSimple way to run class from console Pin
TadejP8122-Nov-13 11:41
memberTadejP8122-Nov-13 11:41 
QuestionVS ?? Pin
Member 103773933-Nov-13 5:09
memberMember 103773933-Nov-13 5:09 
AnswerRe: VS ?? Pin
Flavi114-Nov-13 5:44
memberFlavi114-Nov-13 5:44 
NewsModbus Framework for .NET Pin
Flavi126-Oct-13 1:29
memberFlavi126-Oct-13 1:29 
GeneralRe: Modbus Framework for .NET Pin
Member 399685331-Oct-13 6:41
memberMember 399685331-Oct-13 6:41 

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