Modbus TCP slave for Linux/Unix in C or C++?

  • Thread starter Ernst Murnleitner
  • Start date

Thread Starter

Ernst Murnleitner


maybe somebody can help me: I am looking for a modbusTcp slave source code, in C or ++, for Linux. Does someone have such. Open source would be preferred.

I already tried one which I found on a link at (modbus_tcp.h, modbus_tcp.c from Philip Costigan). While this code does work as master, the slave does not work.

However, I am not sure, if I used it correctly. First I called "set_up_tcp_slave()" which opens a socket and binds it to the port 502. Here AF_INET, ANADDR_ANY, SOCK_STREAM and IPPROTO is used by default. This function waits correctly until I try to connect with a master (I used Modbucfg: Windows, Visual C++ 6.0). When I connect from the master, a connection seems to be established (fd = 7). But then when the program is listening, it a) receives a timeout or b) select() delivers > 0 but recv() delivers -1. Thus in the program "socket failure" ist set. I don't know what these two functions do.


Diego Urda Carrasco

My solution: Asynchronous sockets tech.
When you have the socket from system, you must set up the asynchronous mode for this socket, pointing to a FD_READ event. (API winsock32 funct)
You must writte a function that will be processed for the Windows Messages Processor on the FD_READ event when there is a input on the TCP/IP channel, meanwhile Windows system is processing others tasks, IS THE MASTER WHO TRIES TO CONNECT WITH THIS SLAVE !!.
I normally work on Delphi/pascal, but using API winsock32 functions is very similar for C language.
>Best regards.
Can you give me the link of Philip, so I will download the code to get a look on it.

If I find a solution for slave I will send to you.

Best Regards
Hi Ernst,
I need to develop ModbusTCP Master on HP-Unix. Can you advice me where to find the link?

Checkout\MODBUSProtocolSuite.htm for a Source Code Library. However it is not opensource.