Click here to Skip to main content
Click here to Skip to main content

Modbus TCP class

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

 
AnswerRe: Can't Write Coils PinmemberStephan Stricker22-Nov-12 4:17 
Questionconvert error Pinmembertomasz25616-Nov-12 3:28 
AnswerRe: convert error PinmemberStephan Stricker18-Nov-12 9:44 
QuestionCan't open with free ms tools Pinmemberchris wedgwood26-Oct-12 5:09 
AnswerRe: Can't open with free ms tools PinmemberStephan Stricker18-Nov-12 9:45 
GeneralRe: Can't open with free ms tools Pinmemberchris wedgwood19-Nov-12 0:11 
QuestionUnable to Read, able to write Pinmemberalexamex16-Oct-12 7:38 
AnswerRe: Unable to Read, able to write PinmemberStephan Stricker16-Oct-12 8:15 
QuestionAny VB.Net synchronous mode examples available? PinmemberMember 90645551-Jul-12 3:25 
QuestionIt works PinmemberMember 862908814-May-12 7:06 
QuestionDisconnects and holding registers value displacement Pinmemberharizhent16-Mar-12 5:31 
AnswerRe: Disconnects and holding registers value displacement PinmemberStephan Stricker20-Mar-12 3:51 
GeneralMy vote of 5 PinmemberNikolay Ugorliev24-Feb-12 2:27 
QuestionModbus says error: Illegal function! Pinmemberxeroxvrey6-Feb-12 6:57 
Hello, I get this error: (Modbus says error: Illegal function!) When working with a simulator called "Modbus Slave", but if I taste with another called "mod_RSsim" works perfectly, the same error I get when I connect to a device physical Modbus TCP. Is it a compatibility issue with some other Modbus protocol?
AnswerRe: Modbus says error: Illegal function! PinmemberStephan Stricker6-Feb-12 7:07 
GeneralRe: Modbus says error: Illegal function! Pinmemberxeroxvrey6-Feb-12 11:27 
QuestionModbus TCP Class not working PinmemberJoseph Frippiat23-Jan-12 2:50 
AnswerRe: Modbus TCP Class not working PinmemberStephan Stricker23-Jan-12 2:56 
GeneralRe: Modbus TCP Class not working PinmemberJoseph Frippiat23-Jan-12 3:13 
GeneralRe: Modbus TCP Class not working PinmemberJoseph Frippiat23-Jan-12 23:24 
QuestionCould you help me PinmemberDantty27-Dec-11 5:42 
BugTCP Network Bug Pinmembermrraphi16-Nov-11 22:20 
GeneralRe: TCP Network Bug PinmemberMember 841032817-Nov-11 3:07 
GeneralRe: TCP Network Bug Pinmembermrraphi17-Nov-11 4:43 
QuestionNo connection could be made because the target machine actively refused it PinmemberChiWaiLam6-Oct-11 2:00 
AnswerRe: No connection could be made because the target machine actively refused it PinmemberMember 841032817-Nov-11 3:09 
QuestionMODBUS TCP Server in ASP.net Pinmemberarulchris8-Sep-11 22:50 
GeneralHelp with address mapping Pinmembersandeepbaynes17-May-11 20:17 
Questionnewbie interested in Modbus TCP/IP Pinmemberautocatch3-May-11 17:27 
AnswerRe: newbie interested in Modbus TCP/IP PinmemberStephan Stricker4-May-11 9:36 
GeneralRe: newbie interested in Modbus TCP/IP Pinmemberautocatch4-May-11 14:30 
GeneralBig Bug Pinmembermrraphi29-Mar-11 4:36 
GeneralRe: Big Bug PinmemberStephan Stricker6-Apr-11 6:05 
GeneralMy vote of 5 PinmemberJongchanAhn24-Mar-11 23:26 
Generalconnection lost error Pinmemberttusharbb23-Jan-11 19:28 
Generalerror Pinmemberttusharbb23-Jan-11 2:08 
GeneralRe: error Pinmembermrraphi29-Mar-11 4:22 
Generalproblem for "illegal data adress" error Pinmemberhouzc200910-Nov-10 2:52 
GeneralRe: problem for "illegal data adress" error PinmemberMichael Fauchon17-Nov-10 18:54 
GeneralRe: problem for "illegal data adress" error Pinmemberhouzc200917-Nov-10 23:46 
Generalproblem with cross thread Pinmemberdonelbaron5-Nov-10 4:25 
GeneralRe: problem with cross thread PinmemberMichael Fauchon17-Nov-10 18:56 
GeneralNo connection could be made because the target machine actively refused it. Pinmembernsoutinho15-Oct-10 1:17 
GeneralRe: No connection could be made because the target machine actively refused it. Pinmemberlarsberghahn26-Jan-11 1:27 
GeneralTimeout Error Solved PinmemberMichael Fauchon25-Aug-10 6:55 
Questionuse of the dll in VB Pinmemberswissjay2-Aug-10 9:44 
AnswerRe: use of the dll in VB Pinmembernugentoffer23-Sep-10 11:41 
GeneralRe: use of the dll in VB PinmemberMichael Fauchon9-Oct-10 9:53 
Questionwhat language to open the source code Pinmemberdnqhung27-Jul-10 23:10 
AnswerRe: what language to open the source code PinmemberMichael Fauchon30-Sep-10 3:26 

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