Mimic simple python script with QSerialPort
Solved
General and Desktop
-
Given this simple (and working) python script:
#! /usr/bin/python import sys import serial com = serial.Serial(sys.argv[1], 1200) com.dtr=False com.close()
I tried to write it in Qt 6.2.0 under Ubuntu 21.10:
QSerialPort serial("/dev/ttyACM0"); serial.setBaudRate(QSerialPort::BaudRate::Baud1200); serial.open(QIODevice::ReadWrite); // checked the return value, the port is opened serial.setDataTerminalReady(false); serial.close();
The expected behavior is the reset of the Arduino board connected to
/dev/ttyACM0
.
The Python script achieve the goal, the Qt code does nothing (i.e. does not trigger the reset).What am I missing in the Qt code?
-
Hi,
Did you check the return value of
setDataTerminalReady
? -
@SGaist yes, but the problem is I had to add a delay after the opening of the port before setting the DTR. With this delay (at least 500 ms) the Qt code works like the Python one (without any delay, though).
try:
QSerialPort *serial(new QSerialPort("/dev/ttyACM0")); serial->setBaudRate(QSerialPort::BaudRate::Baud1200); if(serial->open(QIODevice::ReadWrite)) { connect(serial, &QSerialPort::dataTerminalReadyChanged, serial, [=]()->void{ serial->close(); serial->deleteLater(); }, Qt::QueuedConnection); serial->setDataTerminalReady(false); } else { // Could not open port }
-
try:
QSerialPort *serial(new QSerialPort("/dev/ttyACM0")); serial->setBaudRate(QSerialPort::BaudRate::Baud1200); if(serial->open(QIODevice::ReadWrite)) { connect(serial, &QSerialPort::dataTerminalReadyChanged, serial, [=]()->void{ serial->close(); serial->deleteLater(); }, Qt::QueuedConnection); serial->setDataTerminalReady(false); } else { // Could not open port }