Python QML: serial port open signals "NoError" error
Unsolved
Qt for Python
-
On first open, and on open after close, serial port signals a "PySide6.QtSerialPort.QSerialPort.SerialPortError.NoError" error. The application can read from and write to the port.
On second and subsequent opens, serial port signals "PySide6.QtSerialPort.QSerialPort.SerialPortError.OpenError" as expected.
On close when open, serial port signals nothing, as expected.
On close when closed, serial port signals "PySide6.QtSerialPort.QSerialPort.SerialPortError.NotOpenError" as expected.
Sample terminal application, which of course is C++ and is not QML, works as expected (full disclosure, trivially modified to use monospaced font).
Any recommendations, other than ignoring the error?
from PySide6.QtCore import Slot, QIODevice from PySide6.QtQml import QmlElement from PySide6.QtSerialPort import QSerialPort QML_IMPORT_NAME = "Serial" QML_IMPORT_MAJOR_VERSION = 1 @QmlElement class Serial(QSerialPort): @Slot() def openSerialPort(self): self.setPortName("/dev/ttyACM0") self.open(QIODevice.ReadWrite) @Slot() def closeSerialPort(self): self.close() @Slot() def serialError(self): print(f"error: {self.error()}")
main.qml
import QtQuick import QtQuick.Controls import QtQuick.Layouts import Serial Window { id: window width: 200 height: 200 visible: true Serial { id: serial onErrorOccurred: { serial.serialError() } } ColumnLayout { Button { text: qsTr("Open Serial Port") onClicked: { console.log("Opening serial port") serial.openSerialPort() } } Button { text: qsTr("Close Serial Port") onClicked: { console.log("Closing serial port") serial.closeSerialPort() } } } }
#!/usr/bin/python3 import sys from pathlib import Path import serial from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine if __name__ == "__main__": app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() qml_file = Path(__file__).resolve().parent / "main.qml" engine.load(qml_file) if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec())