Click here to Skip to main content
11,412,833 members (76,889 online)
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

 
AnswerRe: Slave_ID and Slave_Address Pin
Stephan Stricker at 14-Jun-13 4:51
memberStephan Stricker14-Jun-13 4:51 
Questionvalues of message Pin
monkar at 22-May-13 8:08
membermonkar22-May-13 8:08 
AnswerRe: values of message Pin
Stephan Stricker at 22-May-13 8:23
memberStephan Stricker22-May-13 8:23 
GeneralRe: values of message Pin
monkar at 23-May-13 0:57
membermonkar23-May-13 0:57 
GeneralRe: values of message Pin
Stephan Stricker at 23-May-13 3:18
memberStephan Stricker23-May-13 3:18 
GeneralRe: values of message Pin
monkar at 23-May-13 7:02
membermonkar23-May-13 7:02 
GeneralRe: values of message Pin
Stephan Stricker at 24-May-13 9:54
memberStephan Stricker24-May-13 9:54 
QuestionRe: values of message [modified] Pin
vinnie_j at 26-Jul-13 4:11
membervinnie_j26-Jul-13 4:11 
QuestionSiemens S1200 Pin
Andrea Feduzzi at 14-May-13 7:09
memberAndrea Feduzzi14-May-13 7:09 
AnswerRe: Siemens S1200 Pin
Stephan Stricker at 22-May-13 8:13
memberStephan Stricker22-May-13 8:13 
GeneralRe: Siemens S1200 Pin
Andrea Feduzzi at 31-May-13 7:19
memberAndrea Feduzzi31-May-13 7:19 
GeneralMy vote of 5 Pin
JongchanAhn at 24-Mar-13 16:03
memberJongchanAhn24-Mar-13 16:03 
GeneralRe: My vote of 5 Pin
Andrea Feduzzi at 14-May-13 7:10
memberAndrea Feduzzi14-May-13 7:10 
Questionproblem with writing to Siemens PLC holding register with MODBUS TCP Pin
Member 9411161 at 21-Dec-12 10:14
memberMember 941116121-Dec-12 10:14 
Hi,

I am trying communication between my PC and Siemens PLC with MODBUS TCP. PLC is a server, and I set "P#M100.0 BYTE 200" to holding register. I am using first 100 bytes for reading with no problem, but writing to the second 100 bytes doesn't work. I am using WriteMultipleRegister(6, 100, data), everything looks OK and no error msg, but value didn't change on the PLC side.

Could anyone please help?

Thanks a lot in advance.

