Click here to Skip to main content
13,252,259 members (55,638 online)
Click here to Skip to main content
Add your own
alternative version

Stats

652.6K views
55.8K downloads
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.

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

 
QuestionCan't open with free ms tools Pin
chris wedgwood26-Oct-12 5:09
memberchris wedgwood26-Oct-12 5:09 
AnswerRe: Can't open with free ms tools Pin
Stephan Stricker18-Nov-12 9:45
memberStephan Stricker18-Nov-12 9:45 
GeneralRe: Can't open with free ms tools Pin
chris wedgwood19-Nov-12 0:11
memberchris wedgwood19-Nov-12 0:11 
QuestionUnable to Read, able to write Pin
alexamex16-Oct-12 7:38
memberalexamex16-Oct-12 7:38 
AnswerRe: Unable to Read, able to write Pin
Stephan Stricker16-Oct-12 8:15
memberStephan Stricker16-Oct-12 8:15 
QuestionAny VB.Net synchronous mode examples available? Pin
Member 90645551-Jul-12 3:25
memberMember 90645551-Jul-12 3:25 
QuestionIt works Pin
Member 862908814-May-12 7:06
memberMember 862908814-May-12 7:06 
QuestionDisconnects and holding registers value displacement Pin
harizhent16-Mar-12 5:31
memberharizhent16-Mar-12 5:31 
Hi,
Thank you for shearing this code.
I just compiled your sample code and i'm having few problems...
few seconds after connecting and reading any data from HMI(Beijer iX), program disconnects(connection is lost).
Then i try read holding registers from HMI(40001-40005), the results i get is like that..
http://imageshack.us/photo/my-images/269/deletelt.png/[^]
data is correct but it is misplaced or something like that..

thank you.
AnswerRe: Disconnects and holding registers value displacement Pin
Stephan Stricker20-Mar-12 3:51
memberStephan Stricker20-Mar-12 3:51 
GeneralMy vote of 5 Pin
Nikolay Ugorliev24-Feb-12 2:27
memberNikolay Ugorliev24-Feb-12 2:27 
QuestionModbus says error: Illegal function! Pin
xeroxvrey6-Feb-12 6:57
memberxeroxvrey6-Feb-12 6:57 
AnswerRe: Modbus says error: Illegal function! Pin
Stephan Stricker6-Feb-12 7:07
memberStephan Stricker6-Feb-12 7:07 
GeneralRe: Modbus says error: Illegal function! Pin
xeroxvrey6-Feb-12 11:27
memberxeroxvrey6-Feb-12 11:27 
QuestionModbus TCP Class not working Pin
Joseph Frippiat23-Jan-12 2:50
memberJoseph Frippiat23-Jan-12 2:50 
AnswerRe: Modbus TCP Class not working Pin
Stephan Stricker23-Jan-12 2:56
memberStephan Stricker23-Jan-12 2:56 
GeneralRe: Modbus TCP Class not working Pin
Joseph Frippiat23-Jan-12 3:13
memberJoseph Frippiat23-Jan-12 3:13 
GeneralRe: Modbus TCP Class not working Pin
Joseph Frippiat23-Jan-12 23:24
memberJoseph Frippiat23-Jan-12 23:24 
QuestionCould you help me Pin
Dantty27-Dec-11 5:42
memberDantty27-Dec-11 5:42 
BugTCP Network Bug Pin
mrraphi16-Nov-11 22:20
membermrraphi16-Nov-11 22:20 
GeneralRe: TCP Network Bug Pin
Member 841032817-Nov-11 3:07
memberMember 841032817-Nov-11 3:07 
GeneralRe: TCP Network Bug Pin
mrraphi17-Nov-11 4:43
membermrraphi17-Nov-11 4:43 
QuestionNo connection could be made because the target machine actively refused it Pin
ChiWaiLam6-Oct-11 2:00
memberChiWaiLam6-Oct-11 2:00 
AnswerRe: No connection could be made because the target machine actively refused it Pin
Member 841032817-Nov-11 3:09
memberMember 841032817-Nov-11 3:09 
QuestionMODBUS TCP Server in ASP.net Pin
arulchris8-Sep-11 22:50
memberarulchris8-Sep-11 22:50 
GeneralHelp with address mapping Pin
sandeepbaynes17-May-11 20:17
membersandeepbaynes17-May-11 20:17 

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