Function serial::open [] [src]

pub fn open<T: AsRef<OsStr> + ?Sized>(port: &T) -> Result<SystemPort>

A convenience function for opening a native serial port.

The argument must be one that's understood by the target operating system to identify a serial port. On Unix systems, it should be a path to a TTY device file. On Windows, it should be the name of a COM port.


This function returns an error if the device could not be opened and initialized:

  • NoDevice if the device could not be opened. This could indicate that the device is already in use.
  • InvalidInput if port is not a valid device name.
  • Io for any other error while opening or initializing the device.


Provide a system-specific string that identifies a serial port:

let port = serial::open("/dev/ttyUSB0").unwrap();

Hard-coding the device name dimishes the cross-platform utility of serial::open(). To preserve cross-platform functionality, device names should come from external sources:

use std::env;

for arg in env::args_os().skip(1) {
    let port = serial::open(&arg).unwrap();