ramlife

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

最近在调试 android,使用 串口半双工模式接收数据,碰到的问题是 unistd 中的 read 是block的,怎样才能超时退出。
刚开始找到的资料说是 c_cc[VMIN] = 0, c_cc[VTIME] = 10; 这样就能配置为 1s 超时了。但是实际上并不能够达到超时退出的功能。
后来经过进一步的资料查找,发现需要使用 下面的方法才能实现功能, VTIME 的配置好像没那么重要。

        struct timeval timeout;
        timeout.tv_sec = 0;
        timeout.tv_usec = 100000;
        retval = select(fd + 1, &rfds, NULL, NULL, &timeout);

参考: https://stackoverflow.com/questions/10522277/how-can-i-implement-timeout-for-read-when-reading-from-a-serial-port-c-c
https://linux.die.net/man/2/select
http://man7.org/linux/man-pages/man2/select.2.html
https://blog.mbedded.ninja/programming/operating-systems/linux/linux-serial-ports-using-c-cpp/#reading

posted on 2020-03-06 13:18  ramlife  阅读(310)  评论(0编辑  收藏  举报