我想读取来自arduino的串行数据,但是当我运行我的程序时,它只读取缓冲区中的所有数据,这是实际在程序启动之前发送的数据。然后我终止与以下错误:提升asio串口“文件结尾”
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();
}
据我所知read函数应该阻塞,所以它等待下一个输入,对吧?那么它怎么能到达“文件”的末尾?
嗨,我有同样的问题。更糟糕的是,昨天工作的*二进制*会带来与现在一样的问题。可能由于一些超时设置。 –
所以我今天再次运行完全相同的代码,使用完全相同的编译过程,现在它神奇地工作... –