Trouble correctly interpreting serial data

efficient based on what? Memory usage? Perhaps. GC avoidance? Possibly. Serial port usage? Perhaps not.

My point was that when you enter a DataRecieved event you have a known number of bytes to read. There’s no point trying to read more than that, at the point in time the DR handler is invoked you will then have to wait for the next data to come in or a timeout to occur before the handler finishes.

You declare the array outside the handler, that’s ok. You probably need to consider what happens when you get more data than that array can handle, how will you read it (ie multiple passes?) and what the port will do in that scenario, because it won’t invoke the DR handler again until fresh data comes in. When you get less data than the array you won’t have a problem but you currently will hang around for longer than necessary, and in GPS data terms you need to parse it as quick as possible…

So one possible optimisation is simply to read the minimum of your array size or the bytes to read. That way you drain the port and don’t step over the array size.

Parsing the values is always going to be the biggest challenge in my view. Well, doing that quickly and efficiently and not causing GC to go ballistic, that’s the challenge. :slight_smile:

@ Brett - Thanks for the explanation. I was thinking only of memory usage and GC but clearly there are other ways I can make my code more efficient, which is great.