Skip to content

Instantly share code, notes, and snippets.

@datigrezzi
Created December 3, 2018 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datigrezzi/f703de92e903c922a2e9d3890e09e41d to your computer and use it in GitHub Desktop.
Save datigrezzi/f703de92e903c922a2e9d3890e09e41d to your computer and use it in GitHub Desktop.
# script to read data from serial port (typically sent by Arduino)
# The print it in terminal and save it to a csv file
# Iyad Aldaqre
# 24.11.2018
import serial
import time
import csv
device = '/dev/cu.usbmodem14101'
ser = serial.Serial(device, 9600)
print("Connection successful with:", device)
gsr_list = []
startTime = time.time()
print("Recording gsr data...")
print("Press Ctrl + c to stop")
try:
while True:
currentTime = time.time() - startTime
gsr_list.append([currentTime, ser.readline()])
except KeyboardInterrupt:
if len(gsr_list) > 0:
with open('serial_data.csv', 'w') as f:
w = csv.writer(f, delimiter=";")
w.writerow(['ts','gsr']) # header
for row in gsr_list: # iterate all items
w.writerow(row) # write row in csv
print("\nSaved gsr data successful")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment