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!