Solved

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

Posted on 2006-11-24
9
311 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

15 Experts available now in Live!

Get 1:1 Help Now