I have a problem with my python script. It looks like this:
#!/usr/bin/python import time import serial import datetime import sys Y = datetime.datetime.now().strftime('%Y') filedate = datetime.datetime.now().strftime('Z'+Y+'%m%d%H%M') sys.stdout=open(filedate+".dat","w") #sys.stdout=open("test.dat","w") print "-Ceilometer Logfile" time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print "-File created: "+time_now ser=serial.Serial( port='/dev/ttyUSB0', baudrate = 19200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, ) counter=0 ser.flushInput() while 1: tdata = ser.read() time_now2 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') time.sleep(3) data_left = ser.inWaiting() tdata += ser.read(data_left) print "-"+time_now2,"\n", print tdata
It reads the input coming from a serial port using the USB port on the Pi and a serial to USB adapter. There are two issues:
1) The script works fine once started. When I cancel it and start it again there is no error, but the output file is completely messed up. The timestamp is randomly somewhere, there is data missing and there are random digits or character printed in the file. Any what can cause this? My guess is an issue with the serial buffer?!
2) I tried to execute the program every 5 minutes (for test purposes, normally it would be once an hour). But the program created an output file every minute instead of every five minutes. And all the files were messes up as said earlier. Even though everything was fine before that test.
The cronjob looks like this:
*\5 * * * * sudo python /home/pi/readserial.py