In the field of industrial automation, Modbus protocol is often used for inter device communication due to its simplicity and wide support. This article introduces how to use C language and libmodbus library to create a fully functional Modbus TCP server for effective communication between devices.
Complete code:
1. Initialize Modbus server
Firstly, it is necessary to create a Modbus TCP server instance. Usemodbus_new_tcpInitialize a function pointing tomodbus_tfunction, which takes the server's IP address and port number as parameters. In this example, the server is configured to listen on port 1502 at local address 127.0.0.1:
ctx = modbus_new_tcp("127.0.0.1", 1502);2. Set Modbus mapping
Modbus mapping is the way the server stores its data (such as coils, discrete inputs, hold registers, and input registers). Usemodbus_mapping_newto allocate and initialize these data areas:
mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, MODBUS_MAX_READ_REGISTERS, 0);
If the mapping creation fails, the server will print an error message and exit.
3. Listening Connection
Usagemodbus_tcp_listenfunction is used to listen for network connections and returns a socket descriptor. The maximum number of connections that the server can handle is defined by theNB_CONNECTIONmacro:
server_socket = modbus_tcp_listen(ctx, NB_CONNECTION);
4. Processing SIGINT signals
In order to gracefully shut down the server and release resources, we use thesignalfunction to capture SIGINT signals (usually triggered by Ctrl+C) and associate them with theclose_sigintfunction:
signal(SIGINT, close_sigint);
close_sigintfunction to close sockets, release Modbus context and mapping, and then exit the program.
5. Main Loop
The server runs in an infinite loop and uses theselectFunction to listen for activity on the socket. IfselectWhen activity is detected, the server will check whether it is a new connection request or a data request with an established connection:
if (master_socket == server_socket) {
// 处理新的连接请求
} else {
// 处理来自已连接客户端的数据
}For new connections, the server accepts the connection and adds the new socket to the listening set. For data requests, the server usesmodbus_receiveReceive data and then use itmodbus_replySend a response.
6. Performance evaluation
To evaluate the performance of the server, a calculation of the transmission data rate was added to the code. The server calculates the number of bits and registers successfully processed within a given time to obtain the transfer rates of points per second and KiB/second
rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
printf("* %d points/sn", rate);These performance metrics are very useful for optimizing server settings and network configurations.
Conclusion
By using C language and libmodbus library, a Modbus TCP server can be effectively implemented, which not only supports multi client connections but also provides real-time data on its performance. This provides a powerful and flexible solution for device communication in industrial applications.
Leave a Reply