Closed WolfMoestl closed 5 years ago
Hey WolfMoestl,
Thanks for your detailed request. I understand you would like to be able to use the library in a synchronous fashion. Dart is single threaded and locking that thread while waiting for serial communications is not optimal.
Looking at this request I would approach it with:
The StreamTransformer allows you to split incoming data on desired terminator and StreamQueue allows you to use await to get the next available full message.
If I have time I'll poke at it, but maybe this gets you started?
Have a look at the https://github.com/altera2015/usbserial/tree/transaction branch. It should do just what you are looking for. I haven't been able to test this with a real serial device yet as I had none at hand, so all my tests were done with the EchoPort instead of UsbPort.
Example on how to use are in the code and in the tests. https://github.com/altera2015/usbserial/blob/transaction/test/test.dart
Version 0.2.0 now has support for a 'synchronous', using the await keyword, way of interacting with the underlying serial device.
Thanks for implementing this!
Just 2 things:
transaction.dart lines 13 & 14: it's usually '\r\n\', not the other way round as you used it. If one just copies the String you will never get any message back as the usual combination probably used by the device is '\r\n'.
IMHO the returned message should not contain the delimter used (e.g. '\r\n'), so please strip it.
Beside that: works great!
In version 0.1.1
usb_serial
supports asynchronous communications, but not synchronous ones.In my case I need kind of a handshake-type synchronous communication: whenever something is written to the attached USB device I need to know the response from the device for that specific command.
The feature request is the following: When sending a String in synchronous manner...
"\r\n"
) or the maximum amount of milliseconds (configureable) has elapsedNULL
if the device did not response in timeThe point is: I have to wait for the response prior to sending the next command.
So I would like so see interfaces in the
UsbPort
class like:defaultStopSequence
andmaximumWaitTime
should have reasonable defaults (e.g."\n"
and100
).In my case I need different
maximumWaitTimes
for different calls to de attached device, thus I need the option to specify it in thewriteWithRead
call when sending commands which are taking more time on the attached device (e.g. "RESET" needs much more time then "GET_STATUS").A mixture of sync/async USB communications should be suppored as well: Code using this library should be able to:
port.writeWithRead(..)
callsport.write(...)
callsIn fact what I need to do is: