Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

How many serial ports are there?

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
GavinS
Asked:
GavinS
1 Solution
 
gbarenCommented:
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
 
sachin_nigamCommented:
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
 
GavinSAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now