Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read throws exception when sp_nonblocking_read returns negative number in case of error #83

Open
cabbi opened this issue Jan 30, 2024 · 3 comments

Comments

@cabbi
Copy link

cabbi commented Jan 30, 2024

This piece of code in util.dart does not handle error code in case sp_nonblocking_read fails

  static Uint8List read(int bytes, UtilFunc<ffi.Uint8> readFunc) {
    return ffi.using((arena) {
      final ptr = arena<ffi.Uint8>(bytes);
      final len = call(() => readFunc(ptr));
      return Uint8List.fromList(ptr.asTypedList(len));
    });
  }

This will fix the error:

      return Uint8List.fromList(ptr.asTypedList(len < 0 ? 0 : len));
@vinsfortunato
Copy link

I can confirm having this issue too. A workaround is to check for bytesAvailable before calling the read method in non-blocking mode:

// Check for bytes available before calling read.
if (serialPort.bytesAvailable > 0) {
   var bytes = serialPort.read(amountToRead);
   // Do something with the read bytes
}

However this is definitely not ideal and removing the root cause, as suggested by @cabbi would be better.

@cabbi
Copy link
Author

cabbi commented Apr 18, 2024

In my case checking for bytesAvailable is not working.

The first read once data is available returns me an empty list and on a second read attempt I get this exception:

errorCode: 996
hashCode: 1152565821
message: "Overlapped I/O event is not in a signaled state."

@micheljung
Copy link

I think I'm hitting this as well:

[ERROR:flutter/runtime/dart_isolate.cc(1107)] Unhandled exception:
Invalid argument(s): length must be in the range [0, 4611686018427387903].
#0      _checkExternalTypedDataLength (dart:ffi-patch/ffi_patch.dart:62:5)
#1      Uint8Pointer.asTypedList (dart:ffi-patch/ffi_patch.dart:727:5)
#2      Util.read.<anonymous closure> (package:libserialport/src/util.dart:49:37)
#3      using (package:ffi/src/arena.dart:124:31)
#4      Util.read (package:libserialport/src/util.dart:46:16)
#5      _SerialPortReaderImpl._waitRead (package:libserialport/src/reader.dart:145:27)
#6      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:300:17)
#7      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

len is -2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants