I have some code that parses serial data into chunks for Kivy to read.

For some reason the code will work perfectly for a while and then stop. I think I need to clear the buffer/memory somewhere but I have no idea how to do this correctly.

The code looks for an STX (02 in HEX) and reads data until ETX (03). Then it formats the data for Kivy to output to a display.

                while True:
                    buf = self.ser.read()
                    if buf.encode('hex') == '02':
                        data = '[02]'
                        while True:
                            tmp = self.ser.read()
                            if tmp.encode('hex') == '03':
                                data += '[03]'
                                data += tmp
                        print 'Finding STX... ', buf.encode('hex')
                except serial.SerialException as e:
                    print e

        if data.startswith('[02]'):
            data_header = data[4:6]
            data_list = ['00', '02']
            if data_header in data_list:
                content = data[7:-4]
                tmp = '[b][color=FFEC1F]' + content + '[/color][/b]'
                print "Updating content: ", data_header, tmp
                self.root.ids['lb_' + data_header].text = tmp

When I notice the display has stopped and SSH into it, I receive hundreds of lines of

Updating content: 00 [b][color=FFEC1F]200[/color][/b]

Updating content: 02 [b][color=FFEC1F] 383[/color][/b]

Updating content: 00 [b][color=FFEC1F]199[/color][/b]

Updating content: 02 [b][color=FFEC1F] 381[/color][/b]

Updating content: 00 [b][color=FFEC1F]199[/color][/b]


Until it catches up to the latest data.

  • I have noticed some funky things happening in the latest raspbian when the screen blanks out. I would suggest testing with the following: Add consoleblank=0 to the end of the first line of your /boot/cmdline.txt. This will disable the screen sleep or whatever its actually called. – Brett Reinhard Feb 23 '17 at 23:07
  • Thanks I'll try this. Though at the moment the screen doesn't appear to be sleeping. The Kivy app is still displayed but there is no data because the python script has hung/frozen. – sqgun Feb 23 '17 at 23:54
  • ah ok, I must have misread the original post. – Brett Reinhard Feb 23 '17 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.