Vote count:
0
I need to parse a stream of serial data coming from a test instrument, and this seems to be an excellent application for Reactive Extensions.
The protocol is very simple...each "packet" is a single letter followed by numeric digits. The number of numeric digits is fixed for each packet type, but can vary between packet types. e.g.
...A1234B123456C12...
I am trying to break this up into an Observable of Strings, e.g.
"A1234" "B123456" "C12" ...
Thought this would be simple, but don't see the obvious way to approach this (I have some experience with LINQ, but am new to Rx.
Here's the code I have so far, that produces Observable of chars
var serialData = Observable
.FromEventPattern<SerialDataReceivedEventArgs>(SerialPortMain, "DataReceived")
.SelectMany(_ =>
{
int dataLength = SerialPortMain.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = SerialPortMain.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return new char[0];
var chars = Encoding.ASCII.GetChars(data);
return chars;
});
How can I transform this to Observable of String, where each string is a packet?
asked 23 secs ago
Aucun commentaire:
Enregistrer un commentaire