Printing output from serial port

Hi there,

I’m creating a hand dynamometer experiment using the serial port. I’ve been able to confirm the port is open and I can see from the hand dynamometer (via Biopac’s acqknowledge software) that pressure is being applied. Before I try to get events pushed from PsychoPy to Acqknowledge I’m trying to print the level of effort made into the PsychoPy output from the hand dynamometer to a CSV file.

At the moment no CSV file is being created, I can’t see any figures in the output for the pressure being applied and I’m not too sure why. Very grateful for any advice.

My code currently:

#import the relevant packages
import serial
import os
import csv
import time
from random import randint
from psychopy import gui, core

Configure the serial port. This will timeout if data is not recieed within 1 second

ser = serial.Serial(‘COM4’, baudrate = 9600, timeout = 1)
#print serial name to confirm which port was used.
print(ser.name)

if (ser.isOpen() == True):
print(“Serial port is open.”)
else:
print(“Serial port is closed.”)

#Read data from the hand dynamometer
dynamometer_value = ser.readline().decode().strip()
print(dynamometer_value)

#Create a list to store data for each stimulus
results =

#Create a list of all the possible stimuli labels
stimuli_list = [‘rank_1_trivia’, ‘rank_2_trivia’, ‘rank_4_trivia’, ‘rank_5_trivia’]

#Loop through stimulus and record type
for stimulus in stimuli_list:
print(f"Showing: {stimulus}")

#wait for participant to squeeze the hand dynamometer
print("Please squeeze the hand dynamometer")
time.sleep(3) # Time given to squeeze in seconds

#Read data from the hand dynamometer
dynamometer_value = ser.readline().decode().strip()

#Store the stimulus type and the corresponding value
results.append({'stimulus': stimulus, 'effort': dynamometer_value})
print (f"This is the: {dynamometer_value} you are making for this trial")

Create a directory for data if it doesn’t exist

data_dir = ‘data’
if not os.path.exists(data_dir):
os.makedirs(data_dir)
print(f"Directory ‘{data_dir}’ created.“)
else:
print(f"Directory ‘{data_dir}’ already exists.”)

#Ensure participant ID is being captured
#dlg = gui.DlgFromDict(dictionary={‘Participant ID’: ‘’})
#if dlg.OK:

participant = dlg.data[0]

print(f"Participant ID: {participant}")

#else:

print(“Dialog cancelled. Exiting.”)

core.quit()

#create the file name using the particiapnt ID
filename = f"{data_dir}/participant_{participant}_grip_data.csv"
print(f"Filename: {filename}")

#open the data file and save data
with open(filename, ‘w’, newline=‘’) as csvfile:
fieldnames = [‘participant’, ‘stimulus’, ‘effort’]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
print(“CSV header written.”)

#loop through and write each trial's data, including particiapnt number
for trial in results:
    writer.writerow({'participant': participant,
                     'stimulus': trial['stimulus'],
                     'effort': trial['effort']})

print(f"Data saved to {filename}")