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. How to run async serial in tests?
Forum Updated to NodeBB v4.3 + New Features

How to run async serial in tests?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 461 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
    mmikkone
    wrote on last edited by mmikkone
    #1

    Hello,

    I'm trying to run asynchronous serial ports in Qt Test (on Windows 10). Here's the test (edited by suggestion of SGaist):

    void test_serial::test_serial()
    {
        QSerialPort serialPortWrite;
            serialPortWrite.setPortName("COM11");
            serialPortWrite.setBaudRate(115200);
            serialPortWrite.open(QIODevice::WriteOnly);
            QTEST_ASSERT(serialPortWrite.isOpen());
        
            QSerialPort serialPortRead;
            serialPortRead.setPortName("COM12");
            serialPortRead.setBaudRate(115200);
            serialPortRead.open(QIODevice::ReadOnly);
            QTEST_ASSERT(serialPortRead.isOpen());
        
            SerialPortWriter writer(&serialPortWrite);
            SerialPortReader reader(&serialPortRead);
        
            QSignalSpy spy(&serialPortWrite, &QSerialPort::bytesWritten);
        
            QByteArray arr = {"asdfasdf"};
            writer.write(arr);
            QTest::qWait(1000);
        
            qDebug() << spy.count();
        
            QTEST_ASSERT(arr == reader.m_readData);}
    

    SerialPortWriter and SerialPortReader are asynchronous serial writer and reader classes taken from QT creator's examples ("Command line reader async example" and "Command line writer async example"). I just changed m_readData of SerialPortReader to public. I have created and connected COM11 and COM12 together with com0com (found here: https://sourceforge.net/projects/com0com/) with commands:

    setupc install PortName=COM# PortName=COM#
    setupc change CNCA0 RealPortName=COM11
    setupc change CNCB0 RealPortName=COM12

    Spy.count() prints 0, so the signal never gets emitted. So SerialPortWriter::handleBytesWritten never gets called. This is a handler for bytesWritten signal of QSerialPort.

    When I run the examples as their own, they run fine. I have checked that SerialPortWriter works with a serial port client (ScriptCommunicator in my case).

    How do you run asynchronous serial ports in Qt tests?

    -Marko

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

      What type of test main function are you running ?

      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
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        One thing that is missing from your test is the checks to ensure the ports have been opened successfully.

        You might also need to flush them.

        You should also add some QSignalSpy to ensure the appropriate signals are called.

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

        M 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          One thing that is missing from your test is the checks to ensure the ports have been opened successfully.

          You might also need to flush them.

          You should also add some QSignalSpy to ensure the appropriate signals are called.

          M Offline
          M Offline
          mmikkone
          wrote on last edited by
          #3

          @SGaist Edited the test by your suggestion. Ports are open, but the signal never gets emitted. Weird.

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

            What type of test main function are you running ?

            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
            1
            • M Offline
              M Offline
              mmikkone
              wrote on last edited by
              #5

              Ah, very good. Thank you. It was QTEST_APPLESS_MAIN. I changed it to QTEST_MAIN and the test now passes.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kuzulis
                Qt Champions 2020
                wrote on last edited by
                #6

                Look how does it work, e.g. in the QSP sources, there are present the tests/auto directory with the tests.

                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