简体   繁体   中英

PyQT5 - How to display in QLCD widget part of emited signal from serial?

I have connected Arduino to RPi, where Arduino send constantly one by one line information about actual temperature (at this moment, only from one sensor, later I'll add more sensors). Now it's no problem to receive only one information, but later I'll send more data from Arduino like on example below:

1213141516

Where: 12 it's temperature of 1st sensor, 13 it's temperature of 2nd sensor, etc.

My code from thread:

#Worker
class SerialThreadClass(QThread):
    signal = pyqtSignal(str)

    def __init__(self, parent=None):
        super(SerialThreadClass, self).__init__(parent)
        self.serialport = serial.Serial()
        self.serialport.baudrate = 9600
        self.serialport.port='/dev/cu.usbmodem14101'
        self.serialport.open()

    def run(self):
        while True:
            rdln = self.serialport.readline().decode('utf-8').rstrip()
            self.signal.emit(str(rdln))  # pipe
            print(rdln)

My code in MainWindow:

#MainWindow 
        self.mySerial = SerialThreadClass()
        self.mySerial.start()
        self.mySerial.signal.connect(self.ui.average_temp.display)

I have no idea how to split this string across multiple qlcd widgets?! Sorry for my English ;)

EDIT: I found one idea, maybe it's not right way, but it's working:

self.signal.emit(str(rdln[0:2]))

But problem is not resolved in 100%, because I can read "block of signal", but how to make average based on this part of signal?

This has nothing to do with QLCD but with string processing.

If you're sure that the data is always in pairs, you can split the string like this:

values = [rdln[i:i+2] for i in range(0, len(rdln), 2)]

But, since you said that you need average values, those strings must be converted into numbers, so you'd better go off with this:

    total = 0
    for i in range(0, len(rdln), 2):
        total += (int(rdln[i:i+2]))
    average = total / len(rdln)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM