Detect Serial Ports

While working on a program that communicates over the serial port, I wanted to give the user an opportunity to see which serial ports are available and to pick one from a list.
The big question was: How do I detect the existing COM-ports?
First I thought, I take Capesofts WinEvent, do a loop and ask for all possible ports with the function NEWPORT().
That was a fabulous idea with just one shortcoming: If a port was already in use, it did not respond, thus it did not appear in the list.

Other programs, like Docklight, a RS232 Terminal and Monitor, were able to do so.
My first guess was, that they look up the existing ports in the Registry. This is how it looks inside there:

Serial Ports in the Windows registry

Clarion 8 has the function GETREGVALUES(root, keyName, valueQueue), which reads all names for a key into a queue.
Once we filled this RegKeysQueue, we can loop throug that, asking for the name of the recent name, using GETREG() for this. In the screenshot above you see \device\Serial0 being COM1.

! GETREGVALUES(root, keyName, valueQueue)

! This is what we look at:

! This is the QUEUE we fill:
! RegKeysQueue

! If there is any COM-Port at the machine, the appropriate key SERIALCOMM has one entry for each port.


LOOP counter = 1  TO RECORDS(RegKeysQueue)
	GET(RegKeysQueue, counter)	


SORT(PortQueue, +PQ:Portname)

?StrgResult{PROP:Text} = 'Number of Serial Ports found: ' & RECORDS(PortQueue)

The source in the embed inthe downloadable APP also has several calls to the program DebugView.
If you don't have DebugView yet, I strongly encourage you to get it!
It monitors the execution of your program and with calls of the function dbgView('Here goes your message') you get instantly informed about every step of your program, with all informatin you want! Without getting interrupted by MESSAGE() or STOP(), and you can still use them too! ;-)
It's free and it is tremendously helpful.
A must in every programmers toolbox!

A screenshot of the actual program:

Thats all! Add salt and pepper as you like....

You can download the entire program and the the source (Clarion 8). This page was made on 09.06.2013.

Here you find some more samples