Linux 下 C++ 操作串口并彻底释放控制权的总结

0. 引言

在 Linux 系统下,通过 C++ 操作串口并确保其正确释放控制权,是一个常见需处理的问题。本文将通过实际案例,分析在多线程环境下串口无法释放控制权的原因,并提供解决方案。

1. 问题描述

在使用 C++ 打开并操作串口(例如 /dev/ttyUSB0)时,调用 close(fd) 返回 0,但串口控制权未能被彻底释放。这导致其他程序无法正常访问该串口设备。经排查发现,问题出在读取数据的线程仍在阻塞的 read() 调用中,导致串口无法真正释放。

2. 原因分析

在多线程程序中,文件描述符(fd)是被所有线程共享的。当一个线程在阻塞状态下调用 read(fd, ...) 时,另一个线程尝试关闭该文件描述符(调用 close(fd))会导致以下问题:

  • 阻塞的 read() 调用:读取线程仍在等待数据,无法及时响应关闭信号。
  • 资源未完全释放:由于读取线程仍持有对文件描述符的引用,系统无法完全释放串口资源,导致其他程序无法访问。

3. 解决方案

为确保串口控制权能够被彻底释放,需要在关闭文件描述符前,确保所有使用该串口的线程已停止操作。具体步骤如下:

  • 使用线程同步机制:引入一个线程安全的标志位,通知读取线程停止读取并退出。
  • 中断阻塞的 read() 调用:通过关闭文件描述符,强制阻塞在 read() 的读取线程中断 read() 调用,使其返回错误。
  • 等待读取线程结束:在关闭文件描述符后,使用 std::thread::join() 等待读取线程完成,确保所有资源被正确释放。

4. 代码示例

以下是一个完整的 C++ 示例,展示如何在多线程环境下正确管理串口的打开、读取和关闭,确保串口控制权的彻底释放。代码中的日志统一使用 fprintf 进行打印。

#include <iostream>
#include <fcntl.h>      // For open()
#include <termios.h>    // For termios structures and functions
#include <unistd.h>     // For close()
#include <cstring>      // For memset()
#include <cerrno>       // For errno
#include <thread>       // For std::thread
#include <atomic>       // For std::atomic

// Atomic flag to control the reading thread
std::atomic<bool> keepReading(true);

// Function executed by the reading thread
void readThreadFunction(int fd) {
    char buffer[256];
    while (keepReading.load()) {
        ssize_t bytesRead = read(fd, buffer, sizeof(buffer));
        if (bytesRead > 0) {
            // Process the read data
            fprintf(stdout, "Read %zd bytes: ", bytesRead);
            fwrite(buffer, 1, bytesRead, stdout);
            fprintf(stdout, "\n");
        } else if (bytesRead == -1) {
            if (errno == EINTR) {
                // Interrupted by a signal, continue reading
                continue;
            } else if (errno == EBADF) {
                // File descriptor was closed, exit the loop
                break;
            } else {
                fprintf(stderr, "Read error: %s\n", strerror(errno));
                break;
            }
        } else {
            // EOF reached
            break;
        }
    }
    fprintf(stdout, "Read thread exiting.\n");
}

