Jump to content
Sign in to follow this  
CountyIT

How to determine if a serial port is present or not

Recommended Posts

CountyIT

I have a function to determine if a serial port exists on a computer or not. I am working with virtual serial ports and as a result it could actually disappear (the user pulls the USB cable) and re-appear quite often. As a result I need to continue to monitor for the presece (or absense) of this serial port. The one I have (below) loops through the serial ports registry. I need to keep the code as tight and small as possible. WOuld there be a better way to do it then the code below?

Func IsPortPresent($PortNum)

Local $Instance = 1, $Key, $Found = False, $Port

While 1

$Key = RegEnumVal("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", $Instance)

If @error = -1 Then

ExitLoop

EndIf

if $Key = "" and @error > 0 Then

WriteLog("Error " & @error & " preforming RegEnumVal")

ExitLoop

EndIf

$Port = RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM",$Key )

If $Port = "" And @error <> 0 Then

WriteLog("Error " & @error & " preforming RegRead")

ExitLoop

EndIf

If $Port = ("COM" & $PortNum) Then

$Found = True

ExitLoop

EndIf

$Instance += 1

WEnd

if $Found = False Then

WriteLog("COM" & $PortNum & " is not present")

EndIf

Return $Found

EndFunc

Share this post


Link to post
Share on other sites
Inververs

#include 'DeviceApi.au3'
Local $friendly_name, $aCom,$Count = 0
_DeviceAPI_GetClassDevices("{4D36E978-E325-11CE-BFC1-08002BE10318}")
While _DeviceAPI_EnumDeviceInfo($Count)
$friendly_name = _DeviceAPI_GetDeviceRegistryProperty($SPDRP_FRIENDLYNAME)
$Count += 1
If $friendly_name Then ConsoleWrite($friendly_name & @LF)
WEnd

Share this post


Link to post
Share on other sites
CountyIT

Thanks Seeker. It kind of looks like they both search the registry in a loop. You think the deviceapi is going to be quicker and faster?

Dang. No luck. The deviceapi.au3 wasn't readily available. Someone posted a link toward the end of the thread but what I tried to run the above script it couldn't find any of the functions...

Edited by CountyIT

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×