Appearance
Linux 串口限时读取
示例
c
#define _DEFAULT_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
static int read_once(int fd)
{
// 1. 等到有数据或 1 秒窗口结束。断开、超时和错误分别返回。
struct pollfd port = {.fd = fd, .events = POLLIN};
const int ready = poll(&port, 1, 1000);
if (ready < 0) { perror("poll"); return 1; }
if (ready == 0) { puts("timeout"); return 2; }
if (!(port.revents & POLLIN))
{
fprintf(stderr, "serial is unavailable: revents=0x%x\n", (unsigned)port.revents);
return 1;
}
// 2. 有可读事件再读取。把本次拿到的字节按长度打印。
uint8_t data[64];
const ssize_t length = read(fd, data, sizeof(data));
if (length < 0) { perror("read"); return 1; }
if (length == 0) { fputs("serial closed\n", stderr); return 1; }
for (ssize_t i = 0; i < length; ++i)
{
printf("%02X%s", (unsigned)data[i], i + 1 == length ? "\n" : " ");
}
return 0;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "usage: %s /dev/ttyUSB0\n", argv[0]);
return 1;
}
// 1. 非阻塞打开,避免驱动在 open 阶段等待载波。保留旧配置供退出时恢复。
const int fd = open(argv[1], O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
if (fd < 0) { perror("open"); return 1; }
struct termios original;
if (tcgetattr(fd, &original) < 0)
{
perror("tcgetattr");
close(fd);
return 1;
}
// 2. 配置原始 115200 8N1,并关闭软硬件流控。
struct termios options = original;
cfmakeraw(&options);
options.c_cflag = (options.c_cflag & ~(CSIZE | PARENB | CSTOPB | CRTSCTS)) | CS8 | CLOCAL | CREAD;
options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
if (cfsetispeed(&options, B115200) < 0 || cfsetospeed(&options, B115200) < 0 ||
tcsetattr(fd, TCSANOW, &options) < 0)
{
perror("configure serial");
close(fd);
return 1;
}
// 3. 读取结束后恢复原有配置,再关闭文件描述符。
int result = read_once(fd);
if (tcsetattr(fd, TCSANOW, &original) < 0)
{
perror("restore serial");
result = 1;
}
close(fd);
return result;
}执行和验证
使用 termios 把设备配置为 115200 8N1,poll() 等待可读。有数据时按十六进制输出,1 秒没有数据时输出 timeout,退出码为 2。设备错误的退出码为 1。
在 Linux 仓库根目录运行:
sh
mkdir -p .work
cc -std=c11 -Wall -Wextra -Werror examples/linux/serial-timeout/main.c -o .work/serial-timeout
./.work/serial-timeout /dev/ttyUSB0先关掉抢占同一设备的串口工具。使用已接好共地的 3.3 V 适配器或配置正确的虚拟串口,核对设备名和访问权限。正常条件下对端可发送 41 00 7E。一次 read() 返回本次已经可读的字节数,连续调用才可能收齐完整报文。
预测题 如果发完前两个字节后停顿,程序会等待第三个字节吗?
改动题 将等待窗口改成 200 ms,用空闲设备重新运行。
核对改法
一次可读事件只负责读当前的数据,不会等待固定帧长。把 poll() 的第三个参数改为 200 后,空闲窗口约为 200 ms,正常超时仍返回 2。
Linux termios 手册 · Linux poll 手册
验证记录
已在 Alpine 3.22.8 的 WSL 环境编译并运行。伪终端验证了空等超时、41 00 7E 的原样读取及退出后恢复原配置。实体串口与天空星接线待实测。