Solved

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

Posted on 2006-11-24
9
302 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

27 Experts available now in Live!

Get 1:1 Help Now