CompilerIf Defined(AvailableComs, #PB_List) = 0
Global NewList AvailableComs.s()
CompilerEndIf
Procedure GetAvailableComPorts() ;this procedure scans the registry (on win) or dmesg (on linux) to find the connected com ports
Protected hKey, lpcbName, lpName.s, a$, lType, i, j, Dmesg, Fgrep, Port.s
ClearList(AvailableComs())
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
If RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "HARDWARE\DEVICEMAP\SERIALCOMM", 0, #KEY_QUERY_VALUE, @hKey) = #ERROR_SUCCESS
lpName = Space(1024)
Repeat
lpcbName = 1024
If RegEnumValue_(hKey, i, @lpName, @lpcbName, 0, 0, 0, 0) = #ERROR_SUCCESS
a$ = Left(lpName, lpcbName)
If a$ = ""
Break
EndIf
lpcbName = 1024
lType = 0
If RegQueryValueEx_(hKey, a$, 0, @lType, @lpName, @lpcbName) = #ERROR_SUCCESS
AddElement(AvailableComs())
AvailableComs() = lpName
EndIf
i + 1
Else
Break
EndIf
ForEver
RegCloseKey_(hKey)
EndIf
CompilerCase #PB_OS_Linux
Dmesg = RunProgram("/bin/dmesg", "", "", #PB_Program_Open | #PB_Program_Read)
If Dmesg
Fgrep = RunProgram("/bin/fgrep", "tty", "", #PB_Program_Open | #PB_Program_Connect | #PB_Program_Read, Dmesg)
If Fgrep
While ProgramRunning(Fgrep)
a$ = ReadProgramString(Fgrep)
i = FindString(a$, "ttyS", 1)
If i > 0
j = FindString(a$, " ", i)
Port = "/dev/" + Mid(a$, i, j - i)
hKey = 1
ForEach AvailableComs()
If AvailableComs() = Port
hKey = 0
Break
EndIf
Next
If hKey
AddElement(AvailableComs())
AvailableComs() = Port
EndIf
EndIf
Wend
CloseProgram(Fgrep)
EndIf
CloseProgram(Dmesg)
EndIf
CompilerCase #PB_OS_MacOS
CompilerEndSelect
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
GetAvailableComPorts()
ForEach AvailableComs()
Debug AvailableComs()
Next
CompilerEndIf