Solved

How many serial ports are there?

Posted on 2001-06-18
3
208 Views
Last Modified: 2007-12-19
Hi
1.Does someone have a function()/api that returns the number of serial ports installed on a system.

2. Is there a better way to test if a serial port is free other than trying to open the port and checking for errors.

Thanks
/GavinS
0
Comment
Question by:GavinS
[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 Comments
 
LVL 5

Expert Comment

by:gbaren
ID: 6204732
Gavin,

I found looping through the ports to be the easiest. There are, other methods.

1. Registry Method 1
Enumerate all values in key HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM. Key names will be \Device\Serial0, \Device\Serial1, etc. The value is that port's name: COM1, COM2, etc.

Don't make the common mistake: SERIALCOMM is the key. \Device\Serial0 is the value name, slashes and all.

Drawback: You can not use scrrun.dll's Registry functions because you can not enumerate. Must use RegEnumKey.


2. Registry Method 2
Get the value from key HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\COM Name Arbiter\. Value name COMDB. The first byte should tell you the next x for COMx to be assigned. I have not been able to verify this conclusively.

Drawback: You can have COM1, COM2 and COM8. Next is COM9 but that does not mean you have 8 ports.







0
 

Accepted Solution

by:
sachin_nigam earned 100 total points
ID: 6205183
hi
One thing that you can do is try opening ports from number 1 to 16 in binary mode...if a port can be opened that means that port number is available..if cannot that means that port number isn't..here is the code snippet:-

..
Dim ffree as integer,i as integer,intPort as integer

for i = 1 to 16
On Error Resume Next
ffree = freefile
Open "COM" & cstr(i) For Binary Shared as #ffree
if Err.Number = 0 then ''means Port isd available
intPort = intPort + 1
Close #ffree
end if
Next
Msgbox "Number of Ports:-" & intPort

..
Hope this helps
Cheers
Sachin
0
 

Author Comment

by:GavinS
ID: 6208840
Hhmmm, Wasn't quite what I was after, but that will do for now. I've seen api functions posted on various vb web sites in the past that return the number of com port installed, but do you think I could find the damn things when I really wanted it. Not on your nelly!    -Thanks

Regards
/GavinS

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

751 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