Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Android/hardware/hardware/nxp/nxp_mcu_loader/     Datei vom 26.5.2026 mit Größe 2 kB image not shown  

Quelle  uart.rs  Sprache: unbekannt

 
use crate::transport::{to_timeout, Transport};
use std::io;
use std::path::Path;
use std::time::Instant;

pub const DEFAULT_BAUD_RATE: u32 = 57600;

pub struct UartTransport {
    port: Box<dyn serialport::SerialPort>,
}

impl Transport for UartTransport {
    fn read(&mut self, buf: &mut [u8], deadline: Instant) -> io::Result<()> {
        self.read_until(buf, deadline)
    }

    fn write(&mut self, buf: &[u8], deadline: Instant) -> io::Result<()> {
        self.write_until(buf, deadline)
    }
}

impl UartTransport {
    pub fn new(path: impl AsRef<Path>, baud_rate: Option<u32>) -> io::Result<Self> {
        let path = path.as_ref().to_str().ok_or_else(|| {
            io::Error::new(io::ErrorKind::InvalidInput, "Path is not valid unicode")
        })?;
        let baud_rate = baud_rate.unwrap_or(DEFAULT_BAUD_RATE);

        let port = serialport::new(path, baud_rate)
            .preserve_dtr_on_open() // TIOCMBIS -> ENOTTY on pts
            .open()?;

        Ok(Self { port })
    }

    fn read_until(&mut self, buf: &mut [u8], deadline: Instant) -> io::Result<()> {
        let mut cursor = buf;
        while !cursor.is_empty() {
            self.set_deadline(deadline)?;
            let n = self.port.read(cursor)?;
            if n == 0 {
                return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
            }
            assert!(n <= cursor.len());
            cursor = &mut cursor[n..];
        }
        Ok(())
    }

    fn write_until(&mut self, buf: &[u8], deadline: Instant) -> io::Result<()> {
        let mut cursor = buf;
        while !cursor.is_empty() {
            self.set_deadline(deadline)?;
            let n = self.port.write(cursor)?;
            if n == 0 {
                return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
            }
            assert!(n <= cursor.len());
            cursor = &cursor[n..];
        }
        Ok(())
    }

    fn set_deadline(&mut self, deadline: Instant) -> io::Result<()> {
        Ok(self.port.set_timeout(to_timeout(deadline)?)?)
    }
}

Messung
CHZG
819510092

[zur Elbe Produktseite wechseln0.24QuellennavigatorsAnalyse erneut starten2026-11-21]