int main() {
    const char* portname = "/dev/ttyUSB0";  // Serial port device name
    int fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY); // Open the serial port

    if (fd == -1) {
        fprintf(stderr, "Unable to open port %s: %s\n", portname, strerror(errno));
        return -1;
    }

    // Clear the O_NDELAY flag to make read() blocking
    if (fcntl(fd, F_SETFL, 0) == -1) {
        fprintf(stderr, "fcntl failed: %s\n", strerror(errno));
        close(fd);
        return -1;
    }

    struct termios options;
    if (tcgetattr(fd, &options) < 0) {
        fprintf(stderr, "Failed to get attributes: %s\n", strerror(errno));
        close(fd);
        return -1;
    }

    // Set baud rates to 115200
    if (cfsetispeed(&options, B115200) < 0 || cfsetospeed(&options, B115200) < 0) {
        fprintf(stderr, "Failed to set baud rate: %s\n", strerror(errno));
        close(fd);
        return -1;
    }

    // Configure 8N1
    options.c_cflag &= ~PARENB; // No parity
    options.c_cflag &= ~CSTOPB; // 1 stop bit
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;     // 8 data bits

    // Enable the receiver and set local mode
    options.c_cflag |= (CLOCAL | CREAD);

    // Set raw input mode (non-canonical, no echo)
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable software flow control
    options.c_oflag &= ~OPOST;                  // Disable output processing

    // Apply the settings
    if (tcsetattr(fd, TCSANOW, &options) < 0) {
        fprintf(stderr, "Failed to set attributes: %s\n", strerror(errno));
        close(fd);
        return -1;
    }

    fprintf(stdout, "Serial port opened and configured successfully.\n");

    // Start the reading thread
    std::thread reader(readThreadFunction, fd);

    // Simulate main thread work
    fprintf(stdout, "Press Enter to stop reading and close the serial port...\n");
    std::cin.get();

    // Signal the reading thread to stop
    keepReading.store(false);

    // Close the file descriptor to interrupt read()
    if (close(fd) < 0) {
        fprintf(stderr, "Failed to close port: %s\n", strerror(errno));
    }

    // Wait for the reading thread to finish
    if (reader.joinable()) {
        reader.join();
    }

    fprintf(stdout, "Serial port closed and control released.\n");
    return 0;
}

4.1 代码说明

  • 打开串口

    int fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
    
    • O_RDWR: 以读写模式打开。
    • O_NOCTTY: 串口不会成为调用进程的控制终端。
    • O_NDELAY: 打开时不阻塞。
  • 配置串口参数
    使用 termios 结构体设置波特率为 115200,数据位为 8 位,无奇偶校验和 1 位停止位(8N1 模式)。

  • 启动读取线程

    std::thread reader(readThreadFunction, fd);
    

    创建一个新线程来处理串口数据的读取。

  • 关闭串口
    在主线程接收到用户输入后,设置 keepReadingfalse,并关闭文件描述符 fd,这将中断阻塞在 read() 的读取线程。

  • 等待线程结束
    使用 reader.join() 等待读取线程安全退出,确保资源的正确释放。

4.2 关键点解释

  • 线程同步
    使用 std::atomic<bool> 类型的 keepReading 变量,确保主线程与读取线程之间的同步。当主线程需要关闭串口时,通过设置 keepReadingfalse,通知读取线程退出循环。

  • 中断阻塞的 read() 调用
    关闭文件描述符 close(fd) 会导致阻塞在 read(fd, ...) 的读取线程中断 read() 调用,read() 返回 -1errno 被设置为 EBADF。读取线程检测到 EBADF 后,退出循环。

  • 等待线程结束
    使用 std::thread::join() 等待读取线程完成,确保所有资源在释放前已被正确处理。

5. 总结

在 Linux 下使用 C++ 操作串口时,尤其是在多线程环境中,需要特别注意资源的正确管理和线程的同步。通过以下步骤,可以确保串口控制权的正确释放:

  • 使用阻塞式 read()

    • 简化线程同步和资源管理。
    • 通过关闭文件描述符中断 read() 调用,使读取线程能够及时响应并退出。
  • 引入线程同步机制

    • 使用 std::atomic<bool> 或其他线程安全的标志位,通知读取线程停止读取并退出。
  • 等待读取线程结束

    • 使用 std::thread::join() 等方法,确保所有读取线程在关闭串口前已安全退出。

通过遵循上述步骤,可以有效避免串口资源被占用或无法释放的问题,确保系统中其他进程能够正常访问串口设备。

6. 附录:进一步优化建议

虽然本文聚焦于解决多线程读取导致串口无法释放的问题,但在实际应用中,可能还需要考虑以下优化:

  • 使用 RAII 模式管理资源

    • 通过封装串口操作和线程管理在一个类中,利用构造函数和析构函数自动管理资源,提升代码的健壮性和可维护性。
  • 处理异常和错误

    • 在多线程环境中,确保所有可能的异常和错误都被正确捕获和处理,避免资源泄漏和程序崩溃。
  • 使用高级库

    • 考虑使用成熟的串口通信库,如 libserial,这些库封装了更多的细节,提供更可靠的串口管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橘色的喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值