QSerialPort blocks when open() called
-
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
open(QSerialPort::ReadWrite)
is called.
After that, Second time I reopen the same serial port, everything seems fine, Is it a bug forQSerialPort
?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 port
instance 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
bytesAvailable()
ofserial port
always return 0.
As long as I reopen the port, everything seems OK and the program works well. -
-
@BriFuture said in QSerialPort blocks when open() called:
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. -
@BriFuture said in QSerialPort blocks when open() called:
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?
-
@BriFuture said in QSerialPort blocks when open() called:
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.
-
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
setPortName
andopen
the instance ofQSerialPort
as 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.
-