I wrote a C++ method to find all serial ports, open, write and close and use to Q_INVOKABLE to call this method from a QML. At QML, first I push a LoadingPage.qml to StackView and then I call the find() Serial Ports, inside the onClicked: Button slot.
The problem: It is a freezing on push a LoadingPage.qml to StackView if there are many serial ports connected, the animation start and then immediately freezes, when the function find finish the animation start again. [SerialPort.qml] How is it the better way to solve that?
//SerialPort.qml
Button {
text: qsTr("start")
onClicked: {
stackView.push(Qt.resolvedUrl("LoadingPage.qml"))
module.find()
}
}
QVector<QString> Physical::find()
{
m_ports.clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
bool hasError = false;
QSerialPort port;
port.setPort(info);
if (port.open(QIODevice::ReadWrite)) {
if (!hasError && !port.setBaudRate(serial::baudRate)) {
emit error(tr("Can't set baud to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError && !port.setDataBits(serial::dataBits)) {
emit error(tr("Can't set data bits to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError && !port.setParity(serial::parity)) {
emit error(tr("Can't set parity to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError && !port.setStopBits(serial::stopBits)) {
emit error(tr("Can't set stop bits to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError && !port.setFlowControl(serial::flowCtrl)) {
emit error(tr("Can't set flow control to %1, error %2")
.arg(port.portName())
.arg(port.error()));
hasError |= true;
}
if (!hasError) {
m_ports.append(port.portName());
}
QByteArray data;
data.resize(1);
data[0] = ID_READ;
port.write(data);
port.close();
}
}
return m_ports;
}