Struct usbd_serial::SerialPort

source ·
pub struct SerialPort<'a, B, RS = DefaultBufferStore, WS = DefaultBufferStore>
where B: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>,
{ /* private fields */ }
Expand description

USB (CDC-ACM) serial port with built-in buffering to implement stream-like behavior.

The RS and WS type arguments specify the storage for the read/write buffers, respectively. By default an internal 128 byte buffer is used for both directions.

Implementations§

source§

impl<'a, B> SerialPort<'a, B>
where B: UsbBus,

source

pub fn new<'alloc: 'a>( alloc: &'alloc UsbBusAllocator<B> ) -> SerialPort<'a, B, DefaultBufferStore, DefaultBufferStore>

Creates a new USB serial port with the provided UsbBus and 128 byte read/write buffers.

source

pub fn new_with_interface_names<'alloc: 'a>( alloc: &'alloc UsbBusAllocator<B>, comm_if_name: Option<&'static str>, data_if_name: Option<&'static str> ) -> SerialPort<'a, B, DefaultBufferStore, DefaultBufferStore>

Same as SerialPort::new, but allows specifying the names of the interfaces

source§

impl<'a, B, RS, WS> SerialPort<'a, B, RS, WS>
where B: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>,

source

pub fn new_with_store<'alloc: 'a>( alloc: &'alloc UsbBusAllocator<B>, read_store: RS, write_store: WS ) -> SerialPort<'a, B, RS, WS>

Creates a new USB serial port with the provided UsbBus and buffer backing stores.

source

pub fn new_with_store_and_interface_names<'alloc: 'a>( alloc: &'alloc UsbBusAllocator<B>, read_store: RS, write_store: WS, comm_if_name: Option<&'static str>, data_if_name: Option<&'static str> ) -> SerialPort<'a, B, RS, WS>

Creates a new USB serial port with the provided UsbBus and buffer backing stores.

source

pub fn line_coding(&self) -> &LineCoding

Gets the current line coding.

source

pub fn dtr(&self) -> bool

Gets the DTR (data terminal ready) state

source

pub fn rts(&self) -> bool

Gets the RTS (request to send) state

source

pub fn write(&mut self, data: &[u8]) -> Result<usize>

Writes bytes from data into the port and returns the number of bytes written.

§Errors
  • WouldBlock - No bytes could be written because the buffers are full.

Other errors from usb-device may also be propagated.

source

pub fn read(&mut self, data: &mut [u8]) -> Result<usize>

Reads bytes from the port into data and returns the number of bytes read.

§Errors

Other errors from usb-device may also be propagated.

source

pub fn flush(&mut self) -> Result<()>

Sends as much as possible of the current write buffer. Returns Ok if all data that has been written has been completely written to hardware buffers Err(WouldBlock) if there is still data remaining, and other errors if there’s an error sending data to the host. Note that even if this method returns Ok, data may still be in hardware buffers on either side.

Trait Implementations§

source§

impl<Bus: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>> ErrorType for SerialPort<'_, Bus, RS, WS>

§

type Error = Error

Error type of all the IO operations on this type.
source§

impl<Bus: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>> Read for SerialPort<'_, Bus, RS, WS>

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
source§

fn read_exact( &mut self, buf: &mut [u8] ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
source§

impl<Bus: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>> ReadReady for SerialPort<'_, Bus, RS, WS>

source§

fn read_ready(&mut self) -> Result<bool, Self::Error>

Get whether the reader is ready for immediately reading. Read more
source§

impl<B, RS, WS> UsbClass<B> for SerialPort<'_, B, RS, WS>
where B: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>,

source§

fn get_configuration_descriptors( &self, writer: &mut DescriptorWriter<'_> ) -> Result<()>

Called when a GET_DESCRIPTOR request is received for a configuration descriptor. When called, the implementation should write its interface, endpoint and any extra class descriptors into writer. The configuration descriptor itself will be written by UsbDevice and shouldn’t be written by classes. Read more
source§

fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str>

Gets a class-specific string descriptor. Read more
source§

fn reset(&mut self)

Called after a USB reset after the bus reset sequence is complete.
source§

fn endpoint_in_complete(&mut self, addr: EndpointAddress)

Called when endpoint with address addr has completed transmitting data (IN packet). Read more
source§

fn control_in(&mut self, xfer: ControlIn<'_, '_, '_, B>)

Called when a control request is received with direction DeviceToHost. Read more
source§

fn control_out(&mut self, xfer: ControlOut<'_, '_, '_, B>)

Called when a control request is received with direction HostToDevice. Read more
source§

fn get_bos_descriptors( &self, writer: &mut BosWriter<'_, '_> ) -> Result<(), UsbError>

Called when a GET_DESCRIPTOR request is received for a BOS descriptor. When called, the implementation should write its blobs such as capability descriptors into writer. The BOS descriptor itself will be written by UsbDevice and shouldn’t be written by classes.
source§

fn poll(&mut self)

Called whenever the UsbDevice is polled.
source§

fn endpoint_setup(&mut self, addr: EndpointAddress)

Called when endpoint with address addr has received a SETUP packet. Implementing this shouldn’t be necessary in most cases, but is provided for completeness’ sake. Read more
source§

fn endpoint_out(&mut self, addr: EndpointAddress)

Called when endpoint with address addr has received data (OUT packet). Read more
source§

fn get_alt_setting(&mut self, interface: InterfaceNumber) -> Option<u8>

Called when the interfaces alternate setting state is requested. Read more
source§

fn set_alt_setting( &mut self, interface: InterfaceNumber, alternative: u8 ) -> bool

Called when the interfaces alternate setting state is altered. Read more
source§

impl<Bus: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>> Write for SerialPort<'_, Bus, RS, WS>

source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, blocking until all intermediately buffered contents reach their destination.
source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer. Read more
source§

fn write_fmt( &mut self, fmt: Arguments<'_> ) -> Result<(), WriteFmtError<Self::Error>>

Write a formatted string into this writer, returning any error encountered. Read more
source§

impl<Bus: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>> WriteReady for SerialPort<'_, Bus, RS, WS>

source§

fn write_ready(&mut self) -> Result<bool, Self::Error>

Get whether the writer is ready for immediately writing. Read more
source§

impl<B, RS, WS> Read<u8> for SerialPort<'_, B, RS, WS>
where B: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>,

§

type Error = UsbError

Read error
source§

fn read(&mut self) -> Result<u8, Self::Error>

Reads a single word from the serial interface
source§

impl<B, RS, WS> Write<u8> for SerialPort<'_, B, RS, WS>
where B: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>,

§

type Error = UsbError

Write error
source§

fn write(&mut self, word: u8) -> Result<(), Self::Error>

Writes a single word to the serial interface
source§

fn flush(&mut self) -> Result<(), Self::Error>

Ensures that none of the previously written words are still buffered

Auto Trait Implementations§

§

impl<'a, B, RS, WS> Freeze for SerialPort<'a, B, RS, WS>
where RS: Freeze, WS: Freeze,

§

impl<'a, B, RS, WS> RefUnwindSafe for SerialPort<'a, B, RS, WS>

§

impl<'a, B, RS, WS> Send for SerialPort<'a, B, RS, WS>
where RS: Send, WS: Send,

§

impl<'a, B, RS, WS> Sync for SerialPort<'a, B, RS, WS>
where RS: Sync, WS: Sync,

§

impl<'a, B, RS, WS> Unpin for SerialPort<'a, B, RS, WS>
where RS: Unpin, WS: Unpin,

§

impl<'a, B, RS, WS> UnwindSafe for SerialPort<'a, B, RS, WS>
where RS: UnwindSafe, WS: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.