Click here to Skip to main content
Licence CPOL
First Posted 5 Nov 2006
Views 129,945
Downloads 7,319
Bookmarked 71 times

Modbus TCP class

By | 5 Nov 2006 | Article
A Modbus TCP class.

Sample screenshot

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 creates two communication lines for each slave. This is necessary because the synchronous thread has to wait for a previous command to finish. This would block the asynchonous connection if we don't use a separate line. The class directory contains a help file (ModbusClassTCP\doc) with detailed information for every function.

There is an example application included that shows the basic features. It contains all functions in asynchronous mode. The synchronous functions are similar. The sample has some additional code to make the results and handling more comfortable. It has a timer included that generates a watchdog telegram for the Modbus slave I use for testing. You can disable this watchdog, or modify it for your needs.

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 1.00

    This is the first release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Stephan Stricker



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIt works PinmemberMember 86290886:06 14 May '12  
QuestionDisconnects and holding registers value displacement Pinmemberharizhent4:31 16 Mar '12  
AnswerRe: Disconnects and holding registers value displacement PinmemberStephan Stricker2:51 20 Mar '12  
GeneralMy vote of 5 PinmemberNikolay Ugorliev1:27 24 Feb '12  
QuestionModbus says error: Illegal function! Pinmemberxeroxvrey5:57 6 Feb '12  
AnswerRe: Modbus says error: Illegal function! PinmemberStephan Stricker6:07 6 Feb '12  
GeneralRe: Modbus says error: Illegal function! Pinmemberxeroxvrey10:27 6 Feb '12  
QuestionModbus TCP Class not working PinmemberJoseph Frippiat1:50 23 Jan '12  
AnswerRe: Modbus TCP Class not working PinmemberStephan Stricker1:56 23 Jan '12  
GeneralRe: Modbus TCP Class not working PinmemberJoseph Frippiat2:13 23 Jan '12  
GeneralRe: Modbus TCP Class not working PinmemberJoseph Frippiat22:24 23 Jan '12  
QuestionCould you help me PinmemberDantty4:42 27 Dec '11  
BugTCP Network Bug Pinmembermrraphi21:20 16 Nov '11  
GeneralRe: TCP Network Bug PinmemberMember 84103282:07 17 Nov '11  
GeneralRe: TCP Network Bug Pinmembermrraphi3:43 17 Nov '11  
QuestionNo connection could be made because the target machine actively refused it PinmemberChiWaiLam1:00 6 Oct '11  
AnswerRe: No connection could be made because the target machine actively refused it PinmemberMember 84103282:09 17 Nov '11  
QuestionMODBUS TCP Server in ASP.net Pinmemberarulchris21:50 8 Sep '11  
GeneralHelp with address mapping Pinmembersandeepbaynes19:17 17 May '11  
Questionnewbie interested in Modbus TCP/IP Pinmemberautocatch16:27 3 May '11  
AnswerRe: newbie interested in Modbus TCP/IP PinmemberStephan Stricker8:36 4 May '11  
GeneralRe: newbie interested in Modbus TCP/IP Pinmemberautocatch13:30 4 May '11  
GeneralBig Bug Pinmembermrraphi3:36 29 Mar '11  
GeneralRe: Big Bug PinmemberStephan Stricker5:05 6 Apr '11  
GeneralMy vote of 5 PinmemberJongchanAhn22:26 24 Mar '11  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 6 Nov 2006
Article Copyright 2006 by Stephan Stricker
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid