Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 for QSerialPort?

    As the following log showed:

    3e88e6ea-e290-443b-bc06-a7fda610e9a9-image.png

    below are the codes:
    be4dcb1c-ca13-4663-b59a-6bfa80d06478-image.png



  • Here is another log and the serialport open operation costs 16s now:

    ef108ae1-0a1c-44d7-aeb4-fbac194c9aaf-image.png

    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() of serial port always return 0.
    As long as I reopen the port, everything seems OK and the program works well.


  • Lifetime Qt Champion

    Hi @BriFuture,

    which platform are you on and which Qt version are you using?

    Regards



  • Windows 10 1909
    Qt 5.12.9 MSVC2017 64bit


  • Lifetime Qt Champion

    @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.



  • @jsulm
    All my data is processed in the backgrount thread,
    the data object will be send by signals/slots mechanism after decoding


  • Qt Champions 2020

    @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?



  • @kuzulis
    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.


  • Qt Champions 2020

    @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:

    1. open the Program Without pluging in the USB port
    2. 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)
    3. pass the device name to the method setPortName and open the instance of QSerialPort 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.



  • @kuzulis
    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.

    Thank you for the help. @aha_1980 @jsulm @kuzulis


Log in to reply