[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2006-11-24
9
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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