no connection to the serial port with windows

Hello,
I am new to Xojo and I wrote a small app that sends and receives data to/from an Arduino uno. I use the Portlistupdater as in the tutorial for the Barcodereader (code below).
The app works fine on my Mac in the debugger. Now I have tried the program on a Windows10 laptop in the Xojo debugger. According to system control, the Arduino is at COM port 3. But my SerialPortsPopupMenu shows COM4. Of course, a connection is impossible.
When I unplug the USB cable, it changes to COM5.
With the serial monitor of the Arduino-IDE I can easily send and receive data via COM3.
Does anyone know what’s wrong? I get more and more desperate if I can’t figure it out.

Code (PortListUpdater -> Run):

Dim count As Integer = SerialPortsPopupMenu.ListCount

If System.SerialPortCount <> count Then
// The number of serial devices has changed so update the menu
SerialPortsPopupMenu.DeleteAllRows
For i As Integer = 0 To System.SerialPortCount - 1
SerialPortsPopupMenu.AddRow(System.SerialPort(i).Name)
Next
If System.SerialPortCount < count Then // a device has been removed
SerialPortsPopupMenu.ListIndex = 0
Else // one has been added so select the new device
SerialPortsPopupMenu.ListIndex = System.SerialPortCount - 1
End If
End If

One recommendation it to make certain you .Close() the open serial port after you finish using it.

This is the code for the open/closed button event (also like in the tutorial):
If Me.Caption = “Disconnect” Then // Disconnect from the serial port
Serial1.Close
Me.Caption = “Connect”
SerialPortsPopupMenu.Enabled = True
PortListUpdater.Mode = Timer.ModeMultiple ’ turn it on
Else // Connect to the serial port
// Set the serial port to the index of the one chosen in the popup menu
Serial1.SerialPort = System.SerialPort(SerialPortsPopupMenu.ListIndex)
If Serial1.Open Then
Me.Caption = “Disconnect”
SerialPortsPopupMenu.Enabled = False
PortListUpdater.Mode = Timer.ModeOff
Else
Beep
MessageBox(“The selected serial port could not be opened.”)
End If
End If

But I can not connect to the port at all. It is not found if the app is startet with windows.

Windows, Linux, Mac all have their subtle differences in how their serial ports behave - but don’t worry, there are solutions.

You may want to code your serial interface to more dynamic, and less “hard coded”. Don’t rely on Windows to ever hand you a proper port# consistently.

Here’s what I do…

1.- Monitor the serial ports constantly and look for changes in the # of ports (just like the example code demonstrated).
2.- Create a list of objects that hold all the serial pointers.
3.- Iterate thru each of them, looking for ports that will open (not in use).
4.- Open each idle port, using the serial config appropriate for your Arduino (correct Baud rate, etc.).
5.- Interrogate the port by using a serial command that the Arduino will respond to, assuring you’ve landed on the right port.
6.- Use the port you allocated in step #5.

This method is a bit exotic, but it works across most every platform, and allows for some elegant “auto connect” plug-and-play behavior.

Thank you very much for your help. I think you gave me an idea.

When I do the following:
For i As Integer = 0 To System.SerialPortCount - 1
MsgBox(System.SerialPort(i).Name)
Next

… comes COM5 first, then COM3 and then after a break COM4. Com4 is always listed last.

But when one device is added (in my case COM3), the code from the tutorial sets:
SerialPortsPopupMenu.ListIndex = System.SerialPortCount - 1

and so I always try to connect to Port COM4.

Tomorrow I’ll report whether I succeed to solve the problem.