How to run async serial in tests?
-
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=COM12Spy.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
-
What type of test main function are you running ?
-
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.
-
What type of test main function are you running ?
-
Look how does it work, e.g. in the QSP sources, there are present the tests/auto directory with the tests.