jpnurmi / flutter_libserialport

Serial Port for Flutter
https://pub.dev/packages/flutter_libserialport
MIT License
139 stars 80 forks source link

read not working on Windows #104

Open letisoft opened 1 month ago

letisoft commented 1 month ago

Hello, this works: SerialPortReader reader = SerialPortReader(_port);

//reader.stream.
reader.stream.listen((data) {
  print(data);
});

this doesn't work: Uint8List read = _port.read(1,timeout: 100000);

Of course I am not using both in the same time :)

any idea why?

Best regards: V

letisoft commented 1 month ago

Hello,

I am not sure this lib works at all on Windows. Does anyone ever got it working on Windows 11?

as simple as this: SerialPort sp = SerialPort(ports[0]); List bytes = [0x39]; sp.write(Uint8List.fromList(bytes)); Uint8List read = sp.read(10); print(read.length);

same device is just fine with RelaTerm for example Best regards: V

markszente commented 1 month ago

It seems I can only read when I open the port using openRead, but not openReadWrite. Too bad.

micheljung commented 4 weeks ago

Trying to get it work on Windows 11, too. Currently getting:

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)

No idea if it's me or the library but I'm using openReadWrite as well.

Update: Apparently, that's libserialport#83

micheljung commented 4 weeks ago

I finally got it to work.

From the documentation:

When a new configuration is created, all of its settings are initially set to the special -1 value.

You should always configure all settings before using a port. There are no default settings applied by the library. When you open a port, it may have default settings from the OS or driver, or the settings left over by the last program to use it.

Also note that:

Configurations must be disposed using dispose().

So I updated my code and set all settings explicitly:

    var port = SerialPort(address);
    check(port.openReadWrite(), "Could not open for read/write: $port");
    var config = SerialPortConfig();
    config.baudRate = 115200;
    config.bits = 8;
    config.parity = SerialPortParity.none;
    config.stopBits = 1;
    config.xonXoff = 0;
    config.rts = 1;
    config.cts = 0;
    config.dsr = 0;
    config.dtr = 1;
    port.config = config;
    config.dispose();

And it worked! When rts and dtr weren't set correctly, I had the behavior that no bytes were received.