Ports... Should be simple?
Ports... Should be simple?
Ports... Should be simple?
Ports... Should be simple?
Ports... Should be simple?
Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple?
Ports... Should be simple? Ports... Should be simple?
Ports... Should be simple?
Go Back  Xtreme Visual Basic Talk > > > Ports... Should be simple?


Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2010, 06:21 PM
axle12693 axle12693 is offline
Freshman
 
Join Date: May 2009
Posts: 26
Default Ports... Should be simple?


Why isn't this seemingly simple piece of code working?

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each sp As String In My.Computer.Ports.SerialPortNames
            ListBox1.Items.Add(sp)
        Next
    End Sub
Reply With Quote
  #2  
Old 07-13-2010, 06:37 PM
PlausiblyDamp's Avatar
PlausiblyDampPorts... Should be simple? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Any chance you could give a little more detail regarding what it is doing compared with what you expect it to do?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 07-13-2010, 06:54 PM
AtmaWeapon's Avatar
AtmaWeaponPorts... Should be simple? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Are you running Windows 7 64-bit?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 07-13-2010, 07:09 PM
axle12693 axle12693 is offline
Freshman
 
Join Date: May 2009
Posts: 26
Default

I expect it to display (in ListBox1) a list of all the serial ports on my computer.

Instead, it just does absolutely nothing. The window sits there with the first - blank - element in ListBox1 selected.

EDIT

And yes, I'm running W7 64-bit
Reply With Quote
  #5  
Old 07-13-2010, 08:18 PM
AtmaWeapon's Avatar
AtmaWeaponPorts... Should be simple? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

There is a known issue on 64-bit Windows 7 where exceptions thrown in the form's Load handler are swallowed and don't crash the application. The upcoming Windows 7 SP1 fixes it, or you can see http://support.microsoft.com/kb/976038/ for a sort of fix.

If you're lazy, here's a quick and dirty way to get some information:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            For Each sp As String In My.Computer.Ports.SerialPortNames
                ListBox1.Items.Add(sp)
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
It won't give you a lot of information, but it might shed some light on what's happening. You can put a breakpoint on the call to MessageBox.Show() and use the debugger to poke at ex and figure out more.

If an exception is not being thrown at all (if the above code doesn't show the message box), I'm sort of stumped.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 07-13-2010, 08:30 PM
axle12693 axle12693 is offline
Freshman
 
Join Date: May 2009
Posts: 26
Default

oh...well, thx... but I guess you're stumped. No exception.
Reply With Quote
  #7  
Old 07-13-2010, 09:33 PM
AtmaWeapon's Avatar
AtmaWeaponPorts... Should be simple? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Interesting; is there *any* other code in the project?

*edit*
Or is it possible that there aren't any serial ports on the computer?
Reply With Quote
  #8  
Old 07-14-2010, 04:56 AM
jo0lsPorts... Should be simple? jo0ls is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2005
Location: London
Posts: 1,050
Default

I get a list of Com ports with the code in the first post.

There's a problem with SerialPortNames that could be responsible. They aren't stored in the correct format in the registry. See:
http://connect.microsoft.com/VisualS...with-bluetooth

You might be getting, say, 1 port with a corrupted name which you don't get to see.

Try dumping the length of the strings found (if any) in the loop:

Code:
For Each sp As String In My.Computer.Ports.SerialPortNames
    MessageBox.Show(sp.Length)
Next
If there is a string with a length then you've discovered the windows bug.

Also check the registry location mentioned, which should list your ports:
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM

If there is a port - what does the binary view of the name show?
Reply With Quote
  #9  
Old 07-14-2010, 07:55 AM
dbasnett's Avatar
dbasnett dbasnett is offline
Junior Contributor
 
Join Date: May 2005
Posts: 299
Default

Replace for next loop with this

ListBox1.Items.AddRange(IO.Ports.SerialPort.GetPortNames)
Reply With Quote
  #10  
Old 07-16-2010, 04:46 PM
gpraceman's Avatar
gpracemanPorts... Should be simple? gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Another way to get serial port names is via WMI. See the last post at
http://vbcity.com/forums/t/154919.aspx
. Check each of the "Name" values to see if it contains the "COM" string and then extract the port number. The COM# will be surrounded by parenthesis. I think this will get around the problem that Bluetooth ports can cause when using SerialPort.GetPortNames(). Plus it can be handy to get the "friendly name" of the port, like you would see in Device Manager, and display that to your users.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.

Last edited by gpraceman; 07-16-2010 at 05:04 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Ports... Should be simple?
Ports... Should be simple?
Ports... Should be simple? Ports... Should be simple?
Ports... Should be simple?
Ports... Should be simple?
Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple? Ports... Should be simple?
Ports... Should be simple?
Ports... Should be simple?
 
Ports... Should be simple?
Ports... Should be simple?
 
-->