【问题标题】:Why can I not recieve serial data using Rust?为什么我不能使用 Rust 接收串行数据?
【发布时间】: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 上,所以在这种情况下,它通常是 COM9COM12。我希望这是一个跨平台的应用程序,所以我没有使用serialport 提供的open_native() 函数。

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

【问题讨论】:

    标签: rust io serial-port


    【解决方案1】:

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

    port.write_data_terminal_ready(true);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 2015-04-12
      相关资源
      最近更新 更多