I've built a serial logger in c# using the solution from Ben Voigt
https://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport
byte[] buffer = new byte[blockLimit];
Action kickoffRead = null;
kickoffRead = delegate {
port.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar) {
try {
int actualLength = port.BaseStream.EndRead(ar);
byte[] received = new byte[actualLength];
Buffer.BlockCopy(buffer, 0, received, 0, actualLength);
raiseAppSerialDataEvent(received);
}
catch (IOException exc) {
handleAppSerialError(exc);
}
kickoffRead();
}, null);
};
kickoffRead();
All other methods I tried failed in one way or another (Ben Voigt explains extensively why on his excellent page). Now I'm only left with two questions:
1). How do I gracefully stop listening to the serial port? If I close the comport it will raise a nasty exception.
2). Is there an elegant way to raise an event if there is no data recieved for a certain time?