Managing multiple UART ports

This is perhaps more of a general programming question, but as I’m implementing this with TinyCLR I figured there might be some “best practices” I should be aware of.

Basically, I have an application where I need to read data from multiple UART ports simultaneously. Latency is also important to me; I’d like to read the data and process it as fast as is feasible (I.E. avoid long timeouts.)

Essentially, I’m just looking for any thoughts or suggestions. Should I use a separate thread for receiving and processing each port? Should I read one byte each loop from each port and build their messages concurrently? Any tricks in the TinyCLR UART implementation I should know about?

Thanks!

Reading the ports round-robin style and then sending off completed messages to be handled will be most efficient (roughly same number of CPU cycles per read cycle, except that threads will add the overhead of N context switches for N ports).

The real challenge is what you do with the data. TinyCLR runtimes are single-core, so whether you use threads or not, the processing of completed messages/packets will delay reading from the ports. This is where at least using a thread for the message processing is a good choice because that cost is then spread across time slices (which I seem to recall are 18mS or less each, by my recollection here is fuzzy).

So my recommendation : one thread for round robin across the ports, plus a thread for message processing. For max overall performance, keep the thread count low.

2 Likes

Thank you! That makes total sense. I appreciate the insight.