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 open
Qt 6.11 is out! See what's new in the release blog

QSerialPort open

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 4.1k 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.
  • W Offline
    W Offline
    wangshuqi
    wrote on last edited by
    #1

    @
    #include <QtSerialPort/QSerialPort>

    #include <QApplication>
    #include <QtCore/QTime>
    #include <QtCore/QTextStream>

    int main(int argc, char *argv[])
    {
    // QApplication app(argc, argv);

    QTextStream cout(stdout);
    QTextStream cin(stdin);
    
    QSerialPort serial;
    QString portName = "ttyUSB0";
    serial.setPortName(portName);
    
    if (!serial.open(QIODevice::ReadWrite)) {
        cout << QObject::tr("Failed to open port %1, error: %2")
                .arg(portName)
                .arg(serial.errorString()) << endl;
        return 1;
    }
    
    if (!serial.setBaudRate(QSerialPort::Baud115200)) {
        cout << QObject::tr("Can't set baud rate 9600 baud to port %1, error code %2")
                   .arg(portName).arg(serial.error());
        return 1;
    }
    
    if (!serial.setDataBits(QSerialPort::Data8)) {
        cout << QObject::tr("Can't set 8 data bits to port %1, error code %2")
                   .arg(portName).arg(serial.error());
        return 1;
    }
    
    if (!serial.setParity(QSerialPort::NoParity)) {
        cout << QObject::tr("Can't set no parity to port %1, error code %2")
                   .arg(portName).arg(serial.error());
        return 1;
    }
    
    if (!serial.setStopBits(QSerialPort::OneStop)) {
        cout << QObject::tr("Can't set 1 stop bit to port %1, error code %2")
                   .arg(portName).arg(serial.error());
        return 1;
    }
    
    if (!serial.setFlowControl(QSerialPort::NoFlowControl)) {
        cout << QObject::tr("Can't set no flow control to port %1, error code %2")
                   .arg(portName).arg(serial.error());
        return 1;
    }
    
    
    QByteArray requestData("ls");
    requestData.append(QLatin1String("\x0A"));
    
    // write request
    serial.write(requestData);
    if (serial.waitForBytesWritten(5000)) {
        //! [8] //! [10]
        // read response
        if (serial.waitForReadyRead(5000)) {
            QByteArray responseData = serial.readAll();
            while (serial.waitForReadyRead(10))
                responseData += serial.readAll();
    
            QString response(responseData);
            //! [12]
            cout << response << endl;
            //! [10] //! [11] //! [12]
        } else {
            cout << QObject::tr("Wait read response timeout %1")
                         .arg(QTime::currentTime().toString());
        }
        //! [9] //! [11]
    } else {
        cout << QObject::tr("Wait write request timeout %1")
                     .arg(QTime::currentTime().toString());
    }
    
    return 0;
    

    }
    @

    When i run the program, it appears like this:
    QApplication::qAppName: Please instantiate the QApplication object first

    How can i do ?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      first uncomment line 1 in your main() ;)
      on the return do app.exec() instead of returning 0

      Also you need to move the implementation of your serial port to an object which lives in the Qt event loop. If you use a gui simply add it into a widget. If not just use an QObject which does the work instead doing all in the main function.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wangshuqi
        wrote on last edited by
        #3

        i tried, but failed. Could U give a simple example ? Thank you

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wangshuqi
          wrote on last edited by
          #4

          i tried, but failed. Could U give a simple example ? Thank you
          [quote author="raven-worx" date="1386321238"]first uncomment line 1 in your main() ;)
          on the return do app.exec() instead of returning 0

          Also you need to move the implementation of your serial port to an object which lives in the Qt event loop. If you use a gui simply add it into a widget. If not just use an QObject which does the work instead doing all in the main function.[/quote]

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

            Hi,

            What did fail ?

            Since you are not using any GUI element you can use a QCoreApplication

            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
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by
              #6

              BTW: This is not the QSerialPort issue.. :)

              1 Reply Last reply
              0
              • W Offline
                W Offline
                wangshuqi
                wrote on last edited by
                #7

                Does it work without Qt event loop environment?
                [quote author="SGaist" date="1386331898"]Hi,

                What did fail ?

                Since you are not using any GUI element you can use a QCoreApplication[/quote]

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  it works without an event loop when you only use the blocking methods (all waitXXX() methods).
                  Maybe in your case it's enough to just instantiate the Q(Core)Application?

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    wangshuqi
                    wrote on last edited by
                    #9

                    yes, you are right.
                    [quote author="SGaist" date="1386331898"]Hi,

                    What did fail ?

                    Since you are not using any GUI element you can use a QCoreApplication[/quote]

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      wangshuqi
                      wrote on last edited by
                      #10

                      Atfer 2 places modified, it's ok now. But i've a new problem.
                      The following is the response, and the first line "ls" is the sent data.
                      Now i don't want the first line to be displayed, then how?

                      ls
                      bin lib mnt root tmp
                      dev linuxrc opt sbin usr
                      etc lost+found proc sys

                      [quote author="raven-worx" date="1386339040"]it works without an event loop when you only use the blocking methods (all waitXXX() methods).
                      Maybe in your case it's enough to just instantiate the Q(Core)Application?[/quote]

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

                        Isn't that your device that echoes what it has 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
                        • W Offline
                          W Offline
                          wangshuqi
                          wrote on last edited by
                          #12

                          Ah, maybe, i'll check
                          [quote author="SGaist" date="1386578714"]Isn't that your device that echoes what it has received ?[/quote]

                          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