USB System.SerialPort connect very slow

Hello

I’m using “System.SerialPort” to connect to an USB device. However, if the device cannot be found / is not connected, this command takes very very long to execute (with the device connected, there is no problem). Is there a way to speed it up in case the device is not found?

Thanx
Lukas

For i=System.SerialPortCount-1 downto 0 Try SerialPort= System.SerialPort(i) // every try takes approx. 10 sec if no device is connected If Open() Then // do some coe else MsgBox("DEBUG: Could not connect => " + str(i)) End If Close() Catch err As OutOfBoundsException End Try Next

A For loop does not check the exit value until after at least one loop, even if the test would fail first.

Apparently it isn’t an error to attempt opening a non-existent device. USB is a hot patch device so the system may be waiting for you to plug one in.

Try another loop construct like:

Dim i As Integer = System.SerialPortCount

While i>0
    Try
        SerialPort= System.SerialPort(i)  
        If Open() Then 
          // do something
        else
          MsgBox("DEBUG: Could not connect => " + str(i))
        End If     
        Close() 
    Catch err As OutOfBoundsException
    End Try
    i = i - 1
Wend

This will avoid trying to open a non-existent device.