【发布时间】:2017-08-26 14:24:36
【问题描述】:
我正在尝试读取来自 arduino 的串行数据,但是当我运行我的程序时,它只读取缓冲区中的所有数据,即在程序启动之前实际发送的数据。然后 i 以以下错误终止:
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();
}
据我所知,读取功能应该是阻塞的,所以它等待下一个输入,对吧?那么怎么才能到达“文件”的末尾呢?
【问题讨论】:
-
您好,我也遇到了同样的问题。更糟糕的是,昨天运行的 binary 给出了与今天相同的问题。可能是由于一些超时设置。
-
所以我今天再次运行完全相同的代码,编译过程完全相同,现在它神奇地工作了......
标签: c++ boost serial-port boost-asio