Data lost when logging with PySerial - Raspberry Pi Forums


i'm using pyserial log sirf binary messages through usb port. because it's binary data, have section of code looks end of each message , insert new line each message corresponds line in logger. when go through log files after, there data missing. there multiple lines of messages missing; others have wrong length, 2 separate messages got cut off , combined; , there few lines start of message found mid-line.

suspect issue overflow, although inefficient code or not enough processing power; however, i'm not sure how go debugging , fixing this. appreciated.

i'm using rpi b+, python 2.7, pyserial 3.0.1, , 115200 baud.

data logger:

code: select all

# import libraries import argparse import binascii import os import serial import time  # arguments def getarguments():     parser = argparse.argumentparser()     parser.add_argument('file',                         action = 'store',                           = 'data file parse')     parser.add_argument('time',                         action = 'store',                           = 'recording time')     return parser.parse_args()  # read port def portread(port):     while port.in_waiting == 0:         pass     return format(int(binascii.hexlify(port.read()), 16), '02x')  # main function def main():     # arguments     args = getarguments()          # open file     f = open(args.file, 'ab')          # open port     temp = args.file.split('_')[2]     c = temp.split('.')[0]     p = serial.serial(c, 115200)     p.reset_input_buffer()     p.reset_output_buffer()          # log data     t = int(args.time)     starttime = time.time()     timeelapsed = 0     while timeelapsed < t:         # find start of message         while os.stat(f.name).st_size == 0:             data = portread(p)             if data != 'a0':                 continue             prev = data             data = portread(p)             if data != 'a2':                 continue             f.write(prev + ' ' + data + ' ')             f.flush()             os.fsync(f)             break                  data = portread(p)         f.write(data + ' ')                  # find end of message         while data == 'b0':             data = portread(p)             f.write(data + ' ')             if data != 'b3':                 continue             data = portread(p)             if data == 'a0':                 f.write('\r\n')             f.write(data + ' ')                  # update timers         timeelapsed = time.time() - starttime          # close port     p.close()          # close file     f.close()  if __name__ == '__main__':     main()  

you put serial reader in thread - it's in own function anyway. keep adding either global variable or 1 passed argument when started thread. need flag variable stop thread writing data variable after had been used before deleted consumer. however, before did alter code portread's while loop emptied serial buffer , consuming code 'ate' resultant list, calling portread when list had shrunk nothing.


raspberrypi



Comments