Example code for implementing Modbus TCP communication based on C language embedded system

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Example code for implementing Modbus TCP communication based on C language embedded system缩略图

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 502

#define SERVER_IP "192.168.0.100"
#define DEVICE_ID 1

#define BUFFER_SIZE 1024

int main() {
    // 创建TCP连接
    int sockfd;
    struct sockaddr_in server_addr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        printf("Failed to create socket: %sn", strerror(errno));
        return 1;
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        printf("Failed to connect to server: %sn", strerror(errno));
        close(sockfd);
        return 1;
    }

    printf("Connected to server successfully.n");

    // 准备modbus请求
    unsigned char request[BUFFER_SIZE];
    unsigned char response[BUFFER_SIZE];
    int request_length, response_length;

    // 读保持寄存器
    int register_address = 0;
    int register_count = 4;
    request_length = modbus_build_request(request, DEVICE_ID, MODBUS_FC_READ_HOLDING_REGISTERS, register_address, register_count);

    // 发送modbus请求
    if (send(sockfd, request, request_length, 0) == -1) {
        printf("Failed to send request: %sn", strerror(errno));
        close(sockfd);
        return 1;
    }

    printf("Sent request successfully.n");

    // 接收modbus响应
    response_length = recv(sockfd, response, BUFFER_SIZE, 0);
    if (response_length == -1) {
        printf("Failed to receive response: %sn", strerror(errno));
        close(sockfd);
        return 1;
    }

    printf("Received response successfully.n");

    // 解析modbus响应
    int register_value = modbus_get_response_value(response, response_length);

    printf("Register value: %dn", register_value);

    // 关闭TCP连接
    close(sockfd);

    return 0;
}

The above example code uses the read and hold register function in the Modbus protocol to send requests over a TCP connection, receive and parse responses, and finally close the TCP connection. You can modify the parameters such as SERVER, IP, and VNet ID in the code according to the actual situation, as well as the address and quantity of the read hold register.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 502

#define SERVER_IP "192.168.0.100"
#define DEVICE_ID 1

#define BUFFER_SIZE 1024

int main() {
    // 创建TCP连接
    int sockfd;
    struct sockaddr_in server_addr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        printf("Failed to create socket: %sn", strerror(errno));
        return 1;
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        printf("Failed to connect to server: %sn", strerror(errno));
        close(sockfd);
        return 1;
    }

    printf("Connected to server successfully.n");

    // 准备modbus请求
    unsigned char request[BUFFER_SIZE];
    unsigned char response[BUFFER_SIZE];
    int request_length, response_length;

    // 读保持寄存器
    int register_address = 0;
    int register_count = 4;
    request_length = modbus_build_request(request, DEVICE_ID, MODBUS_FC_READ_HOLDING_REGISTERS, register_address, register_count);

    // 发送modbus请求
    if (send(sockfd, request, request_length, 0) == -1) {
        printf("Failed to send request: %sn", strerror(errno));
        close(sockfd);
        return 1;
    }

    printf("Sent request successfully.n");

    // 接收modbus响应
    response_length = recv(sockfd, response, BUFFER_SIZE, 0);
    if (response_length == -1) {
        printf("Failed to receive response: %sn", strerror(errno));
        close(sockfd);
        return 1;
    }

    printf("Received response successfully.n");

    // 解析modbus响应
    int register_value = modbus_get_response_value(response, response_length);

    printf("Register value: %dn", register_value);

    // 关闭TCP连接
    close(sockfd);

    return 0;
}
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.