Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSerialPort blocks when open() called
BriFuture last edited by BriFuture
If the serial port is plugged after program has launched. Then the first time serial port is selected and the serialport instance would block for 20 seconds most (following log shows 5 seconds blocking before the operation is done) when
After that, Second time I reopen the same serial port, everything seems fine, Is it a bug for
As the following log showed:
below are the codes:
Here is another log and the serialport open operation costs 16s now:
By the way, the
serial portinstance is constructed in another thread instead of UI thread,
but if the background thread blocks and uncorresponding, the program would crash when it tries to close the thread.
How should i do to avoid the problem.
By the way, the first time serial port is opened, no data recved and
serial portalways return 0.
As long as I reopen the port, everything seems OK and the program works well.
which platform are you on and which Qt version are you using?
Windows 10 1909
Qt 5.12.9 MSVC2017 64bit
By the way, the serial port instance is constructed in another thread
Why do you need this thread? QSerialPort is assynchronous.
If you insist in using a thread for the serial port you should communicate via signals/slots with that thread instead of directly accessing the serial port from UI thread.
All my data is processed in the backgrount thread,
the data object will be send by signals/slots mechanism after decoding
After that, Second time I reopen the same serial port, everything seems fine, Is it a bug for QSerialPort?
Most likelly, it is a problem with your device/driver. What is the serial port do you use?
The driver is provided by FTDI , the mcu connects pc by a uart-usb cable.
It was weired that several computer got that case as long as the mcu connected to pc after program launched.
got that case as long as the mcu connected to pc after program launched.
I don't know your program logic (how it opens a serial port and in what of sequence).
But, if you unplug the USB/Serial cable when the serial port is open, then you need to close the opened serial port.
It has not differences the moment when you plug your USB/Serial cable to the PC (before or after the program startup). A main thing is that you need to open the serial port only when it appeares in the system (becomes active in the device manager).
PS: FTDI is a good choose, I never had the problems with this chip.
BriFuture last edited by BriFuture
The error occured if I conducted following steps:
- open the Program Without pluging in the USB port
- plug in the usb port, iterate over QSeiralPortInfo to get the device name (Does the serialport appear in the system If QSerialPortInfo can get the name)
- pass the device name to the method
openthe instance of
QSerialPortas first figure I have showed.
Then the open process costs for approximately 6 to 20 seconds.
I agree with you, FTDI is good and My previous program built with Qt5.7 works fine with it. After I upgrade Qt to 5.12 (first 5.12.5 and now 5.12.9), the serial port seems abnormal.
I meet the problem that serialport of 5.12.5 could not recv/send messages (https://forum.qt.io/topic/107003/qt5-13-1-5-12-5-s-qserialport-trouble/15) and I upgraded Qt to 5.12.9, maybe I have to try another version of Qt.
I follow your advice to check the device manager of Windows. Now the problem has gone.
I don't know which part is the cause of the problem (maybe the error is eliminated because my computer restarts?) Nevertheless, my program runs well.