how do you loop through available com ports until the correct one is found?
Posted on 2006-11-24
I have a serial port device and I want to know how to loop through the available com ports and programmically choose the correct com port. The reason I want to do this stems from having some hardware & app and I do not want the users having to select which comm port to connect to but instead have it automatically connect. Most client users do not have a clue about comm ports.
The problem is not so easy to solve. I am using visual studio 2005 and it has built in serial port capabailities. However, per microsofts documentation, the main gui thread can open/send data to the serial port but a worker thread will poll the serial port and fire when data is received from the serial port. You have to capture this event by using DataReceived and invoking via delegate. I have been successful when you open/close serialport instance just once. Now when I try to do so multiple times in an attempt to poll the other comm ports, I get application hanging. Could someone post a suggestion of how to tackle this or a small code block??