how do you loop through available com ports until the correct one is found?

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??  

cheers
Hawk
hawkeenAsked:
Who is Participating?
 
DEEPESHConnect With a Mentor Commented:
Check this link
http://www.devx.com/dotnet/Article/31001/0/page/2
'-------------------------------------------
    Private Sub DataReceived( _
       ByVal sender As Object, _
       ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
       Handles serialPort.DataReceived

        txtDataReceived.Invoke(New _
                       myDelegate(AddressOf updateTextBox), _
                       New Object() {})
    End Sub

0
 
DEEPESHCommented:
Use this code to detect avialable ports at your comp
Imports Sustem.IO.Ports  
 Dim ports As String() = SerialPort.GetPortNames()

        Console.WriteLine("The following serial ports were found:")

        ' Display each port name to the console.
        Dim port As String
        For Each port In ports
            Console.WriteLine(port)
        Next port

        Console.ReadLine()

u will get port name , whic you wnat then use following code

serialPort = New SerialPort()

        ' Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName)
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate)
        _serialPort.Parity = SetPortParity(_serialPort.Parity)
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits)
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits)
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)

        ' Set the read/write timeouts
        _serialPort.ReadTimeout = 500
        _serialPort.WriteTimeout = 500

        _serialPort.Open()
        _continue = True
        _serialPort.writeline(text)
        _serialPort.close
0
 
AlexFMCommented:
See SerialPort.GetPortNames Method.
For every port name returned by this method, create SerialPort instance and try to talk with device. If device is responding, this is port you need.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
hawkeenAuthor Commented:
Deepesh, I do not understand how your code will allow me to loop through all the available comm ports???  After I write to the serialport via serialport.open there is no mention of datareceived event which is on another thread (worker thread separate from the gui thread). Also, I do not want the user setting any properties. I want them all set programmically. I can do this because I will be giving them the hardware device and will know its info.

AlexFM,  I thought about creating an instance of each serialport found in GetPortNames but how will the DataReceived event work?  For example, if you have 2 comm ports, com1 and com2.  I can connect to both via windows but one will simply echo back and the other will send an ACK back letting me know its ready to communicate. Will the Datareceived event via invoke work for each instance of the serial ports? Will there be timing/thread issues due to so many serial port instances? Thoughts?

cheers
Hawk

0
 
DEEPESHCommented:
imports Sustem.IO.Ports  
 Dim ports As String() = SerialPort.GetPortNames()

        Console.WriteLine("The following serial ports were found:")

        ' Display each port name to the console.
        Dim port As String
        For Each port In ports
            Console.WriteLine(port)
        Next port

        Console.ReadLine()
will give you all ports, but to read visit following link
http://www.codeworks.it/net/VBNetRs232.htm
0
 
hawkeenAuthor Commented:

How do you create instances of serialPort to use with DataReceived event handler when the instances have to be "withevents" ??  The solution I am looking for needs to use visual studio 2005 and include the DataReceived event handler.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.