Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort::open occassionally hangs before success
Qt 6.11 is out! See what's new in the release blog

QSerialPort::open occassionally hangs before success

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KyleHagen
    wrote on last edited by
    #1

    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
    				}
                       ...
    	}
    
    
    aha_1980A 1 Reply Last reply
    0
    • K KyleHagen

      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
      				}
                         ...
      	}
      
      
      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Qt has to stay free or it will die.

      K 1 Reply Last reply
      1
      • aha_1980A aha_1980

        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

        K Offline
        K Offline
        KyleHagen
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on last edited by
          #4

          @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.
          aha_1980A 1 Reply Last reply
          2
          • K kuzulis

            @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.
            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Qt has to stay free or it will die.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by kuzulis
              #6

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

              1 Reply Last reply
              3

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved