read method

SerialReadEvent read(
  1. int len,
  2. int timeout
)

Reads up to len number of bytes from the serial port with the specified millisecond timeout.

timeout can be positive for a blocking read with atimeout in milliseconds, zero for a non-blocking read, or negative for a blocking read that will block until length number of bytes are read. For a non-blocking or timeout-bound read, this method may return less than the requested number of bytes. For a blocking read with the VMIN setting configured, this method will block until at least VMIN bytes are read. For a blocking read with both VMIN and VTIME settings configured, this method will block until at least VMIN bytes are read or the VTIME interbyte timeout expires after the last byte read. In either case, this method may return less than the requested number of bytes. timeout can be positive for a blocking read with a timeout in milliseconds, zero for a non-blocking read, or negative for a blocking read.

Returns a 'ReadEvent' containing the number of bytes read and a bytes array on success, false on timeout.

Implementation

SerialReadEvent read(int len, int timeout) {
  _checkStatus();

  var data = malloc<Uint8>(len);
  try {
    var dataRead =
        _checkError(_nativeSerialRead(_serialHandle, data, len, timeout));
    return SerialReadEvent(dataRead, data);
  } finally {
    malloc.free(data);
  }
}