Anne
AnswerRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Stephan Stricker at 21-Dec-12 23:55
memberStephan Stricker21-Dec-12 23:55 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Member 9411161 at 2-Jan-13 10:16
memberMember 94111612-Jan-13 10:16 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Stephan Stricker at 3-Jan-13 5:12
memberStephan Stricker3-Jan-13 5:12 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Member 9411161 at 2-Jan-13 13:16
memberMember 94111612-Jan-13 13:16 
AnswerRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Andrea Feduzzi at 14-May-13 7:12
memberAndrea Feduzzi14-May-13 7:12 
QuestionCan't Write Coils Pin
KitaMenang at 22-Nov-12 4:08
memberKitaMenang22-Nov-12 4:08 
AnswerRe: Can't Write Coils Pin
Stephan Stricker at 22-Nov-12 4:17
memberStephan Stricker22-Nov-12 4:17 
Questionconvert error Pin
tomasz256 at 16-Nov-12 3:28
membertomasz25616-Nov-12 3:28 
AnswerRe: convert error Pin
Stephan Stricker at 18-Nov-12 9:44
memberStephan Stricker18-Nov-12 9:44 
QuestionCan't open with free ms tools Pin
chris wedgwood at 26-Oct-12 5:09
memberchris wedgwood26-Oct-12 5:09 
AnswerRe: Can't open with free ms tools Pin
Stephan Stricker at 18-Nov-12 9:45
memberStephan Stricker18-Nov-12 9:45 
GeneralRe: Can't open with free ms tools Pin
chris wedgwood at 19-Nov-12 0:11
memberchris wedgwood19-Nov-12 0:11 
QuestionUnable to Read, able to write Pin
alexamex at 16-Oct-12 7:38
memberalexamex16-Oct-12 7:38 
AnswerRe: Unable to Read, able to write Pin
Stephan Stricker at 16-Oct-12 8:15
memberStephan Stricker16-Oct-12 8:15 
QuestionAny VB.Net synchronous mode examples available? Pin
Member 9064555 at 1-Jul-12 3:25
memberMember 90645551-Jul-12 3:25 
QuestionIt works Pin
Member 8629088 at 14-May-12 7:06
memberMember 862908814-May-12 7:06 
QuestionDisconnects and holding registers value displacement Pin
harizhent at 16-Mar-12 5:31
memberharizhent16-Mar-12 5:31 
AnswerRe: Disconnects and holding registers value displacement Pin
Stephan Stricker at 20-Mar-12 3:51
memberStephan Stricker20-Mar-12 3:51 
GeneralMy vote of 5 Pin
Nikolay Ugorliev at 24-Feb-12 2:27
memberNikolay Ugorliev24-Feb-12 2:27 
QuestionModbus says error: Illegal function! Pin
xeroxvrey at 6-Feb-12 6:57
memberxeroxvrey6-Feb-12 6:57 
AnswerRe: Modbus says error: Illegal function! Pin
Stephan Stricker at 6-Feb-12 7:07
memberStephan Stricker6-Feb-12 7:07 
GeneralRe: Modbus says error: Illegal function! Pin
xeroxvrey at 6-Feb-12 11:27
memberxeroxvrey6-Feb-12 11:27 
QuestionModbus TCP Class not working Pin
Joseph Frippiat at 23-Jan-12 2:50
memberJoseph Frippiat23-Jan-12 2:50 
AnswerRe: Modbus TCP Class not working Pin
Stephan Stricker at 23-Jan-12 2:56
memberStephan Stricker23-Jan-12 2:56 
GeneralRe: Modbus TCP Class not working Pin
Joseph Frippiat at 23-Jan-12 3:13
memberJoseph Frippiat23-Jan-12 3:13 
GeneralRe: Modbus TCP Class not working Pin
Joseph Frippiat at 23-Jan-12 23:24
memberJoseph Frippiat23-Jan-12 23:24 
QuestionCould you help me Pin
Dantty at 27-Dec-11 5:42
memberDantty27-Dec-11 5:42 
BugTCP Network Bug Pin
mrraphi at 16-Nov-11 22:20
membermrraphi16-Nov-11 22:20 
GeneralRe: TCP Network Bug Pin
Member 8410328 at 17-Nov-11 3:07
memberMember 841032817-Nov-11 3:07 
GeneralRe: TCP Network Bug Pin
mrraphi at 17-Nov-11 4:43
membermrraphi17-Nov-11 4:43 
QuestionNo connection could be made because the target machine actively refused it Pin
ChiWaiLam at 6-Oct-11 2:00
memberChiWaiLam6-Oct-11 2:00 
AnswerRe: No connection could be made because the target machine actively refused it Pin
Member 8410328 at 17-Nov-11 3:09
memberMember 841032817-Nov-11 3:09 
QuestionMODBUS TCP Server in ASP.net Pin
arulchris at 8-Sep-11 22:50
memberarulchris8-Sep-11 22:50 
GeneralHelp with address mapping Pin
sandeepbaynes at 17-May-11 20:17
membersandeepbaynes17-May-11 20:17 
Questionnewbie interested in Modbus TCP/IP Pin
autocatch at 3-May-11 17:27
memberautocatch3-May-11 17:27 
AnswerRe: newbie interested in Modbus TCP/IP Pin
Stephan Stricker at 4-May-11 9:36
memberStephan Stricker4-May-11 9:36 

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