为什么我不能使用 Rust 接收串行数据?

Why can I not recieve serial data using Rust?

我一直在尝试从 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() 函数只是扫描打开的端口和 returns 我想要连接的端口。我在 Windows,所以在这种情况下通常是 COM9COM12。我希望这是一个跨平台的应用程序,所以我没有使用 serialport 提供的 open_native() 函数。

我尝试将缓冲区的大小从 1 字节更改为 1000,我尝试将不同版本的 read 放入端口,我尝试跳过超时错误,并且我已经尝试直接将读取的字节输出到 io::Stdout。有什么想法吗?

显然,我使用的 serialport crate 需要您设置命令

port.write_data_terminal_ready(true);

以便它开始读取数据。在 Linux 上,如果没有它,它工作得很好。花了 4 个小时试图改变我使用的 IO reader。