【发布时间】:2021-12-28 03:12:54
【问题描述】:
我一直在尝试从 Feather M0 读取串行数据,但由于某种原因,我无法将数据读入缓冲区。该设备肯定会输出串行数据,PlatformIO 和 Arduino IDE 都在各自的串行监视器中显示串行数据。但是,当我在 Rust 中读取它时,它每次都会超时,无论我将它设置为什么超时值。这是我的代码:
// First, find the serial port
let port_info = find_receiver();
// If we didn't find a port, then we can't continue
if port_info.is_none() {
panic!("Could not find a serial port");
}
let mut port = serialport::new(port_info.unwrap().port_name, 9600)
.timeout(Duration::from_millis(1000))
.open()
.expect("Could not open serial port");
let mut serial_buf: Vec<u8> = vec![0; 8];
loop {
let n = port.read_exact(serial_buf.as_mut_slice()).unwrap();
println!("Buffer is {:?}", serial_buf);
}
find_reciever() 函数只是扫描打开的端口并返回我想要连接的端口。我在 Windows 上,所以在这种情况下,它通常是 COM9 或 COM12。我希望这是一个跨平台的应用程序,所以我没有使用serialport 提供的open_native() 函数。
我尝试将缓冲区的大小从 1 字节更改为 1000,我尝试将不同版本的 read 放入端口,我尝试跳过超时错误,我尝试直接输出读取字节到 io::Stdout。有什么想法吗?
【问题讨论】:
标签: rust io serial-port