简体   繁体   中英

How to Use Serial Port in Multiple Threads in Rust?

I am trying to read and write to my serial port on Linux to communicate with a microcontroller and I'm trying to do so in Rust.

My normal pattern when developing in say C++ or Python is to have two threads: one which sends requests out over serial periodically and one which reads bytes out of the buffer and handles them.

In Rust, I'm running into trouble with the borrow checker while using the serial crate. This makes sense to me why this is, but I'm unsure what designing for an asynchronous communication interface would look like in Rust. Here's a snippet of my source:

let mut port = serial::open(&device_path.as_os_str()).unwrap();
let request_temperature: Vec<u8> = vec![0xAA];

thread::spawn(|| {
    let mut buffer: Vec<u8> = Vec::new();
    loop {
        let _bytes_read = port.read(&mut buffer);
        // process data
        thread::sleep(Duration::from_millis(100));
    }
});

loop {
    port.write(&request_temperature);
    thread::sleep(Duration::from_millis(1000));
}

How can I emulate this functionality where I have two threads holding onto a mutable resource in Rust? I know that since this specific example could be done in a single thread, but I'm thinking for an eventual larger program this would end up being multiple threads.

You can wrap your port in a Arc and a Mutex , then you can write something like:

use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

struct Port;
impl Port {
    pub fn read(&mut self, _v: &mut Vec<u8>) {
        println!("READING...");
    }
    pub fn write(&mut self, _v: &Vec<u8>) {
        println!("WRITING...");
    }
}

pub fn main() {
    let mut port = Arc::new(Mutex::new(Port));
    let p2 = port.clone();

    let handle = thread::spawn(move || {
        let mut buffer: Vec<u8> = Vec::new();
        for j in 0..100 {
            let _bytes_read = p2.lock().unwrap().read(&mut buffer);
            thread::sleep(Duration::from_millis(10));
        }
    });

    let request_temperature: Vec<u8> = vec![0xAA];
    for i in 0..10 {
        port.lock().unwrap().write(&request_temperature);
        thread::sleep(Duration::from_millis(100));
    }

    handle.join();
}

So that this will run on a test machine, I've replaced the serial port with a stub class, reduced the sleeps and replaced the infinite loop with some finite loops.

While this works, you'll probably actually want proper communication between the threads at some stage, at which point you'll want to look at std::sync::mpsc::channel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM