pythonqtserialport

PyQT5 QtSerialPort problem reading Hex from port


I have the follow configuration: Raspberry Rpi4B / Python 3.11.2 / PyQt5 5.15.9 Touch panel HDMI IPS 10,1" 1024x600

I have a problem when I use QtSerialPort for communicate with example a computer. Opening communication is no problem, sending Hex data is no problem. Only when receiving Hex data it is wrong end separate. See my my code below. I have tried different ways to read, always the same result.

I think I might not be using the correct command. It must be simple. I check the sending hex data with another computer it is correct.

from PyQt5 import QtCore, QtWidgets, QtSerialPort

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self.send1_btn = QtWidgets.QPushButton(
            text="Send1",
            clicked=self.send1
        )
        self.send2_btn = QtWidgets.QPushButton(
            text="Send2",
            clicked=self.send2
        )
        self.send3_btn = QtWidgets.QPushButton(
            text="Send3",
            clicked=self.send3
        )

        lay = QtWidgets.QVBoxLayout(self)
        hlay = QtWidgets.QHBoxLayout()
        hlay.addWidget(self.send1_btn)
        hlay.addWidget(self.send2_btn)
        hlay.addWidget(self.send3_btn)
        lay.addLayout(hlay)
        
        self.serial = QtSerialPort.QSerialPort('/dev/ttyUSB0',
        baudRate=QtSerialPort.QSerialPort.Baud57600,
        parity=QtSerialPort.QSerialPort.NoParity,
        dataBits=QtSerialPort.QSerialPort.Data8,
        stopBits=QtSerialPort.QSerialPort.OneStop)
        self.serial.open(QtCore.QIODevice.ReadWrite)
        self.serial.setDataTerminalReady(True)
        #self.serial.readyRead.connect(self.readFromSerial) 
        self.serial.readyRead.connect(self.handle_ready_Read)         

    #def readFromSerial(self):
    #self.Esp1=(self.serial.readLine().data())
        
    QtCore.pyqtSlot()
    def handle_ready_Read(self):
        self.Esp1 = self.serial.readAll()
        print (self.Esp1) 
        if (self.Esp1 ==b'xAAxO2x3Cx31x19   '): # Ok = xAAxO2x3Cx31x19        
                print ("Ok")
        else:
                print ("Error")
                                    
    @QtCore.pyqtSlot()
    def send1(self):    
        # Reset,out service 
        self.serial.write(b'\xCC\x02\x3C\x55\x5F')
        # Syncro
        self.serial.write(b'\xCC\x02\x3C\x24\x2E')
        # Setup 
        self.serial.write(b'\xCC\x04\x3C\x23\x00\x10\x3F')
        # Enable
        self.serial.write(b'\xCC\x02\x3C\x12\x1C')
        
    @QtCore.pyqtSlot()
    def send2(self):
        # value  
        self.serial.write(b'\xCC\x04\x3C\x34\x00\x05\x45')
        self.step1=1
        
    @QtCore.pyqtSlot()
    def send3(self):
        # Confirm Ok 
        self.serial.write(b'\xCC\x02\x3C\x22\x2C')    
        
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

Output:

b'\xaa\x02'
Error
b'<1\x19'
Error

Hex data send xAAxO2x3Cx31x19

I tried different ways to read all give the same result

I recieve the follow

b'\xaa\x02'
b'<1\x19'

The correct data is:

b'xAAxO2x3Cx31x19

Solution

  • Your solution is working perfect for me, without wrong messages. Great!

    def handle_ready_Read(self):
        self.Esp1 = self.serial.readAll()
        while self.serial.waitForReadyRead(10):
            self.Esp1 += self.serial.readAll()
        if self.Esp1 ==b'\xaa\x02<1\x19': # Return Ok = xAAxO2x3Cx31x19
             self.Esp2=1
             print (self.Esp1,"Ok")
             self.serial.write(b'\xCC\x02\x3C\x22\x2C')  # Confirm Ok
        else:
             self.Esp2=2
             print (self.Esp1,"Error") # Error