Serial OutOfBoundsException on Windows

I am running the Xojo 2018 1.1 on Win 8.1. With no com ports, I plug in an Arduino thus creating a com port. SystemSerialPortCount reports there is one com port. Then I try to access System.SerialPort(0) and get an out-of-bounds exception. Could it be that the code updating SerialPortCount is preceding the code that prepares the serial port object?

I’ve tried adding some exception handling code, but it doesn’t seem to work. I’ve tried Try/Catch, and Exception, but it ends jumping into App.UnhandledException. Code below (with both modes of exception handling). BTW my understanding is a bit vague when it comes to exception handling though it seems simple enough according to the docs. Am I doing something wrong?

Dim sp As SerialPort

#if TargetWindows
If System.SerialPortCount = 1 Then
Try
sp = System.SerialPort(0)
Catch e As OutOfBoundsException
sp = Nil
End Try
If sp <> Nil Then
Return sp.Name
End If
End If
#endif

Return “”

Exception

Return “”

Turn on Break On Exceptions and run your code in the debugger. It should stop at the line that causes the exception. My guess is that it’s actually somewhere else.

Thanks Tim for your reply

It’s stopping on the line

sp = System.SerialPort(0)

I had an idea, to test the Ubound of System.SerialPort but this results in an error:

If System.SerialPort.Ubound = 0 Then

Error: There is more than one item with this name and its not clear to which it refers

I imagine this is because the SerialPort property is both an array and a collection? Same error when trying to access .Count or .Item(0) (as if it was a collection)

It appears that it depends on the driver. That is, with a standard FTDI USB driver, I have not been able to reproduce the problem. With a CH340 USB driver, it hangs every time. I will try updating the driver.

Nevertheless, is there something I am doing wrong with the exception handling? Because I would expect that to work in any case.

As the port scan is running on a thread, I tried sleeping the thread in between the test of SerialPortCount and accessing SerialPort(). The theory is that the driver needs some time to get all its data structures in order. It sort of works, or at least it doesn’t bomb every time now.

Nevertheless the bigger problem of maintaining a list of currently available COM ports might not have a solution, given the tools at hand. Because its a “critical section” problem. There will always be the problem that between testing SerialPortCount and accessing the SerialPort() array/collection, the driver data structures could change. That is, presuming the driver is running on interrupts that are occurring at a level that Xojo cannot control. I assume a Xojo thread CriticalSection is not going to make any difference.

Not sure if it matters, but try changing

Exception

into

Exception e2 as RuntimeException

Didn’t try that yet Thomas as I came up with another idea for maintaining a list of active serial ports. It is based on the premise that providing a collection with an invalid key does not throw an exception, it just returns Nil.

This code seems to work. I am calling this from a timer event, then displaying in a listbox when the port count changes:

Function PortsGet As String()
Dim i As Integer
Dim portName As String
Dim ports(-1) As String

For i = 1 To 20
portName = “COM” + Str(i)
If PortExists(portName) Then
ports.Append portName
End If
Next
Return Ports

End Function

Function PortExists(portName As String) As Boolean
Dim sp As SerialPort
Dim exists As Boolean = False

Try
sp = System.SerialPort(portName)
exists = (sp <> Nil)
Catch
exists = False // never gets here apparently
End Try

Return exists

End Function