Click here to Skip to main content
Click here to Skip to main content
Go to top

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

 
Questionmodbus message response is not receiving in same order PinmemberUma Mahes17-Sep-13 20:10 
AnswerRe: modbus message response is not receiving in same order PinmemberStephan Stricker18-Sep-13 3:30 
GeneralRe: modbus message response is not receiving in same order PinmemberUma Mahes21-Jan-14 1:48 
SuggestionCan not read/write Address more than 32767. Edited conversion [modified] Pinmemberhadihidayat9-Sep-13 15:52 
GeneralMy vote of 5 Pinmember陳志杰25-Aug-13 17:49 
SuggestionSome improvements necessary Pinmembermuharrem27-Jul-13 13:17 
GeneralRe: Some improvements necessary PinmemberStephan Stricker29-Jul-13 7:10 
GeneralRe: Some improvements necessary Pinmembermuharrem30-Jul-13 4:51 
Questionasp.net and ModbusTCP class Pinmemberenfrost15-Jul-13 8:57 
AnswerRe: asp.net and ModbusTCP class PinmemberStephan Stricker15-Jul-13 11:10 
QuestionMore simple sample, please!) PinmemberMember 101198262-Jul-13 1:57 
NewsModbus TCP Sample (VB.net) [modified] Pinmemberhadihidayat2-Jul-13 0:36 
QuestionNot able to read from Modbus TCP simulator PinmemberMayurShah.in28-Jun-13 21:51 
AnswerRe: Not able to read from Modbus TCP simulator PinmemberStephan Stricker29-Jun-13 20:55 
QuestionModbus RTU over TCP PinmemberJosepLluis17-Jun-13 10:18 
AnswerRe: Modbus RTU over TCP PinmemberStephan Stricker17-Jun-13 13:33 
GeneralRe: Modbus RTU over TCP PinmemberJosepLluis18-Jun-13 2:31 
GeneralRe: Modbus RTU over TCP Pinmemberkselvia@eprod.com21-Jun-13 23:21 
QuestionSlave_ID and Slave_Address PinmemberSBerube13-Jun-13 2:04 
AnswerRe: Slave_ID and Slave_Address PinmemberStephan Stricker14-Jun-13 3:51 
Questionvalues of message Pinmembermonkar22-May-13 7:08 
AnswerRe: values of message PinmemberStephan Stricker22-May-13 7:23 
GeneralRe: values of message Pinmembermonkar22-May-13 23:57 
GeneralRe: values of message PinmemberStephan Stricker23-May-13 2:18 
GeneralRe: values of message Pinmembermonkar23-May-13 6:02 

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