0

I am using a script that takes the arduino serial and writes it to an out.dat file. The scripts works fine when run after boot, however when I added the script to /etc/rc.local it runs the script, but does not record the information

#!/usr/bin/python
# get lines of text from serial port, save them to a file

from __future__ import print_function
import serial, io
import datetime

addr  = '/dev/ttyACM0'  # serial port to read data from
baud  = 9600            # baud rate for serial port
fname = 'gps-log'+datetime.datetime.now().strftime("%m-%d-%Y %H-%M-%S")+'.dat'   # log file to save data in
fmode = 'a'             # log file mode = append

with serial.Serial(addr,9600) as pt, open(fname,fmode) as outf:
    spb = io.TextIOWrapper(io.BufferedRWPair(pt,pt,1),
        encoding='ascii', errors='ignore', newline='\r',line_buffering=True)
    spb.readline()  # throw away first line; likely to start mid-sentence (incomplete)
    while (1):
        x = spb.readline()  # read one line of text from serial port
        print (x,end='')    # echo line of text on-screen
        outf.write(x)       # write line of text to file
        outf.flush()        # make sure it actually gets written out

Credit to https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=64545

Note, it is supposed to create a new file named after the date and time. When I boot to command line, it prints the serial, so I know the program has executed. However, when I power down there is no .dat file to be found.

2

fname = 'gps-log'+datetime.datetime.now().strftime("%m-%d-%Y %H-%M-%S")+'.dat'

Appears to not involve any path. Where are you expecting to find these files?

I think an init service which does this will default to writing to /, so you could check there. However, you probably want to append an absolute path so you have a better place to store these and find them.

fname = '/var/log/gps/gps-log'+ ....
  • 1
    As @goldilocks points out the file may have been created but not where you are expecting it to be. You can use the find command to check: find / -name '*.py' – Steve Robillard Apr 12 '16 at 19:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.