FengChendian / serial_port_win32

A flutter SerialPort library using win32 API.
BSD 3-Clause "New" or "Revised" License
30 stars 8 forks source link

Help getting serial data as stream in flutter from arduino #22

Open CadMunkey opened 1 year ago

CadMunkey commented 1 year ago

Hi, I'm very new to serial communication and Streams for that matter. However, as a little project I'm trying to listen to a serial feed from an arduino. connected via USB.

I've initially used your sample code and the data comes across fine.

I have then mutilated said code into what is shown below. I'm using flutter riverpod for state management. When I look into the source code I can see you are already using streams but my knowledge is lacking on how to access this data.

I don't get any errors but the app just hangs. Any help is much much appreciated.

` final arduinoProvider = StreamProvider((ref) async* { final arduinoData = await fetchArduinoData();

yield arduinoData; });

Future fetchArduinoData() async { final port = SerialPort("COM3", openNow: false, ByteSize: 8);

port.openWithSettings(BaudRate: 9600); port.writeBytesFromString('A'); String incomingData = ''; String completeData = '';

while (port.isOpened && completeData == '') { incomingData = ''; port.readOnListenFunction = (value) { if (utf8.decode(value) != '>' && utf8.decode(value) != '\n') { incomingData += utf8.decode(value); } if (utf8.decode(value) == '\n') { completeData = incomingData; } }; } return completeData; }`

FengChendian commented 1 year ago

I am very sorry to reply so late. I've been busy with my PhD project. Maybe you have fixed the problem.

It looks like a stream asynchronous problem. I think that it's due to await is not passed nested.

But I never use stream like this. There is nothing I can do to help further. Sorry.