为什么我无法使用 Rust 接收串行数据?

Ion*_*lse 5 io serial-port 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);
}
Run Code Online (Sandbox Code Playgroud)

find_reciever()函数只是扫描打开的端口并返回我想要连接的端口。我使用的是 Windows,所以在这种情况下通常是COM9COM12。我希望这是一个跨平台的应用程序,所以我没有使用提供的open_native()功能serialport

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

Ion*_*lse 9

显然,serialport我使用的板条箱需要你设置命令

port.write_data_terminal_ready(true);
Run Code Online (Sandbox Code Playgroud)

为了让它开始读取数据。在 Linux 上,如果没有它,它也可以正常工作。花了 4 个小时尝试更改我正在使用的 IO 读取器。

  • 好吧,我花了两个小时才偶然发现这个答案...... (4认同)