'PyQT5 problem with reading from port with QtSerialPort

I am currently trying to communicate with a device which sends a data stream by COM. I am having a weird problem with QtSerialPort which I currently don't know how to solve. The description of the problem is: When I restart the device and restart my application, in the application I can open the corresponding port without any error. However, I do not receive any data from the device. I am trying to read data from the COM-Port with the signal readyRead. Now, when I open the port with another program B which uses python's serial, I can successfully read from the device after a restart. Now, after reading successfully from the device with program B, I can also successfully read from the device using the QT-Program A, but only if I do not restart the device. The following code contains the isolated port with QtSerial which reproduces the above mentioned problem.

import sys
from PyQt5 import QtSerialPort
from PyQt5.QtCore import *
from PyQt5.QtSerialPort import *
from PyQt5.QtWidgets import QApplication, QMainWindow

class QtSerialTest(QMainWindow):
    def __init__(self, parent=None) -> None:
        super().__init__()

        self.port_com_port = QtSerialPort.QSerialPort()
        self.port_com_port.setPortName("COM4")
        self.port_com_port.setBaudRate(QSerialPort.BaudRate.Baud115200)
        self.port_com_port.setParity(QSerialPort.Parity.NoParity)
        self.port_com_port.setDataBits(QSerialPort.DataBits.Data8)
        self.port_com_port.setStopBits(QSerialPort.StopBits.OneStop)

        self.port_com_port.open(QIODevice.ReadWrite)
        self.port_com_port.readyRead.connect(self.readFromSerial)

    def readFromSerial(self):
        print(self.port_com_port.readAll())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    test = QtSerialTest()
    app.exec()

I can confirm that after a restart, the function "readFromSerial" is never called, although the device sends.

EDIT: I forgot to mention: I compared the port-settings from program A and B, they are equal



Solution 1:[1]

After some search, I could remove the workaround and it works now. I missed an important configuration-parameter. After opening the QtSerialPort, self.port_com_port.setDataTerminalReady(True) needs to be called.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user44791