Python record script posner test

This is the script to be run in the computer, after the arduino has already been hooked up.

In linux the way to run this is to copy the text below in a new file with name ending in “.py” and running “path-to-file/” in a terminal:


# script to generate a file for storing data from the posner test run in arduino:

#written by Andre Maia Chagas 24.06.2013
#contact: openeuroscience(at)
#distributed under Creative Commons Attribution-ShareAlike 3.0 Unported License.

#this script opens the serial port and reads data coming from an arduino UNO.
#it also creates a folder and a file to store the read data, in case they don’t

#import necessary libraries:
import os #library that enables system operations
import datetime #library to get the current date
import serial #import library to read/write serial port

#serial port address
#check to see if there are any devices connected to the serial port:
if os.path.exists(portName):
#create the serial comm object (first par is the serial port and the second,
#the baud rate, in this case 115200 bits per second)
ser = serial.Serial(portName, 115200)
print(“no serial devices connected!”)
print(“continuing without serial communication \n”)

#get the current date

#path of the file to open

#set the subject name
#set the name of the file
print tmpNow[0:10]+”_”+subjectName+”.txt”
print fileName

#check to see if the folder where you want to create
#the file exists. if it doesn’t exist, create it:
if not os.path.exists(folderPath):

#create/open file in the specified folder in append mode:

#write the date and the subject name at the beggining of the appended part
newFile.write(tmpNow[0:19]+”_”+subjectName+” \n”)

#if there is a serial device connected:
if serFlag==1:
#write one byte to the arduino
#wait for a specific signal from the arduino
while ardCheck<0:
#now that one received the signal from the arduino,
#reset the “ardCheck” flag
print “device connected”
#now make python read data undefinetly untill a “break” command is called:
while True:
#if the termination flag “*999**” is found, send the break command
if tmpVar==”*999**\n”:
print “test done!”
print “no serial port connected!”
newFile.write(“no serial port connected!”)

#Close the file

2 thoughts on “Python record script posner test”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: