Check for pattern in a non-blocking manner using PyQt6

44 views Asked by At

I have a method command(self, command, data), part of a class CommandProcessor that checks a buffer for a specific pattern and then returns it content:

 command(self, message):
     serial_port.write(bytes(message))
     attempt = 3
     while attempt > 0:
         sleep(1)
         response = self.check_response()
         if type(response) == list or response == 0:
             return response
         else: attempt++

When I click a button in my GUI that calls the command method, I want the buffer contents to be displayed on a QtLabel as soon as the buffer contents match my requirements. The problem I now have, is that i use sleep to repeatedly check the buffer contents. This blocks the GUI. I tried using PyQt6's signals and slots, but they don't allow me to return the buffer contents, only to call (or connect to) a method that can do a certain thing. I want to use returns because this provides an easy way to process received data, for example:

result = command(0x01, "test")
print(result)

Maybe I'm overthinking this and someone knows an alternative solution!

The buffer that I referred to earlier is filled with data from the serial port addressed by QtSerialPort. Maybe I direct connection between the command method and QtSerialPort's readyRead signal is possible?

0

There are 0 answers