Linux C 串口应用程序:基础框架、数据读写与解析

本文将介绍使用 Linux C 语言编写一个串口应用程序,涵盖以下几个方面:

  1. 应用程序框架
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>

#define SERIAL_PORT_1 "/dev/ttyS0"
#define SERIAL_PORT_2 "/dev/ttyS1"
#define SERIAL_PORT_3 "/dev/ttyS2"

int open_serial_port(const char* port) {
    int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        perror("open_serial_port");
        return -1;
    }

    fcntl(fd, F_SETFL, 0);
    struct termios options;
    tcgetattr(fd, &options);

    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);

    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_cflag &= ~CRTSCTS;

    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_iflag &= ~(IXON | IXOFF | IXANY);

    options.c_oflag &= ~OPOST;

    options.c_cc[VMIN] = 1;
    options.c_cc[VTIME] = 0;

    tcsetattr(fd, TCSANOW, &options);

    return fd;
}

int main() {
    int fd1 = open_serial_port(SERIAL_PORT_1);
    int fd2 = open_serial_port(SERIAL_PORT_2);
    int fd3 = open_serial_port(SERIAL_PORT_3);

    if (fd1 == -1 || fd2 == -1 || fd3 == -1) {
        return -1;
    }

    while (1) {
        // 从串口1读取数据
        // ...

        // 向串口2写入数据
        // ...

        // 从串口2读取数据
        // ...

        // 向串口3写入数据
        // ...

        // 从串口3读取数据
        // ...

        // 向串口1写入数据
        // ...
    }

    close(fd1);
    close(fd2);
    close(fd3);

    return 0;
}
  1. 从串口读取数据并解析
// 从串口读取数据
char buf[256];
int len = read(fd1, buf, sizeof(buf));
if (len > 0) {
    // 解析数据
    // ...
}
  1. 向串口写入数据
// 向串口写入数据
char msg[] = 'Hello, world!';
write(fd2, msg, strlen(msg));
  1. 完整代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>

#define SERIAL_PORT_1 "/dev/ttyS0"
#define SERIAL_PORT_2 "/dev/ttyS1"
#define SERIAL_PORT_3 "/dev/ttyS2"

int open_serial_port(const char* port) {
    int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        perror("open_serial_port");
        return -1;
    }

    fcntl(fd, F_SETFL, 0);
    struct termios options;
    tcgetattr(fd, &options);

    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);

    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_cflag &= ~CRTSCTS;

    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_iflag &= ~(IXON | IXOFF | IXANY);

    options.c_oflag &= ~OPOST;

    options.c_cc[VMIN] = 1;
    options.c_cc[VTIME] = 0;

    tcsetattr(fd, TCSANOW, &options);

    return fd;
}

int main() {
    int fd1 = open_serial_port(SERIAL_PORT_1);
    int fd2 = open_serial_port(SERIAL_PORT_2);
    int fd3 = open_serial_port(SERIAL_PORT_3);

    if (fd1 == -1 || fd2 == -1 || fd3 == -1) {
        return -1;
    }

    while (1) {
        // 从串口1读取数据
        char buf[256];
        int len = read(fd1, buf, sizeof(buf));
        if (len > 0) {
            // 解析数据
            // ...
        }

        // 向串口2写入数据
        char msg[] = 'Hello, world!';
        write(fd2, msg, strlen(msg));

        // 从串口2读取数据
        len = read(fd2, buf, sizeof(buf));
        if (len > 0) {
            // 解析数据
            // ...
        }

        // 向串口3写入数据
        write(fd3, buf, len);

        // 从串口3读取数据
        len = read(fd3, buf, sizeof(buf));
        if (len > 0) {
            // 解析数据
            // ...
        }

        // 向串口1写入数据
        write(fd1, buf, len);
    }

    close(fd1);
    close(fd2);
    close(fd3);

    return 0;
}
Linux C 串口应用程序:基础框架、数据读写与解析

原文地址: https://www.cveoy.top/t/topic/nTIo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录