代码之家  ›  专栏  ›  技术社区  ›  Rasmus Tollund

Boost asio串行端口“文件结束”

  •  2
  • Rasmus Tollund  · 技术社区  · 7 年前

    我试图读取来自arduino的串行数据,但当我运行程序时,它只读取缓冲区中的所有数据,即程序启动前实际发送的数据。然后我以以下错误终止:

    terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
    what():  read: End of file
    Aborted (core dumped)
    

    我甚至不想要程序执行前的数据。这是我的c++代码:

    #include <iostream>
    #include <stdint.h>
    
    #include <boost/asio.hpp>
    #include <boost/asio/serial_port.hpp>
    
    using namespace boost;
    
    int main() {
        asio::io_service io;
        asio::serial_port port(io);
    
        port.open("/dev/ttyUSB0");
        port.set_option(asio::serial_port_base::baud_rate(115200));
    
        char d;
    
        while ( true ) {
            asio::read(port, asio::buffer(&d, 1));
            std::cout << d << std::endl;
        }
    
        port.close();
    }
    

    2 回复  |  直到 7 年前
        1
  •  5
  •   Atilla Filiz    7 年前

    这对我来说很有效。您需要为串行端口提供最小字符限制,以具有阻塞能力。

    stty -F /dev/ttyUSB5 115200 line 0 min 1 time 0
    

    编辑 :更好的选择似乎只是将端口设置为原始模式。

    stty -F /dev/ttyUSB0 raw
    
        2
  •  0
  •   Rasmus Tollund    7 年前

    但阿蒂拉·菲利兹的回答解决了这个问题。