Communication between Raspberry Pico and QtSerialPort not working on Windows
-
I have a Raspberry Pico connect via USB to my Windows Computer. On the Pico the following program is running:
int main() { stdio_init_all(); while(true) { printf("HelloWorld"); sleep_ms(1000); } }
I try to read the output via QSerialPort. My Qt program is:
int main(int argc, char *argv[]) { QSerialPort serialPort("COM3"); qDebug() << "Opening"; if (!serialPort.open(QIODevice::ReadWrite)) { qDebug() << "Failed to open " << serialPort.portName(); exit(-1); } qDebug() << "Open!"; while (true) { if (serialPort.waitForReadyRead(2000)) { QByteArray response = serialPort.readAll(); qDebug().noquote() << "R: " << response; } else { qDebug() << "Got Nothing"; } } }
If I plug in the Pico via USB and start the Qt program, the output I get is:
Opening Open! Got Nothing Got Nothing Got Nothing ...
But, If I open the COM port using a different tool, in my case the SerialMonitor from the Arudino IDE, I see
HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld
as excpected. If I start my Qt program after I opened the COM port with the Arudio IDE, I can also see the output:
Opening Open! R: HelloWorld R: HelloWorld R: HelloWorld
The same issue is also reported on Stackoverflow, but using PyQt.
This issue appears only with Windows. On a Raspberry Pi everything works as expected.- Why does QtSerialPort works as expected if I open the Port with a different tool before?
- What does the Arudino IDE do, that QtSerialPort is (maybe) missing?
-
@J-Hilk While searching for proof that values like baudrate are irrelevant for USB-CDC (e. g. here ), I found a thread shows the answer: Windows COM access to PICO via USB trouble.
The Data-Terminal-Ready flag needs to be set to DTR_CONTROL_ENABLE.
This can be done via
QSerialPort::setDataTerminalReady(true)
after opening the Port.