Click here to Skip to main content
12,399,362 members (56,000 online)
Click here to Skip to main content
Add your own
alternative version

Stats

437.9K views
46.3K downloads
140 bookmarked
Posted

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 don't want to explain how the modbus protocol works in detail, because there is plenty of information around.

Using the code

All commands are sent in synchronous or asynchronous mode. If a value is accessed in synchronous mode, the program will stop and wait for the slave to respond. If the slave does not 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 asynchronous connection. The class directory contains a help file (ModbusClassTCP\doc) with detailed information for every function.

A sample application is included in the package that shows the basic features. It uses asynchronous mode for all function calls. The sample has some additional code to make the result handling more comfortable.

Points of Interest

Remember that a lot of Modbus clients use a watchdog telegram to make sure the master is still active. If you don’t call this watchdog within a specified time, the slave will close the connection. This feature is used to avoid unused open ports when the connection is interrupted.

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

You may also be interested in...

Comments and Discussions

 
QuestionSiemens S1200 Pin
Andrea Feduzzi14-May-13 6:09
memberAndrea Feduzzi14-May-13 6:09 
AnswerRe: Siemens S1200 Pin
Stephan Stricker22-May-13 7:13
memberStephan Stricker22-May-13 7:13 
GeneralRe: Siemens S1200 Pin
Andrea Feduzzi31-May-13 6:19
memberAndrea Feduzzi31-May-13 6:19 
GeneralMy vote of 5 Pin
JongchanAhn24-Mar-13 15:03
memberJongchanAhn24-Mar-13 15:03 
GeneralRe: My vote of 5 Pin
Andrea Feduzzi14-May-13 6:10
memberAndrea Feduzzi14-May-13 6:10 
Questionproblem with writing to Siemens PLC holding register with MODBUS TCP Pin
Member 941116121-Dec-12 9:14
memberMember 941116121-Dec-12 9:14 
AnswerRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Stephan Stricker21-Dec-12 22:55
memberStephan Stricker21-Dec-12 22:55 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Member 94111612-Jan-13 9:16
memberMember 94111612-Jan-13 9:16 
Hi Stephen,

Thanks for your reply.

As you suggested, I tried wireshark, and was using your MODBUS tester to test the communication. It seems that for writing multiple registers, the start address couldn't go over 84. If start address is bigger than 84, for example 85, PLC will give out exception.

I saved the file and screen copy, but not sure how to attach it to this message.

and I am new to it, maybe did something silly.

Thanks,

Anne
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Stephan Stricker3-Jan-13 4:12
memberStephan Stricker3-Jan-13 4:12 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Member 94111612-Jan-13 12:16
memberMember 94111612-Jan-13 12:16 
AnswerRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Andrea Feduzzi14-May-13 6:12
memberAndrea Feduzzi14-May-13 6:12 
QuestionCan't Write Coils Pin
KitaMenang22-Nov-12 3:08
memberKitaMenang22-Nov-12 3:08 
AnswerRe: Can't Write Coils Pin
Stephan Stricker22-Nov-12 3:17
memberStephan Stricker22-Nov-12 3:17 
Questionconvert error Pin
tomasz25616-Nov-12 2:28
membertomasz25616-Nov-12 2:28 
AnswerRe: convert error Pin
Stephan Stricker18-Nov-12 8:44
memberStephan Stricker18-Nov-12 8:44 
QuestionCan't open with free ms tools Pin
chris wedgwood26-Oct-12 4:09
memberchris wedgwood26-Oct-12 4:09 
AnswerRe: Can't open with free ms tools Pin
Stephan Stricker18-Nov-12 8:45
memberStephan Stricker18-Nov-12 8:45 
GeneralRe: Can't open with free ms tools Pin
chris wedgwood18-Nov-12 23:11
memberchris wedgwood18-Nov-12 23:11 
QuestionUnable to Read, able to write Pin
alexamex16-Oct-12 6:38
memberalexamex16-Oct-12 6:38 
AnswerRe: Unable to Read, able to write Pin
Stephan Stricker16-Oct-12 7:15
memberStephan Stricker16-Oct-12 7:15 
QuestionAny VB.Net synchronous mode examples available? Pin
Member 90645551-Jul-12 2:25
memberMember 90645551-Jul-12 2:25 
QuestionIt works Pin
Member 862908814-May-12 6:06
memberMember 862908814-May-12 6:06 
QuestionDisconnects and holding registers value displacement Pin
harizhent16-Mar-12 4:31
memberharizhent16-Mar-12 4:31 
AnswerRe: Disconnects and holding registers value displacement Pin
Stephan Stricker20-Mar-12 2:51
memberStephan Stricker20-Mar-12 2:51 
GeneralMy vote of 5 Pin
Nikolay Ugorliev24-Feb-12 1:27
memberNikolay Ugorliev24-Feb-12 1:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.160721.1 | Last Updated 23 May 2014
Article Copyright 2006 by Stephan Stricker
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid