Modbus Tcp Server

Description of the Modbus OPC server. MasterOPC Universal Modbus Server is an OPC server for Modbus RTU/ASCII/TCP industrial applications. The Modbus OPC server provides two OPC interfaces: DA (Data Access, processed data) and HDA (Historical Data Access, archived data).

Active2 years, 1 month ago

I am currently working on a project which will allow different automates to communicate. To do so, I would like to create a client and a server that will talk using the modbus protocol. I am not sure if I want to be using ModBus/TCP, ModBus/RTU or ModBus/ASCII for now.

Modbus Tcp Server

I have searched for client/server examples in C and I could find libraries but no simple example of communication. I would like to start from scratch so libraries are not what I am looking for.

What I am asking for is if someone could give me a simple code written in C for a client and/or a server that communicate using Modbus, since I am not sure of what I will be using any type of Modbus would be a great help (RTU/TCP/ASCII).

Modbus

The simpler the better, what I would like the code to demonstrate is, for example : an initialization to the server, a request, an answer, closing the connection.

Thank you very much for your time.

PiggyGeniusPiggyGenius
1601 gold badge5 silver badges21 bronze badges

1 Answer

Modbus Tcp Server

Three things:

  1. As you're developing your own client and server components, I suggest you to use Modbus only if strictly required or convenient with an eye to openness (i.e. other manufacturers must be able to communicate with your client or server components by means of a standardized protocol - and Modbus fits).
  2. Be aware that Modbus TCP isn't just Modbus RTU(/ASCII) over TCP/IP (which is still allowed, of course, also UDP would be allowed). There are some important differences to take into account.
  3. I understand that you need to understand Modbus at a deeper level. At that point, once you have an open serial channel or (listening) TCP socket inside your C program, you may just start with simple Modbus requests/responses.

Take a look at this short but quite complete description, and also at the documentation of this constantly updated library.

Here's a super-simplified RTU example for Linux, based on libmodbus.
Allow me some C99 relaxation for compactness.
In the real world you should also properly handle signals like SIGTERM, etc...
There's also a modbus_rtu_set_serial_mode (RS232 vs RS485) function for Linux kernels 2.6.28 onwards. You may find other libraries that make working with RS485 easier on your platform.

Master snippet

Slave snippet

matpopmatpop
1,6371 gold badge16 silver badges29 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Modbus Protocol Reference Guide

Not the answer you're looking for? Browse other questions tagged cnetwork-programmingserial-communicationmodbusmodbus-tcp or ask your own question.