Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Android/hardware/hardware/nxp/nxp_mcu_loader/src/   (Android Betriebssystem Version 17©)  Datei vom 26.5.2026 mit Größe 2 kB image not shown  

Quelle  uart.rs

  Sprache: Rust
 

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 V0.5 in Prozent
C=94 H=94 G=93

¤ Dauer der Verarbeitung: 0.11 Sekunden  (vorverarbeitet am  2026-06-28) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.