Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Replace STDIN with a Serial port
Forum Updated to NodeBB v4.3 + New Features

Replace STDIN with a Serial port

Scheduled Pinned Locked Moved Mobile and Embedded
20 Posts 3 Posters 6.0k Views 1 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #11

    Technically, threads are not mandatory at all in this case unless you start heavy processing on the data you received.

    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
    • E Offline
      E Offline
      Epileftric
      wrote on last edited by
      #12

      But I don't see where and how to create a loop for the events in the GUI.

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

        You already have an event loop after you call app.exec(). Use QSerialPort in asynchronous mode

        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
        • E Offline
          E Offline
          Epileftric
          wrote on last edited by
          #14

          but how do I get in that loop!?! That's what I don't get

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

            You don't need to get in that loop, once app.exec is called, the loop runs. Events are sent, signals and slots activates when needed etc.

            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
            • E Offline
              E Offline
              Epileftric
              wrote on last edited by
              #16

              Yeah.. that's what I'm trying to do now. Since I can't use the serial port in a separated thread.

              So what now? Do I have to create a signal handler for the serial?
              I'm quite new to GUIs writing, I'm mainly a microcontroller programmer. Sorry if this are really n00b questions.

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

                I never said it won't work in a separated thread, just that it's really not needed.

                Something like this:

                @
                class KeyPad :: public QObject {
                Q_OBJECT
                public:
                KeyPad(QObject *parent = 0 ) :
                QObject(parent)
                {
                connect(&serialPort, SIGNAL(readyRead()), SLOT(processData()));
                }

                bool initialize(const QString &portName)
                {
                serialPort.setPortName(portName);
                // setup port

                return serialPort.open(QIODevice::ReadWrite); // or ReadOnly if you won't write anything in it
                

                }

                signals:
                valueChanged(int newValue);

                private slots:
                void processData() {
                data.append(serialPort.readAll());
                // check if enough data is there
                // parse data
                // removed processed data from buffer
                // emit signal(s)
                }

                private:
                QByteArray data;
                QSerialPort serialPort;
                };
                @

                I've wrote everything in the "header" for brevity only

                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
                • E Offline
                  E Offline
                  Epileftric
                  wrote on last edited by
                  #18

                  @ connect(&serialPort, SIGNAL(readyRead()), SLOT(processData()));@

                  I get it now, I was doing right that as you write! SO that's what gets it in the "main event loop" right?

                  Of course you didn't said it won't work in a separated thread, but I tried just that and it didn't work because of some "can't create new thread issue"

                  THANKS A LOT!!

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    Epileftric
                    wrote on last edited by
                    #19

                    So I got it, now I can navigate the menues with the serial port as I did with the keyboard. But the only thing that's not working are text entries. Whenever I need to input some text in a QLineEdit widget it doesn't recognizes the input as a keystroke!

                    @
                    void Keypad::SerialPortHandler (void){
                    char buff;

                    serial->read(&buff,1);

                    QKeyEvent *last_key = new QKeyEvent( QEvent::KeyPress, buff , Qt::GroupSwitchModifier, QString(buff) );

                    QCoreApplication::sendEvent(pycgui,last_key);
                    }
                    @

                    I use Qt::GroupSwitchModifier only to differentiate the events I create from the real one so I can manually delete them. But it's not the reason the input is not working!

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

                      AFAIK, text input requires a press and a release event

                      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