Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / XML
Tip/Trick

Modbus TCP class

Rate me:
Please Sign up or sign in to vote.
4.89/5 (84 votes)
23 May 2014CPOL 1.7M   60.4K   154   171
A Modbus TCP class.

The project has moved to GIT

https://github.com/stephan1827/modbusTCP

Please download and ask questions from there.

License

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


Written By
Systems Engineer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMore simple sample, please!) Pin
Voodoo902-Jul-13 1:57
Voodoo902-Jul-13 1:57 
NewsModbus TCP Sample (VB.net) Pin
hadihidayat2-Jul-13 0:36
hadihidayat2-Jul-13 0:36 
QuestionNot able to read from Modbus TCP simulator Pin
MayurShah.in28-Jun-13 21:51
MayurShah.in28-Jun-13 21:51 
AnswerRe: Not able to read from Modbus TCP simulator Pin
Stephan Stricker29-Jun-13 20:55
Stephan Stricker29-Jun-13 20:55 
QuestionModbus RTU over TCP Pin
JosepLluis17-Jun-13 10:18
JosepLluis17-Jun-13 10:18 
AnswerRe: Modbus RTU over TCP Pin
Stephan Stricker17-Jun-13 13:33
Stephan Stricker17-Jun-13 13:33 
GeneralRe: Modbus RTU over TCP Pin
JosepLluis18-Jun-13 2:31
JosepLluis18-Jun-13 2:31 
GeneralRe: Modbus RTU over TCP Pin
kenselvia@gmail.com21-Jun-13 23:21
kenselvia@gmail.com21-Jun-13 23:21 
Thanks for the very useful code!

This describes the difference between modbus TCP and RTU;

http://www.simplymodbus.ca/TCP.htm[^]

ModbusUtility.cs found here calculates the correct CRC;

http://code.google.com/p/nmodbus[^]

This will generate correct RTU MBAP header;

C#
private byte[] CreateReadHeaderRTU(ushort id, byte unit, ushort startAddress, ushort length, byte function)
 {
     byte[] data = new byte[6];
     byte[] crc = new byte[2];
     data[0] = unit; // Was 0 Slave address
     data[1] = function; // Function code
     byte[] _adr = BitConverter.GetBytes((short) IPAddress.HostToNetworkOrder((short) startAddress));
     data[2] = _adr[0]; // Start address
     data[3] = _adr[1]; // Start address
     byte[] _length = BitConverter.GetBytes((short) IPAddress.HostToNetworkOrder((short) length));
     data[4] = _length[0]; // Number of data to read
     data[5] = _length[1]; // Number of data to read
     crc = ModbusUtil.CalculateCrc(data);
     byte[] ret = new byte[data.Length + crc.Length];
     Buffer.BlockCopy(data, 0, ret, 0, data.Length);
     Buffer.BlockCopy(crc, 0, ret, data.Length, crc.Length);
     return ret;
 }


But I did not finish the response OnReceive() code

VB
//TCP 00000000 | 00 00 00 00 00 07 01 03 04 42 A0 9B 1C          | .........B...
//RTU 00000000 | 03 03 04 44 C2 41 88 5C C9                      | ...D.A.\.
ushort rtu = BitConverter.ToUInt16(tcpAsyClBuffer, resultptr+2); // Zero for TCP, bytecount for RTU


because I realized ModbusRTU has no transaction ID, therefore it can't be done non-sequentially asynchronously since nothing in the response identifies the reqeust. But I did verify a single request/response was correct. I will eventually have to finish the synchronous RTU support.

I also added support for Enron (Modicon) and Omni extended ASCII read buffer (report) function 65

I created an Excel RTD (real-time-data) add-in using your code-base with a few modifications. I'll post it when I finish.
Ken

QuestionSlave_ID and Slave_Address Pin
SBerube13-Jun-13 2:04
SBerube13-Jun-13 2:04 
AnswerRe: Slave_ID and Slave_Address Pin
Stephan Stricker14-Jun-13 3:51
Stephan Stricker14-Jun-13 3:51 
Questionvalues of message Pin
monkar22-May-13 7:08
monkar22-May-13 7:08 
AnswerRe: values of message Pin
Stephan Stricker22-May-13 7:23
Stephan Stricker22-May-13 7:23 
GeneralRe: values of message Pin
monkar22-May-13 23:57
monkar22-May-13 23:57 
GeneralRe: values of message Pin
Stephan Stricker23-May-13 2:18
Stephan Stricker23-May-13 2:18 
GeneralRe: values of message Pin
monkar23-May-13 6:02
monkar23-May-13 6:02 
GeneralRe: values of message Pin
Stephan Stricker24-May-13 8:54
Stephan Stricker24-May-13 8:54 
QuestionRe: values of message Pin
vinnie_j26-Jul-13 3:11
vinnie_j26-Jul-13 3:11 
QuestionSiemens S1200 Pin
Andrea Feduzzi14-May-13 6:09
professionalAndrea Feduzzi14-May-13 6:09 
AnswerRe: Siemens S1200 Pin
Stephan Stricker22-May-13 7:13
Stephan Stricker22-May-13 7:13 
GeneralRe: Siemens S1200 Pin
Andrea Feduzzi31-May-13 6:19
professionalAndrea Feduzzi31-May-13 6:19 
GeneralMy vote of 5 Pin
JongchanAhn24-Mar-13 15:03
JongchanAhn24-Mar-13 15:03 
GeneralRe: My vote of 5 Pin
Andrea Feduzzi14-May-13 6:10
professionalAndrea Feduzzi14-May-13 6:10 
Questionproblem with writing to Siemens PLC holding register with MODBUS TCP Pin
Member 941116121-Dec-12 9:14
Member 941116121-Dec-12 9:14 
AnswerRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Stephan Stricker21-Dec-12 22:55
Stephan Stricker21-Dec-12 22:55 
GeneralRe: problem with writing to Siemens PLC holding register with MODBUS TCP Pin
Member 94111612-Jan-13 9:16
Member 94111612-Jan-13 9:16 

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.