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

QSerialPort::open occassionally hangs before success



  • Hello there,

    We've been using qt for some time now and are very happy. However, we are having one problem I can't find any information in the documentation of forums on.

    Background:
    Our application polls serial ports and when it finds a device of correct vendor ID, it opens the ports as read/write and begins a handshake. In general this works very quickly and without error.

    Issue:
    Sometimes (~10% of the time), after starting the application, the thread hangs at Port->open(QIODevice::ReadWrite); for between 10-20 seconds before returning. Following the hang, the device has connected as it does when it does not hang.

    Questions:
    Is there anything that can be done to timeout the attempt to open()?
    Is there information on what is causing this short hang, and how to resolve it?

    Thanks kindly for your help
    Kyle

    
    class USB : public QObject
    {
    	Q_OBJECT
    public:
    ...
    	static QSerialPort * Port;
    ...
    private slots:
    ...
    	void start() {
    		setupPort(); // hangs here 
    		if (is_setup) {
    			setupTimeout();
    			emit connected(1);
    		}
    		else {
    			QTimer::singleShot(RECONNECT_POLL, this, SLOT(start()));
    		}
    	}
    
    protected: 
    
        int setupPort() {
    		if (Port && Port->isOpen()) {
    			qDebug() << "USB: Port was open; closing previous port";
    			Port->close(); 
    			return 0;
    		}
    		const auto infos = QSerialPortInfo::availablePorts();
    		int i = 0;
    		for (const QSerialPortInfo &info : infos) {
    			if (info.vendorIdentifier() == (quint16)VENDOR_ID_TEENSY || 
                                info.vendorIdentifier() == (quint16)VENDOR_ID_FTDI ) {
    				Port = new QSerialPort(info);
    				if (Port->isOpen()) {
    					qDebug() << "USB: Port is already open";
    					Port->close();
    					return 0;
    				}
    				if (info.isBusy()) {
    					qDebug() << "USB: Port is busy";
    					Port->close();
    					return 0;
    				}
    				qDebug() << "USB: opening port";
    				Port->open(QIODevice::ReadWrite);  // <<<<<<<<<<<<< hangs here 10% of the time 
    				if (!Port->isOpen()) {
    					stateString = "Failed to open port";
    					qDebug() << "Failed to open port";
    					return 0;
    				}
    				else {
    					qDebug() << "USB: port opened"; // makes it to here after hang  everytime
    				}
                       ...
    	}
    
    

  • Lifetime Qt Champion

    Hi @KyleHagen

    • which OS is that?
    • which serial port (I guess a USB adapter, but which chipset?)
    • and which Qt version?

    Does the problem also occur in one of Qt's setial port examples?

    Regards



  • @aha_1980 said in QSerialPort::open occassionally hangs before success:

    Hi @KyleHagen

    • which OS is that?
    • which serial port (I guess a USB adapter, but which chipset?)
    • and which Qt version?

    Does the problem also occur in one of Qt's setial port examples?

    Regards

    Hi there, thanks for your response.

    This is Windows 10 x64

    The USB device is a MK20DX256 microcontroller.
    Another device we use it the Zynq-based Zybo board which uses an FTDI USB-UART converter. I can't seem to reproduce the issue on this device.

    This is using qt 5.12.3

    I will see if I can reproduce the issue on a serial port example



  • @KyleHagen,

    1. Don't use QSPI::isBusy(), as it tries to open/close the serial port.
    2. Most likelly the problem is in FW of your MCU, that 'hangs' the device with multiple open/close sequences.

  • Lifetime Qt Champion

    @kuzulis said in QSerialPort::open occassionally hangs before success:

    Most likelly the problem is in FW of your MCU, that 'hangs' the device with multiple open/close sequences.

    Resp. the corresponding Windows driver. I'd search the vendors web page for updated drivers first.



  • @aha_1980 said in QSerialPort::open occassionally hangs before success:

    Resp. the corresponding Windows driver. I'd search the vendors web page for updated drivers first.

    Usually, the MCU's FW provides the USB CDC stack. So, for this, the Windows (as and Linux) contains the standard drivers. IMHO, need to check the FW sources which related to handling of CDC-class callbacks and etc (maybe there are present some 'get stucks', like the sleep() calls or something else).


Log in to reply