5

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')[3]
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
Ghanima
  • 15,958
  • 17
  • 65
  • 125
BallerNacken
  • 333
  • 1
  • 4
  • 13

1 Answers1

8

From man 5 crontab:

Step values can be used in conjunction with ranges. Following a range with "/<number>" specifies skips of the number's value through the range. For example, "0-23/2" can be used in the 'hours' field to specify command execution for every other hour (the alternative in the V7 standard is "0,2,4,6,8,10,12,14,16,18,20,22"). Step values are also permitted after an asterisk, so if specifying a job to be run every two hours, you can use "*/2".

You are using a backslash, but the spec is a forward slash.

The reason to look in section 5 of the manual (man 5 ...) is that crontab is command, and a related type of configuration file. Commands are in section 1, files are in section 5. man all by itself defaults to section 1, so just plain man crontab does not give you the page with this information.

goldilocks
  • 60,325
  • 17
  • 117
  • 234