Jump to content

How to determine if a serial port is present or not


 Share

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...