Serial.LastErrorCode = 31

I have an app that is installed on about a dozen machines (all Windows 10 64-bit but the app is 32-bit). The app starts up about a minute after the machine is booted up (they are shutdown nightly) and connects to 3 separate serial devices. On several machines (but not all - arg!) one of the devices serial ports fails to open fairly consistently. When it fails it gives me a LastErrorCode of 31. We attempt to close and reopen the port several times but to no avail. If we restart the app it starts up as it should. It’s almost as if the serial port is in some weird state that Xojo can’t negotiate.

There is nothing special about the serial device. Select the port, baud, stop bit, set RequestToSend = false, and then connect and start polling. The Connect itself seems to be open but it’s the error event that catches it.

Does anyone know what error code 31 is? According to the Xojo Docs most windows errors are in the range of 10004 to 11004 so I’m wondering if this is a Xojo error. Anyone have any ideas?

31 on Windows:

31 (0x1F)
A device attached to the system is not functioning.


Hm…not exactly helpful since all the other serial devices work.

It might just be that I need to wait a bit more for Windows to startup. We’ll see with some testing.

You could ask Xojo engineers what is done internally and ask which functions are called from the Windows SDK to perform the task.

Try a reinstall of the serial drivers if its a USB<>Serial device.
If its a USB<>Serial device, try a different device, it might be on its way out.
Try a call to GetLastError and FormatMessage straight after the Serial.Open (or in the Error event) to see if it returns more information.


Format Message

Use it after calling


to see what information is in the error.

The error codes are listed here:

System Error Codes

With MBS Plugin you could use

GetWindowsErrorMessageMBS(ErrorCode as Integer) as String

To call formatMessage function easily.

Am I using the LastErrorCode from the Serial class or am I calling into the OS? If into the OS, is there an equivalent MBS call?

My assumption was that Xojo simply fails to open connection and puts the OS level error code into LastErrorCode.
Sending the number 31 to GetWindowsErrorMessageMBS, will give you an error message, but that may be just the general failure message quoted above.