Click here to Skip to main content
11,583,484 members (65,416 online)
Click here to Skip to main content

Modbus TCP class

, 23 May 2014 CPOL 316.7K 37.2K 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 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

You may also be interested in...

Comments and Discussions

 
QuestionCan't open with free ms tools Pin
chris wedgwood26-Oct-12 4:09
memberchris wedgwood26-Oct-12 4:09 
AnswerRe: Can't open with free ms tools Pin
Stephan Stricker18-Nov-12 8:45
memberStephan Stricker18-Nov-12 8:45 
GeneralRe: Can't open with free ms tools Pin
chris wedgwood18-Nov-12 23:11
memberchris wedgwood18-Nov-12 23:11 
QuestionUnable to Read, able to write Pin
alexamex16-Oct-12 6:38
memberalexamex16-Oct-12 6:38 
AnswerRe: Unable to Read, able to write Pin
Stephan Stricker16-Oct-12 7:15
memberStephan Stricker16-Oct-12 7:15 
QuestionAny VB.Net synchronous mode examples available? Pin
Member 90645551-Jul-12 2:25
memberMember 90645551-Jul-12 2:25 
QuestionIt works Pin
Member 862908814-May-12 6:06
memberMember 862908814-May-12 6:06 
QuestionDisconnects and holding registers value displacement Pin
harizhent16-Mar-12 4:31
memberharizhent16-Mar-12 4:31 
AnswerRe: Disconnects and holding registers value displacement Pin
Stephan Stricker20-Mar-12 2:51
memberStephan Stricker20-Mar-12 2:51 
GeneralMy vote of 5 Pin
Nikolay Ugorliev24-Feb-12 1:27
memberNikolay Ugorliev24-Feb-12 1:27 
QuestionModbus says error: Illegal function! Pin
xeroxvrey6-Feb-12 5:57
memberxeroxvrey6-Feb-12 5:57 
AnswerRe: Modbus says error: Illegal function! Pin
Stephan Stricker6-Feb-12 6:07
memberStephan Stricker6-Feb-12 6:07 
GeneralRe: Modbus says error: Illegal function! Pin
xeroxvrey6-Feb-12 10:27
memberxeroxvrey6-Feb-12 10:27 
QuestionModbus TCP Class not working Pin
Joseph Frippiat23-Jan-12 1:50
memberJoseph Frippiat23-Jan-12 1:50 
Hello,
I am not able to make the code work.
When I try, with the sample application, to read 32 coils starting at address 1000 (with the button , I always get empty data (zeroes everywhere). In the OnReceive function, id and function are equals to zero and tcpAsyClBuffer is filled with zeroes.
I checked in the slave Modbus, it receives the request and it answers it. I don't know where the data are going?
Thanks for any answer,
Joseph
AnswerRe: Modbus TCP Class not working Pin
Stephan Stricker23-Jan-12 1:56
memberStephan Stricker23-Jan-12 1:56 
GeneralRe: Modbus TCP Class not working Pin
Joseph Frippiat23-Jan-12 2:13
memberJoseph Frippiat23-Jan-12 2:13 
GeneralRe: Modbus TCP Class not working Pin
Joseph Frippiat23-Jan-12 22:24
memberJoseph Frippiat23-Jan-12 22:24 
QuestionCould you help me Pin
Dantty27-Dec-11 4:42
memberDantty27-Dec-11 4:42 
BugTCP Network Bug Pin
mrraphi16-Nov-11 21:20
membermrraphi16-Nov-11 21:20 
GeneralRe: TCP Network Bug Pin
Member 841032817-Nov-11 2:07
memberMember 841032817-Nov-11 2:07 
GeneralRe: TCP Network Bug Pin
mrraphi17-Nov-11 3:43
membermrraphi17-Nov-11 3:43 
QuestionNo connection could be made because the target machine actively refused it Pin
ChiWaiLam6-Oct-11 1:00
memberChiWaiLam6-Oct-11 1:00 
AnswerRe: No connection could be made because the target machine actively refused it Pin
Member 841032817-Nov-11 2:09
memberMember 841032817-Nov-11 2:09 
QuestionMODBUS TCP Server in ASP.net Pin
arulchris8-Sep-11 21:50
memberarulchris8-Sep-11 21:50 
GeneralHelp with address mapping Pin
sandeepbaynes17-May-11 19:17
membersandeepbaynes17-May-11 19:17 
Questionnewbie interested in Modbus TCP/IP Pin
autocatch3-May-11 16:27
memberautocatch3-May-11 16:27 
AnswerRe: newbie interested in Modbus TCP/IP Pin
Stephan Stricker4-May-11 8:36
memberStephan Stricker4-May-11 8:36 
GeneralRe: newbie interested in Modbus TCP/IP Pin
autocatch4-May-11 13:30
memberautocatch4-May-11 13:30 
GeneralBig Bug Pin
mrraphi29-Mar-11 3:36
membermrraphi29-Mar-11 3:36 
GeneralRe: Big Bug Pin
Stephan Stricker6-Apr-11 5:05
memberStephan Stricker6-Apr-11 5:05 
GeneralMy vote of 5 Pin
JongchanAhn24-Mar-11 22:26
memberJongchanAhn24-Mar-11 22:26 
Generalconnection lost error Pin
ttusharbb23-Jan-11 18:28
memberttusharbb23-Jan-11 18:28 
Generalerror Pin
ttusharbb23-Jan-11 1:08
memberttusharbb23-Jan-11 1:08 
GeneralRe: error Pin
mrraphi29-Mar-11 3:22
membermrraphi29-Mar-11 3:22 
Generalproblem for "illegal data adress" error Pin
houzc200910-Nov-10 1:52
memberhouzc200910-Nov-10 1:52 
GeneralRe: problem for "illegal data adress" error Pin
Michael Fauchon17-Nov-10 17:54
memberMichael Fauchon17-Nov-10 17:54 
GeneralRe: problem for "illegal data adress" error Pin
houzc200917-Nov-10 22:46
memberhouzc200917-Nov-10 22:46 
Generalproblem with cross thread Pin
donelbaron5-Nov-10 3:25
memberdonelbaron5-Nov-10 3:25 
GeneralRe: problem with cross thread Pin
Michael Fauchon17-Nov-10 17:56
memberMichael Fauchon17-Nov-10 17:56 
GeneralNo connection could be made because the target machine actively refused it. Pin
nsoutinho15-Oct-10 0:17
membernsoutinho15-Oct-10 0:17 
GeneralRe: No connection could be made because the target machine actively refused it. Pin
larsberghahn26-Jan-11 0:27
memberlarsberghahn26-Jan-11 0:27 
GeneralTimeout Error Solved Pin
Michael Fauchon25-Aug-10 5:55
memberMichael Fauchon25-Aug-10 5:55 
Questionuse of the dll in VB Pin
swissjay2-Aug-10 8:44
memberswissjay2-Aug-10 8:44 
AnswerRe: use of the dll in VB Pin
nugentoffer23-Sep-10 10:41
membernugentoffer23-Sep-10 10:41 
GeneralRe: use of the dll in VB Pin
Michael Fauchon9-Oct-10 8:53
memberMichael Fauchon9-Oct-10 8:53 
Questionwhat language to open the source code Pin
dnqhung27-Jul-10 22:10
memberdnqhung27-Jul-10 22:10 
AnswerRe: what language to open the source code Pin
Michael Fauchon30-Sep-10 2:26
memberMichael Fauchon30-Sep-10 2:26 
GeneralError: Slave Time out Pin
_raghu26-Jul-10 1:37
member_raghu26-Jul-10 1:37 
GeneralProblem between "transaction identifier" and "Unit identifier" in Modbus header Pin
ld.dubost7-Jul-10 21:44
memberld.dubost7-Jul-10 21:44 
Questionwhat's my problem? Pin
M.Afsar19-May-10 0:34
memberM.Afsar19-May-10 0:34 

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