Solved

How many serial ports are there?

Posted on 2001-06-18
3
202 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
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

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

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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

12 Experts available now in Live!

Get 1:1 Help Now