小编Ion*_*lse的帖子

为什么我无法使用 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。我希望这是一个跨平台的应用程序,所以我没有使用提供的 …

io serial-port rust

5
推荐指数
1
解决办法
862
查看次数

标签 统计

io ×1

rust ×1

serial-port ×1