Solved

How many serial ports are there?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 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

19 Experts available now in Live!

Get 1:1 Help Now