How can I listen to multiple Serial Ports asynchronously in C#
- by Kamiel Wanrooij
I have an application that listens to a piece of hardware on a USB to Serial converter. My application should monitor more than one serial port at the same time.
I loop the serial ports I need to listen to, and create a thread for each port. In the thread I have my data handing routine.
When I assign one port, it runs flawlessly. When I listen to the other one, it also works. When I open both ports however, the second port always throws an UnauthorizedAccessException when calling serialPort.Open(). It does not matter in what order I open the ports, the second one always fails.
I listen to the ports using serialPort.ReadLine() in a while loop.
Can .NET open more than one port at the same time? Can I listen to both? Or should I use another (thread safe?) way to access my serial port events?