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 5.7k 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.
  • M Offline
    M Offline
    mrdebug
    wrote on 13 Dec 2014, 12:03 last edited by
    #7

    It' very easy to do. I know and use Olimex A20 with serial devices and graphic interaction.
    I suggest you to write a class based on QSerialPort and implement events on special data.
    If you want I can to it for you.

    Which kind of hardware do you use?

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Epileftric
      wrote on 14 Dec 2014, 17:26 last edited by
      #8

      What i'm missing is: should I create a new thread for it to monitor the serial port and create the events? or not?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrdebug
        wrote on 14 Dec 2014, 17:35 last edited by
        #9

        In this case I normally use an Object based on QThread to manage the serial device. I normally add to the object the Tcp features, because sometimes the serial device can be attached with a rs232 - tct converter.
        The object has to manage the device and can emit an event at particular situations.
        For example, if you have a usb - rfid reader, when a tag will read, the object emit an event with the read tag code.

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Epileftric
          wrote on 14 Dec 2014, 20:04 last edited by
          #10

          So it has to be something like this:

          @
          class Keypad : public Qthread {

          public: 
          Keypad(){
           initialize_serial_port();
           this->start();
          

          }

          private:
          QSerialPort serial;
          void run() {
             while(1) {
                 serial.get_stff();
                generate_event_for_gui();
           }
          

          }
          };@

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 14 Dec 2014, 20:44 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 14 Dec 2014, 20:49 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 14 Dec 2014, 20:54 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 14 Dec 2014, 21:18 last edited by
                  #14

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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 14 Dec 2014, 22:50 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 14 Dec 2014, 22:58 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
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 14 Dec 2014, 23:12 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 14 Dec 2014, 23:19 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 15 Dec 2014, 21:53 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
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 15 Dec 2014, 22:25 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

                              16/20

                              14 Dec 2014, 22:58

                              • Login

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