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

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 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

 
QuestionHow to communicate with many devices at the same time? PinmemberMember 109289987-Aug-14 22:31 
QuestionHow Can I give command to any hardware unit using modbus ??? PinmemberPdeveloper29-Jul-14 1:04 
QuestionBug in CreateReadHeader code PinmemberMember 861764024-Jun-14 11:38 
QuestionAsync timeout ,byte swapping and a Modbus slave project PinmemberMember 861764024-Jun-14 7:29 
QuestionIssues with using this code in C++ project PinmemberMike Grove19-Jun-14 0:19 
QuestionMessage Transaction Id Pinmemberkhalprin27-May-14 4:17 
BugHow to wait the end of ModBus readcoil function PinmemberRhyzko3-Apr-14 5:37 
GeneralRe: How to wait the end of ModBus readcoil function PinprofessionalStephan Stricker23-May-14 3:57 
QuestionModbus Slave application Pinmemberromasimeiz26-Feb-14 3:25 
AnswerRe: Modbus Slave application PinmemberStephan Stricker27-Feb-14 7:59 
GeneralRe: Modbus Slave application Pinmembernasher21927-Jul-14 22:31 
QuestionHow to download??? Pinmemberrquirch10-Jan-14 10:40 
QuestionModbus TCP Class for VB.net 2008 PinmemberThe Real John Richards10-Jan-14 0:02 
AnswerRe: Modbus TCP Class for VB.net 2008 PinmemberStephan Stricker7-Feb-14 8:46 
QuestionThanks! PinmemberMember 92730099-Jan-14 4:04 
QuestionSimple way to run class from console PinmemberTadejP8122-Nov-13 11:41 
QuestionVS ?? PinmemberMember 103773933-Nov-13 5:09 
AnswerRe: VS ?? PinmemberFlavi114-Nov-13 5:44 
NewsModbus Framework for .NET PinmemberFlavi126-Oct-13 1:29 
GeneralRe: Modbus Framework for .NET PinmemberMember 399685331-Oct-13 6:41 
GeneralRe: Modbus Framework for .NET PinmemberFlavi18-Nov-13 1:51 
QuestionIllegal Data Address Pinmembersoftwaremonkey3-Oct-13 3:43 
AnswerRe: Illegal Data Address Pinmembersoftwaremonkey3-Oct-13 4:16 
GeneralRe: Illegal Data Address Pinmembermehmet_alixx16-Jan-14 7:53 
Questionmodbus message response is not receiving in same order PinmemberUma Mahes17-Sep-13 20:10 

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