views:

361

answers:

1

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?

+2  A: 

The exception has a very specific meaning, it tells you that somebody already has the port opened. Who could that be? Triple-check four times that you are really using a different port name when you open the 2nd one.

Next step is to take the USB emulator to the parking lot and run over it with your car several times so it can no longer drive a programmer nuts. Get one from another manufacturer that uses a different device driver supplier.

Hans Passant
The parking lot comment helped :-) it turned out my closures caused the same port to be opened again. Stupid me! :( Thanks!
Kamiel Wanrooij
+1 for the USB device destruction tip. All USB to serial devices are most definitely NOT made equal.
Andy