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. How to solve the QSerialPort::ResourceError on Linux platform?
Forum Update on Monday, May 27th 2025

How to solve the QSerialPort::ResourceError on Linux platform?

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportlinuxubuntu 18.04qt 5.13.1qthread
18 Posts 5 Posters 3.9k 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.
  • Y Yash001

    @SGaist
    Yes It is working properly in Mac and Windows with same code.

    One things I came to know if I will remove device from USB port then auto auto availablePorts = QSerialPortInfo::availablePorts(); is recognized device is removed.

    Here the log for last two line:

    10/24/19 1:38 PM | ../../SquidStat/SquidStat/SerialThread.cpp | error on serial port:  QSerialPort::ResourceError
    10/24/19 1:38 PM | ../../SquidStat/SquidStat/InstrumentEnumerator.cpp | Plus1102 is disconnected
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #6
    This post is deleted!
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #7

      Re-reading your issue, I am not sure what you are after. That is a normal behaviour. You remove your device while it's in use. Why would you except your application to stay silent about that ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Y 1 Reply Last reply
      0
      • SGaistS SGaist

        Re-reading your issue, I am not sure what you are after. That is a normal behaviour. You remove your device while it's in use. Why would you except your application to stay silent about that ?

        Y Offline
        Y Offline
        Yash001
        wrote on last edited by Yash001
        #8

        @SGaist said in How to solve the QSerialPort::ResourceError on Linux platform?:

        You remove your device while it's in use.

        SerialCommunicator::SerialCommunicator(const InstrumentInfo &info, QObject *parent) :
        	QObject(parent)
        {
        	_rawData.reserve(1024*1024);
        
        	_serialThread = new SerialThread(info);
        
        	connect(_serialThread, &SerialThread::NewData, this, &SerialCommunicator::DataArrived, Qt::QueuedConnection);
        	connect(this, &SerialCommunicator::SendData, _serialThread, &SerialThread::DataToSend, Qt::QueuedConnection);
        }
        
        
        SerialCommunicator::~SerialCommunicator() {
        	if (_serialThread->isRunning()) {
        		_serialThread->quit();
        		_serialThread->wait();
        	}
        	_serialThread->deleteLater();
        }
        

        I am receiving some initial information from device with help of sub thread. I create objects SerialCommunicator communicator(info) on subthread. and object will delete because of communicator goes out of scope.

        Now, if I will get initial information properly then I am creating object of SerialCommunicator on main thread and object will alive until device is connected.

        I was thinking it resource error due to threading. So that I modified all code, Now I am creating only one object of SerialCommunicator (for single port) through out application. It will all received information.

        But still I am at same page nothing is change. I am getting same error.

        few things I would like to mention. It may help.
        After this error: QSerialPort::ResourceError

        1. _serialPort->isReadable() and _serialPort->isWritable() are return true.
        2. if I am trying write data on USB port then I am successfully send it.
        3. od -xc < /dev/ttyACM0 give bash: /dev/ttyACM0: Device or resource busy

        In addition, If I will put the break point or take some time by writing some dummy statement, then I am able to successful make communication in case 1.

        aha_1980A 1 Reply Last reply
        0
        • Y Yash001

          @SGaist said in How to solve the QSerialPort::ResourceError on Linux platform?:

          You remove your device while it's in use.

          SerialCommunicator::SerialCommunicator(const InstrumentInfo &info, QObject *parent) :
          	QObject(parent)
          {
          	_rawData.reserve(1024*1024);
          
          	_serialThread = new SerialThread(info);
          
          	connect(_serialThread, &SerialThread::NewData, this, &SerialCommunicator::DataArrived, Qt::QueuedConnection);
          	connect(this, &SerialCommunicator::SendData, _serialThread, &SerialThread::DataToSend, Qt::QueuedConnection);
          }
          
          
          SerialCommunicator::~SerialCommunicator() {
          	if (_serialThread->isRunning()) {
          		_serialThread->quit();
          		_serialThread->wait();
          	}
          	_serialThread->deleteLater();
          }
          

          I am receiving some initial information from device with help of sub thread. I create objects SerialCommunicator communicator(info) on subthread. and object will delete because of communicator goes out of scope.

          Now, if I will get initial information properly then I am creating object of SerialCommunicator on main thread and object will alive until device is connected.

          I was thinking it resource error due to threading. So that I modified all code, Now I am creating only one object of SerialCommunicator (for single port) through out application. It will all received information.

          But still I am at same page nothing is change. I am getting same error.

          few things I would like to mention. It may help.
          After this error: QSerialPort::ResourceError

          1. _serialPort->isReadable() and _serialPort->isWritable() are return true.
          2. if I am trying write data on USB port then I am successfully send it.
          3. od -xc < /dev/ttyACM0 give bash: /dev/ttyACM0: Device or resource busy

          In addition, If I will put the break point or take some time by writing some dummy statement, then I am able to successful make communication in case 1.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @Yash001 do you really need to use threads? Inproper synchronisation can lead to unexplainable behavior. QSerialPort buffrs data in background, so there's rarely a need for threads.

          Regards

          Qt has to stay free or it will die.

          Y 1 Reply Last reply
          0
          • aha_1980A aha_1980

            @Yash001 do you really need to use threads? Inproper synchronisation can lead to unexplainable behavior. QSerialPort buffrs data in background, so there's rarely a need for threads.

            Regards

            Y Offline
            Y Offline
            Yash001
            wrote on last edited by aha_1980
            #10

            @aha_1980 said in How to solve the QSerialPort::ResourceError on Linux platform?:

            do you really need to use threads? Inproper synchronisation can lead to unexplainable behavior.

            I was thinking same things. So that, I modify the code, and now I am creating only one object of SerialCommunicator for single port in throughout application, and I am creating object from main thread.

            If you are talking about this thread SerialThread::run() then yes I need this thread because of I am keeping port open until device is connected to USB port. And just want to information I am calling SerialThread::start() function only single time in application.

            jsulmJ 1 Reply Last reply
            0
            • Y Yash001

              @aha_1980 said in How to solve the QSerialPort::ResourceError on Linux platform?:

              do you really need to use threads? Inproper synchronisation can lead to unexplainable behavior.

              I was thinking same things. So that, I modify the code, and now I am creating only one object of SerialCommunicator for single port in throughout application, and I am creating object from main thread.

              If you are talking about this thread SerialThread::run() then yes I need this thread because of I am keeping port open until device is connected to USB port. And just want to information I am calling SerialThread::start() function only single time in application.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @Yash001 said in How to solve the QSerialPort::ResourceError on Linux platform?:

              then yes I need this thread because of I am keeping port open until device is connected to USB port

              And why do you think you need a thread for that?

              Y 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Yash001 said in How to solve the QSerialPort::ResourceError on Linux platform?:

                then yes I need this thread because of I am keeping port open until device is connected to USB port

                And why do you think you need a thread for that?

                Y Offline
                Y Offline
                Yash001
                wrote on last edited by Yash001
                #12

                @jsulm said in How to solve the QSerialPort::ResourceError on Linux platform?:

                why do you think you need a thread for that?

                So that I can collect the data on different thread. and manipulate data on different thread so that I want that functionalities to be very fast.

                In addition,
                Today I modify the SerialThread. and remove thread QThread. Here my new SeraulThread.

                class SerialThread : public QObject {
                	Q_OBJECT
                
                public:
                	SerialThread(const InstrumentInfo&);
                  InstrumentInfo getInstrumentInfo() { return _info; };
                  void waitForBytesWritten() { _serialPort->waitForBytesWritten(1000); };
                
                //protected:
                
                	void run();
                    void quit();
                signals:
                	void NewData(const QByteArray&);
                
                public slots:
                	void DataToSend(const QByteArray&);
                
                private slots:
                	void DataArrived();
                	void ErrorOnSerial(QSerialPort::SerialPortError);
                
                private:
                	QSerialPort *_serialPort;
                	InstrumentInfo _info;
                    
                };
                
                SerialThread::SerialThread(const InstrumentInfo &info) :
                	_info(info)
                {
                    //this->moveToThread(this);
                    LOG() <<"serail thread is created";
                }
                
                // Detect the new instruments and read the data from instruments 
                void SerialThread::run() {
                
                	_serialPort = new QSerialPort(this);
                     _serialPort->setPortName(_info.port.name);
                   _serialPort->setBaudRate(DefaultSerialPortSettings::Baudrate());
                   _serialPort->setDataBits(DefaultSerialPortSettings::DataBits());
                     _serialPort->setFlowControl(DefaultSerialPortSettings::FlowControl());
                    _serialPort->setParity(DefaultSerialPortSettings::Parity());
                    _serialPort->setStopBits(DefaultSerialPortSettings::StopBits());
                
                	connect(_serialPort, &QSerialPort::readyRead,
                		this, &SerialThread::DataArrived, Qt::QueuedConnection);
                
                    connect(_serialPort, &QSerialPort::errorOccurred,[=](QSerialPort::SerialPortError error){
                        LOG() <<"error on serial port" << error;
                    });
                	//connect(_serialPort, &QSerialPort::errorOccurred, this, &SerialThread::ErrorOnSerial);
                
                	_serialPort->open(QIODevice::ReadWrite);
                
                    //exec();
                
                
                
                }
                
                void SerialThread::quit(){
                    _serialPort->close();
                    _serialPort->deleteLater();
                }
                
                // executation is quit if data has error.
                void SerialThread::ErrorOnSerial(QSerialPort::SerialPortError error) {
                	if (QSerialPort::NoError == error) {
                		return;
                	}
                
                    //this->quit();
                }
                // data send to COM port
                void SerialThread::DataToSend(const QByteArray &data) {
                	if (!_serialPort->isWritable()) {
                		return;
                	}
                
                	_serialPort->write(data);
                
                }
                // if data is safly arived then read from port
                void SerialThread::DataArrived() {
                    emit NewData(_serialPort->readAll());
                
                }
                
                

                I am still getting resource error.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  Can you explain the exact logic of your application with regards to selecting the USB port and starting the thread ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Y 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Can you explain the exact logic of your application with regards to selecting the USB port and starting the thread ?

                    Y Offline
                    Y Offline
                    Yash001
                    wrote on last edited by Yash001
                    #14

                    @SGaist

                    I have InstrumentEnumerator class. which is inherits from QThread.
                    InstrumentEnumerator::run() function has while(1) loop. while loop contain QSerialPortInfo::availablePorts();.

                    If new port is found, and if it is our device then addInstrument signal is emit fromInstrumentEnumerator::run(), with argument as portname.

                    SIGNALE InstrumentEnumerator::addInstrument connected with MainWindow::addInstrument solt.

                    MainWindow::addInstrument slot create the object of InstrumentOprator class.

                    object of InstrumentOprator take port name as arguments, create the object of SerialCommunicator.

                    object of SerialCommunicator take port name as arguments, create the object of SerialThread.

                    SerialThread:: run() method is create the object of QSerialPort and keep open port.
                    (just for testing purpose I changed, earlier it was as run on separate thread.)

                    SerialThread::run() method is called from SerialCommunicator::start() method;

                    SerialCommunicator::start() method is called from constructor of InstrumentOprator. So In general, constructor of InstrumentOprator do two things 1) create the object of SerialCommunicator and 2) call SerialCommunicator::start().

                    Few other detail:

                    1. I am calling InstrumentEnumerator::start() from constructor of MainWindow class.
                    2. class Serialthread communicate with only QserialPort.
                    3. class SerialCommunicator make frame and decode frame, send and received data from Serialthread.
                    4. class InstrumentOprator is Communicate with MainWindow and SerialCommunicator.
                    1 Reply Last reply
                    0
                    • artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on last edited by
                      #15

                      Hi,
                      may I suggest two approaches towards narrowing the area of search for a problem (I am sorry if you've been there but I am not able to tell what you already know, just what you've written here):

                      1. are you completely sure that we can eliminate external issue? Lack of privileges for the user that program runs can cause odd things, depending on the distro;
                      2. for the sake of dealing with @SGaist suspicions, are you able to modify the program/create a boileplate code that will do the same operations on the port but without deferring to the threads?

                      Kind Regards,
                      Artur

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      Y 1 Reply Last reply
                      1
                      • artwawA artwaw

                        Hi,
                        may I suggest two approaches towards narrowing the area of search for a problem (I am sorry if you've been there but I am not able to tell what you already know, just what you've written here):

                        1. are you completely sure that we can eliminate external issue? Lack of privileges for the user that program runs can cause odd things, depending on the distro;
                        2. for the sake of dealing with @SGaist suspicions, are you able to modify the program/create a boileplate code that will do the same operations on the port but without deferring to the threads?

                        Kind Regards,
                        Artur

                        Y Offline
                        Y Offline
                        Yash001
                        wrote on last edited by Yash001
                        #16

                        @artwaw said in How to solve the QSerialPort::ResourceError on Linux platform?:

                        are you completely sure that we can eliminate external issue? Lack of privileges for the user that program runs can cause odd things, depending on the distro;

                        I install new Ubuntu 18.04 and Qt 5.13.1. After that I add my self to dialout group sudo usermod -a -G dialout $USER. I hope this is enough for making communication with USB Port. did I missing anything?

                        @artwaw said in How to solve the QSerialPort::ResourceError on Linux platform?:

                        for the sake of dealing with @SGaist suspicions, are you able to modify the program/create a boileplate code that will do the same operations on the port but without deferring to the threads?

                        I remove completely thread and eventloop from serial thread class, but still i am getting that issue.

                        In addition, sometime I am able to generate same error in example/serialport/terminal with baud rate 57600. I am not able to find out certain pattern to regenerate that error.

                        My device is continuously sending data frame as soon as I connect to usb port. Terminal example is printing that data on console.

                        I follow this steps with terminal example:

                        1. open serial port
                        2. remove usb cable from port.
                        3. again connect usb cable to usb port.
                        4. open port (here some data will print and after that sometime I am getting error resource or some time I am not getting.)
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          Are you letting the system some oxygen ?
                          Spamming as fast as possible to find if a new device was added doesn't sound like a good design.

                          I would rather use something like KDE's Solid framework to get a notification when something changed on the USB side.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2
                          • Y Offline
                            Y Offline
                            Yash001
                            wrote on last edited by
                            #18

                            Few place I read, it is generated due to some issue with kernel. So I just update the Ubuntu LTS, and Now it is working with same code.

                            1 Reply Last reply
                            1

                            • Login

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