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

 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP PinmemberStephan Stricker3-Jan-13 5:12 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP PinmemberMember 94111612-Jan-13 13:16 
AnswerRe: problem with writing to Siemens PLC holding register with MODBUS TCP PinmemberAndrea Feduzzi14-May-13 7:12 
QuestionCan't Write Coils PinmemberKitaMenang22-Nov-12 4:08 
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 
I am not familiar with the visual studio tools, but I expect to be able to figure it out.
 
However, I cannot open the solution with the free download Visual Studio Express (VB and C# installed)
 
Can someone please give me a step by step on how to get started. Frown | :(
 
Thanks.
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 
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 

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