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
Forum Updated to NodeBB v4.3 + New Features

QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 2.0k Views 2 Watching
  • 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.
  • T Offline
    T Offline
    Tomaz
    wrote on last edited by Tomaz
    #1

    Hi
    I am running qthread. In this thread I run infinitive loop. In loop I am reading USB0tty port. On usb port I have arduino nano with sensors writing values to usb port.
    After work is done in loop I emit signal to another object. If I use clear function with QSerialPort object everything work fine in almost real time but I get error.

    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread.

    If I do not use clear method the error goes away but the message/signal from qthread (signal/slot) comes slow (values do not change) from qtgread object to another object in my qt app.
    I am not expert in USB port anf QSerialPort but I think it has to do with buffer clearance.

    Any Ideas?
    Thomas

    ..h file
    
    #ifndef READTTYUSB0_H
    #define READTTYUSB0_H
    
    #include <QObject>
    #include "QtSerialPort/QSerialPort"
    
    class readttyUSB0 : public QObject
    {
    Q_OBJECT
    
    public:
        readttyUSB0();
        ~readttyUSB0();
    
    public slots:
        void doWork();
    
    signals:
        void usb0Data(QString);
    
    private:
        QSerialPort* sPort = new QSerialPort;
    
        QByteArray data;
        QString str;
    };
    
    #endif // READTTYUSB0_H
    
    .cpp file
    
    #include "readttyusb0.h"
    #include "QThread"
    
    readttyUSB0::readttyUSB0()
    {
        sPort->setPortName("/dev/ttyUSB0");
        sPort->setBaudRate(9600,QSerialPort::AllDirections);
        sPort->setDataBits(QSerialPort::Data8);
        sPort->setFlowControl(QSerialPort::NoFlowControl);
        sPort->setParity(QSerialPort::NoParity);
        sPort->setStopBits(QSerialPort::OneStop);
    
            if(sPort->open(QIODevice::ReadWrite))
            {
            sPort->setDataTerminalReady(QSerialPort::NoSignal);
            }
    }
    
    readttyUSB0::~readttyUSB0()
    {
    delete sPort;
    }
    
    void readttyUSB0::doWork()
    {
        while(1)
        {
            if (QThread::currentThread()->isInterruptionRequested())
            {
            return;
            }
    
            if(sPort->isOpen())
            {
                sPort->waitForReadyRead();
                data=sPort->readLine();
                str=QString::fromStdString(data.toStdString());
    
                emit usb0Data(str);
                sPort->clear();
            }
        }
    }
    Pl45m4P 1 Reply Last reply
    0
    • T Tomaz

      Hi
      I am running qthread. In this thread I run infinitive loop. In loop I am reading USB0tty port. On usb port I have arduino nano with sensors writing values to usb port.
      After work is done in loop I emit signal to another object. If I use clear function with QSerialPort object everything work fine in almost real time but I get error.

      QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread.

      If I do not use clear method the error goes away but the message/signal from qthread (signal/slot) comes slow (values do not change) from qtgread object to another object in my qt app.
      I am not expert in USB port anf QSerialPort but I think it has to do with buffer clearance.

      Any Ideas?
      Thomas

      ..h file
      
      #ifndef READTTYUSB0_H
      #define READTTYUSB0_H
      
      #include <QObject>
      #include "QtSerialPort/QSerialPort"
      
      class readttyUSB0 : public QObject
      {
      Q_OBJECT
      
      public:
          readttyUSB0();
          ~readttyUSB0();
      
      public slots:
          void doWork();
      
      signals:
          void usb0Data(QString);
      
      private:
          QSerialPort* sPort = new QSerialPort;
      
          QByteArray data;
          QString str;
      };
      
      #endif // READTTYUSB0_H
      
      .cpp file
      
      #include "readttyusb0.h"
      #include "QThread"
      
      readttyUSB0::readttyUSB0()
      {
          sPort->setPortName("/dev/ttyUSB0");
          sPort->setBaudRate(9600,QSerialPort::AllDirections);
          sPort->setDataBits(QSerialPort::Data8);
          sPort->setFlowControl(QSerialPort::NoFlowControl);
          sPort->setParity(QSerialPort::NoParity);
          sPort->setStopBits(QSerialPort::OneStop);
      
              if(sPort->open(QIODevice::ReadWrite))
              {
              sPort->setDataTerminalReady(QSerialPort::NoSignal);
              }
      }
      
      readttyUSB0::~readttyUSB0()
      {
      delete sPort;
      }
      
      void readttyUSB0::doWork()
      {
          while(1)
          {
              if (QThread::currentThread()->isInterruptionRequested())
              {
              return;
              }
      
              if(sPort->isOpen())
              {
                  sPort->waitForReadyRead();
                  data=sPort->readLine();
                  str=QString::fromStdString(data.toStdString());
      
                  emit usb0Data(str);
                  sPort->clear();
              }
          }
      }
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Tomaz

      What do you expect from emitting a signal in a while(1) loop?!
      It will never get processed

      Edit:
      Ah havent seen your comment. You only emit once when the loop has ended.

      Have a look at the synchronous and asynchronous QSerialPort examples here:

      • https://doc.qt.io/qt-6/qtserialport-blockingreceiver-example.html
      • https://doc.qt.io/qt-6/qtserialport-terminal-example.html

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

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

        Hi,

        Is it expected that you read only one line ?
        You should rather get everything from the serial port, accumulate in a buffer and then process the data once you have a full frame (or more).

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

        T 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Is it expected that you read only one line ?
          You should rather get everything from the serial port, accumulate in a buffer and then process the data once you have a full frame (or more).

          T Offline
          T Offline
          Tomaz
          wrote on last edited by
          #4

          @SGaist Hi SGaist. Everytime is good to read your replies. Yes. I control what is being red and it is one line from arduino loop (simple sensor data). Thomas

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

            Can you share also the thread related code ?

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

            T 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @Tomaz

              What do you expect from emitting a signal in a while(1) loop?!
              It will never get processed

              Edit:
              Ah havent seen your comment. You only emit once when the loop has ended.

              Have a look at the synchronous and asynchronous QSerialPort examples here:

              • https://doc.qt.io/qt-6/qtserialport-blockingreceiver-example.html
              • https://doc.qt.io/qt-6/qtserialport-terminal-example.html
              T Offline
              T Offline
              Tomaz
              wrote on last edited by Tomaz
              #6

              @Pl45m4 Hi.
              I am sorry but the code is correct on syntax, semantic and semiotic/pragmatic level. The creation of and object has to happen inside the working loop I concluded; (in run loop). I think it has to do with usb port access on low level (QSerialPort c/c++ Qt code). I think. The error is gone now.

              I got it from one of SGaist comments/replies.

              Perhaps SGaist could give us more enlightenment why it has to be inside run loop (or custom loop like I have)? Perhaps because it is made on heap?

              regards, Thomas

              T 1 Reply Last reply
              0
              • T Tomaz

                @Pl45m4 Hi.
                I am sorry but the code is correct on syntax, semantic and semiotic/pragmatic level. The creation of and object has to happen inside the working loop I concluded; (in run loop). I think it has to do with usb port access on low level (QSerialPort c/c++ Qt code). I think. The error is gone now.

                I got it from one of SGaist comments/replies.

                Perhaps SGaist could give us more enlightenment why it has to be inside run loop (or custom loop like I have)? Perhaps because it is made on heap?

                regards, Thomas

                T Offline
                T Offline
                Tomaz
                wrote on last edited by
                #7

                @Tomaz No it is not the heap problem. Perhaps of the scope object is living in?

                K 1 Reply Last reply
                0
                • T Tomaz

                  @Tomaz No it is not the heap problem. Perhaps of the scope object is living in?

                  K Offline
                  K Offline
                  kuzulis
                  Qt Champions 2020
                  wrote on last edited by
                  #8

                  @Tomaz

                  Right now everything is wrong in your code.

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • K kuzulis

                    @Tomaz

                    Right now everything is wrong in your code.

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Where is readttyUSB0 instantiated / is it moved to a thread somewhere? If yes then you forgot to move sPort (btw: very strange name for a member var) as it has no parent.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    T 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      Where is readttyUSB0 instantiated / is it moved to a thread somewhere? If yes then you forgot to move sPort (btw: very strange name for a member var) as it has no parent.

                      T Offline
                      T Offline
                      Tomaz
                      wrote on last edited by Tomaz
                      #10

                      @Christian-Ehrlicher Hi. Sorry for my late reply. Well it is in mainwindow constructor. This is in constructor.

                      rttyUSB0->moveToThread(threadttyUSB0);
                      connect(threadttyUSB0,SIGNAL(started()),rttyUSB0,SLOT(doWork()));
                      connect(rttyUSB0,SIGNAL(usb0Data(QString)),this,SLOT(readSerialCom(QString)));
                      threadttyUSB0->start();```
                      

                      Declared in .h file in private part.

                      What I do in readSerialCom(QString) is not important.

                      I let this loop to run for a week with no problem. No compile error. Should be ok.

                      Thomas

                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Can you share also the thread related code ?

                        T Offline
                        T Offline
                        Tomaz
                        wrote on last edited by
                        #11

                        @SGaist Hi. SGaist. One question. What is the best way (board?) to boot Linux into Qt graphic App (I need access to /dev- USB) in max 2 to 3 seconds? But to be on open source non commercial side. I tried raspberry Pi and eglfs but it needs 10 seconds. regards Thomas

                        Ronel_qtmasterR SGaistS 2 Replies Last reply
                        0
                        • T Tomaz

                          @SGaist Hi. SGaist. One question. What is the best way (board?) to boot Linux into Qt graphic App (I need access to /dev- USB) in max 2 to 3 seconds? But to be on open source non commercial side. I tried raspberry Pi and eglfs but it needs 10 seconds. regards Thomas

                          Ronel_qtmasterR Offline
                          Ronel_qtmasterR Offline
                          Ronel_qtmaster
                          wrote on last edited by
                          #12

                          @Tomaz if you are using yocto, you can disable certain services at start or change the boot order by prioritising your app

                          1 Reply Last reply
                          0
                          • T Tomaz

                            @SGaist Hi. SGaist. One question. What is the best way (board?) to boot Linux into Qt graphic App (I need access to /dev- USB) in max 2 to 3 seconds? But to be on open source non commercial side. I tried raspberry Pi and eglfs but it needs 10 seconds. regards Thomas

                            SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Tomaz hi,

                            It depends on what your device has to do, which services are required, etc.

                            Based on that you can optimize some things to reduce boot time.

                            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
                            0

                            • Login

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