Solved

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

Posted on 2006-11-24
9
308 Views
Last Modified: 2008-03-03
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
0
Comment
Question by:hawkeen
  • 3
  • 2
9 Comments
 
LVL 5

Expert Comment

by:DEEPESH
ID: 18010522
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 18010525
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
 

Author Comment

by:hawkeen
ID: 18010548
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:DEEPESH
ID: 18010659
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
 

Author Comment

by:hawkeen
ID: 18013146

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
 
LVL 5

Accepted Solution

by:
DEEPESH earned 500 total points
ID: 18026336
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now