Solved

How many serial ports are there?

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 55
Sorting multiple rows and columns, and count duplicates in Excel 2013 4 79
Hide vba in gp 7 95
Automatic Email Reminder 4 38
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

777 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