首页  编辑  

串口通信如何同步模式超时读取?

Tags: /硬件开发/   Date Created:
在同步模式下,如果发送数据后,对方不返回数据,如何超时返回?

int fd=0;
fd = open(device, O_RDWR | O_NOCTTY);
    fd_set read_fds, write_fds, except_fds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(fd, &read_fds);
// Set timeout to 1.0 seconds
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
if (select(fd + 1, &read_fds, &write_fds, &except_fds, &timeout) != 1) { return -1; // 超时 } else { //正常读取操作 len = read(fd, &res, sizeof(res)); }