How can I listen to multiple Serial Ports asynchronously in C#
Posted
by Kamiel Wanrooij
on Stack Overflow
See other posts from Stack Overflow
or by Kamiel Wanrooij
Published on 2010-04-13T16:36:57Z
Indexed on
2010/04/13
17:33 UTC
Read the original article
Hit count: 584
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?
© Stack Overflow or respective owner