Skip to content

Commit

Permalink
SerialPortReader: report stream errors
Browse files Browse the repository at this point in the history
This does not a solution to #4 as it does not handle re-subscribtion
or so, just helps to see what's going on.

Ref: #4
  • Loading branch information
jpnurmi committed Sep 26, 2020
1 parent a8db051 commit 2bb77a0
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/src/reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'dart:typed_data';
import 'package:ffi/ffi.dart' as ffi;
import 'package:dart_serial_port/src/bindings.dart';
import 'package:dart_serial_port/src/dylib.dart';
import 'package:dart_serial_port/src/error.dart';
import 'package:dart_serial_port/src/port.dart';
import 'package:dart_serial_port/src/util.dart';

Expand Down Expand Up @@ -88,7 +89,13 @@ class _SerialPortReaderImpl implements SerialPortReader {

void _startRead() {
_receiver = ReceivePort();
_receiver.listen((data) => _controller.add(data));
_receiver.listen((data) {
if (data is SerialPortError) {
_controller.addError(data);
} else {
_controller.add(data);
}
});
final args = _SerialPortReaderArgs(
address: _port.address,
timeout: _timeout,
Expand Down Expand Up @@ -119,6 +126,9 @@ class _SerialPortReaderImpl implements SerialPortReader {
return dylib.sp_nonblocking_read(port, ptr.cast(), bytes);
});
args.sendPort.send(data);
} else if (bytes < 0) {
args.sendPort.send(SerialPortError(
SerialPort.lastErrorMessage, SerialPort.lastErrorCode));
}
}
_releaseEvents(events);
Expand Down

0 comments on commit 2bb77a0

Please sign in to comment.