Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Python QML: serial port open signals "NoError" error
Forum Update on Monday, May 27th 2025

Python QML: serial port open signals "NoError" error

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 220 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lcongdon
    wrote on 29 Aug 2022, 21:21 last edited by
    #1

    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?

    serial.py:

    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()
                }
            }
        }
    }
    

    main.py

    #!/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())
    
    1 Reply Last reply
    0

    1/1

    29 Aug 2022, 21:21

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved