SerialPort class Null safety

Serial port.

Provides means to:

  • obtaining a list of serial ports on the system
  • opening, closing and getting information about ports
  • signals, modem control lines, breaks, etc.
  • low-level reading and writing data, and buffer management

Note: The port must be disposed using dispose() when done.

Error handling

Serial Port for Dart throws SerialPortError in case something goes wrong under the hood in libserialport and it returns an error code:

final port = SerialPort(...);
  port.write(...);
try {
} on SerialPortError catch (e) {
  print(SerialPort.lastError);
}

The error message is that provided by the OS, using the current language settings. The library does not define its own error codes or messages to accompany other return codes.

See also:

Constructors

SerialPort(String name)
Creates a serial port for name.
factory
SerialPort.fromAddress(int address)
@internal
factory

Properties

address int
@internal
read-only
busNumber int?
Gets the USB bus number of a USB serial adapter port.
read-only
bytesAvailable int
Gets the amount of bytes available for reading.
read-only
bytesToWrite int
Gets the amount of bytes waiting to be written.
read-only
config SerialPortConfig
Gets the current configuration of the serial port.
read / write
description String?
Gets the description of the port, for presenting to end users.
read-only
deviceNumber int?
Gets the USB device number of a USB serial adapter port.
read-only
hashCode int
The hash code for this object.
read-onlyinherited
isOpen bool
Gets whether the serial port is open.
read-only
macAddress String?
Gets the MAC address of a Bluetooth serial adapter port.
read-only
manufacturer String?
Get the USB manufacturer of a USB serial adapter port.
read-only
name String?
Gets the name of the port.
read-only
productId int?
Gets the USB Product ID of a USB serial adapter port.
read-only
productName String?
Gets the USB product name of a USB serial adapter port.
read-only
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited
serialNumber String?
Gets the USB serial number of a USB serial adapter port.
read-only
signals int
Gets the status of the control signals for the serial port.
read-only
transport int
Gets the transport type used by the port.
read-only
vendorId int?
Gets the USB vendor ID of a USB serial adapter port.
read-only

Methods

close() bool
Closes the serial port.
dispose() → void
Releases all resources associated with the serial port.
drain() → void
Waits for buffered data to be transmitted.
endBreak() bool
Takes the port transmit line out of the break state.
flush([int buffers = SerialPortBuffer.both]) → void
Flushes serial port buffers. Data in the selected buffer(s) is discarded.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
open({required int mode}) bool
Opens the serial port in the specified mode.
openRead() bool
Opens the serial port for reading only.
openReadWrite() bool
Opens the serial port for reading and writing.
openWrite() bool
Opens the serial port for writing only.
read(int bytes, {int timeout = -1}) Uint8List
Read data from the serial port.
startBreak() bool
Puts the port transmit line into the break state.
toString() String
A string representation of this object.
inherited
write(Uint8List bytes, {int timeout = -1}) int
Write data to the serial port.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

availablePorts List<String>
Lists the serial ports available on the system.
read-only
lastError SerialPortError?
Gets the error for a failed operation.
read-only