Implement Modbus/TCP protocol


Thread Starter



I am have to develop Modbus/TCP in C programming language. I'm allowed to choose a master (my personal computer), first slave (PIC microcontroller) and a second slave (wiznet chip).

I did a lot of searching on the internet and I have found samples in C# and VB that can't really help me. Can you please give me some code source developed in C, or web sites explaining this kind of development problem?

In advance I thank you. Best regards.

James Ingraham

"I am have to develop Modbus/TCP in C..."

Presumably you have already downloaded the Modbus/TCP specification from Oddly, I can't find the sample code that I downloaded about seven years ago from

I did find two maybe-useful resources with a Krugle search. (Google code search, was useless). MatPLC, often discussed here, has a Modbus/TCP implementation availabe under the GPL.

FreeModbus also has an implementation, available under the BSD license.

However, I really think it's better to do this from scratch. You do need to know how to write sockets code in C, for which I recommend Beej's Guide to Network Programming ( or the UNIX Sockets Tutorial (

After that, Modbus/TCP is really quite easy to implement. If you do run into trouble, it should be fairly easy to look at that C# code you found. There's so little going on and the syntax is similar enough that you should be able to "translate" without much issue.

Last, but not least, I recommend running Wireshark to watch your packets go by. Wireshark natively understands Modbus/TCP, so it will pick up on certain errors and let you see if your packets are formed they way you think they are.

Good luck.

-James Ingraham
Sage Automation, Inc.