0
votes

I'm trying to write a python script to read values from the arduino's serial port and write it to a file so I can log the data.

The arduino code is long and complicated, but I'm using Serial.println() to print an integer to the serial port (located at /dev/ttyACM0)

import sys
import serial
import getpass
import datetime
import instrumentDriver

"""location of the arduino"""
location = '/dev/ttyACM0'

"""Connect to the arduino"""
arduino = instrumentDriver.serialDevice(location)

"""Successfully connected!"""
filename = str(sys.argv[1])
dataFile = open(filename+'.dat','w')

"""The main loop -- data is taken here and written to file"""
while True:
    try:
        datum = arduino.read()
        print datum
        dataFile.write(datetime.datetime.now().strftime("%Y-%m-%d"))
        dataFile.write('\t')
        dataFile.write(datum)
        dataFile.write('\n')
    except:
        dataFile.close()
        break

The instrumentDriver.py is just a wrapper for pySerial:

class serialDevice:
    def __init__(self,location):
        self.device = location
        self.port = serial.Serial(location,9600)

    def write(self,command):
        self.port.write(command)

    def read(self):
        return self.port.readline()

I've used this block of code years ago and it worked fine, but it seems to be failing right now and I'm not entirely sure why. I get a SyntaxError on line 45:

scottnla@computer-1 ~/Documents/sensorTest $ python readSerial.py file
  File "readSerial.py", line 45
    print datum
        ^
SyntaxError: invalid syntax

I've tried changing the print statement, but no matter what I'm printing, I get a syntax error -- I speculate that the problem may actually be with the arduino.read() line.

Any advice would be greatly appreciated!

1
Are you using phyton 2 or 3? Try to force your choise. Also what error and where?Lesto
Is that your exact code? There are indentation problems in the block following the """write it to file, along with a timestamp""" docstring (also -- why are you using docstrings as regular comments? See legacy.python.org/dev/peps/pep-0008/#comments for PEP8 information on this)bgporter
Whoops, what an embarrassing mistake! Included the error. I'm using python 2.7.3nathan lachenmyer
The indentation errors were a formatting error; i've fixed them now.nathan lachenmyer
Do you get a syntax error if you comment out the print line?Craig

1 Answers

1
votes

There is still an indentation issue; rewritten as below, it should run:

import sys
import datetime

class serialDevice:
       def __init__(self,location):
              self.device = location
              self.port = sys.stdin # changed to run on terminal

       def write(self,command):
              self.port.write(command)

       def read(self):
              return self.port.readline()

"""location of the arduino"""
location = '/dev/ttyACM0'

"""Connect to the arduino"""
arduino = serialDevice(location)

"""Successfully connected!"""
filename = str(sys.argv[1])
dataFile = open(filename+'.dat','w')

"""The main loop -- data is taken here and written to file"""
while True:
    try:
        """retrieve the raw analog number from the arduino's ADC"""
        datum = arduino.read()
        """write it to file, along with a timestamp"""
        print datum
        dataFile.write(datetime.datetime.now().strftime("%Y-%m-%d"))
        dataFile.write('\t')
        dataFile.write(datum)
        dataFile.write('\n')
    except KeyboardInterrupt:
        """this allows for the user to CTRL-C out of the loop, and closes/saves the file we're writing to."""
        dataFile.close()
        break