Click here to Skip to main content
11,642,381 members (63,136 online)
Click here to Skip to main content

Modbus TCP class

, 23 May 2014 CPOL 324K 37.9K 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

 
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 

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