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
QtWS25 Last Chance

Replace STDIN with a Serial port

Scheduled Pinned Locked Moved Mobile and Embedded
20 Posts 3 Posters 5.7k 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.
  • E Offline
    E Offline
    Epileftric
    wrote on last edited by
    #1

    So I'm working on a project with an "Olinuxino A20":https://www.olimex.com/wiki/A20-OLinuXino-MICRO and its supposed to replace an HMI (human machine interface). We didn't want to go for the touch screen since it will be used pretty roughly and we don't want it to get broken. So instead we went ahead for an old ATM style, with buttons on both sides.

    For the moment I've been programming on a desktop computer for simplicity and emulating the event's with the keyboard with a handler like this one:

    @void PyCGui::keyPressEvent(QKeyEvent* event){
    switch( event->key() ) {
    //~ stuff done with that key
    @

    In the real situation, our hardware keyboard is another microcontroler that sends the key-codes (as chars) over a serial port. I found that Qt4.8 had some classes to replace directly the stdin with any device you like, but currently I can't find how to do it.

    I've even tried the 'rough way'

    @kp_fd = open("/dev/ttyS7", O_RDWR );
    close(STDIN_FILENO);
    dup(kp_fd);
    @

    But it didn't work!

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

      Hi and welcome to devnet,

      Are you thinking of something like "QSerialPort":http://doc.qt.io/qt-5/qserialport.html ?

      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
        #3

        But how do I connect that class to the GUI events?

        I'm currently using 2 serial ports in the program for communication and I write the C code directly where it's needed since I'm more used to that. But if have to use this class to connect it with the GUI I have no problem in changing it!

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

          It will offer you an easier more integrated way to get the data from your serial ports. From there you can create and post key events to the event loop and have them processed like it would come from a standard keyboard

          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
            #5

            From there you can create and post key events to the event loop

            Something like this?

            @
            QSerialPort *keypad= new QSerialPort("/dev/ttyS7", (QObject *)my_gui);
            @

            And that's it? How do I manually connect the signals from the port to my gui?

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

              You should rather write a little class that represent that keypad and have the QSerialPort in there. You'll connect that class to your GUI to keep things cleanly separated.

              On a side note, QWidget is a QObject so there's no need to cast. Furthermore, never use c style cast with QObject derived class, use qobject_cast.

              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
              • mrdebugM Offline
                mrdebugM Offline
                mrdebug
                wrote on 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 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
                  • mrdebugM Offline
                    mrdebugM Offline
                    mrdebug
                    wrote on 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 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
                      • 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