Ras*_*und 2 c++ boost serial-port boost-asio
我正在尝试读取来自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)
Run Code Online (Sandbox Code Playgroud)
我什至不希望程序执行之前的数据。这是我的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();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,读取功能应该被阻塞,所以它等待下一个输入,对吗?那么如何到达“文件”的末尾?
这对我有用。您需要给串行端口一个最小字符限制,以具有阻止功能。
stty -F /dev/ttyUSB5 115200 line 0 min 1 time 0
Run Code Online (Sandbox Code Playgroud)
编辑:更好的选择似乎只是将端口设置为原始模式。
stty -F /dev/ttyUSB0 raw
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
867 次 |
| 最近记录: |