ardera / flutter_packages

My collected packages for pub.dev
MIT License
28 stars 7 forks source link

Advisory on Continuous Read Stream #22

Open mathaou opened 11 months ago

mathaou commented 11 months ago

Hello!

I'm trying to get some form of continuous read from a serial port going. Ideally I just listen to the stream that the SerialPortHandle after open() contains, but that doesn't seem to be working how I expect.

I'm able to read fine, I just want to have a psuedo serial console for the user. Not fully interactive, since the payloads that get written to port are predefined, but I do want to just keep reading and never stop - continually adding to TextField in read only mode.

ardera commented 11 months ago

Hey,

maybe something like this would work:

import 'dart:convert';

// ...

final textBuffer = StringBuffer();

final Stream<String> textStream = serialPortHandle.stream.transform(ascii.decoder);
textStream.listen((chunk) {
  textBuffer.write(chunk);
  setState(() {});
});
ardera commented 11 months ago

though this is probably a common enough use case that I could just add a SerialPortHandle.textStream shortcut for this

mathaou commented 11 months ago

Will try soon and verify functionality.