Serial Port Code fmri-Experiment

Hi there,

I am struggeling to write code for a FMRI- Experiment. The trigger communicates via serial port (COM3 baudrate = 115200). The scanner sends the trigger ‘5’ to the trigger box. I tried using the code provided on this page Communicating with fMRI — PsychoPy v2023.2.0 - however I always get the error message that there is no psychopy.hardware.serial modul anymore.

from psychopy.hardware.serial import SerialPort
ModuleNotFoundError: No module named ‘psychopy.hardware.serial’
################ Experiment ended with exit code 1 [pid:24256] #################

That is why I modified the code, following questions that have been asked about this topic in this forum.

So far the code I wrote, recognizes the serial port when connected, but the sent triggers do not start my experiment. I have already tried to change the code in several ways, but unfortunately I didn´t find a solution.

This is the code used:

import serial 

#Create serial object for device at port 'COM3'
serialCom3 = serial.Serial(
    port='COM3',
    baudrate=115200
)

#Initialize components for Routine "trial" 

#point serialPort to device at port 'COM3' and make sure it's open
serialPort = serialCom3
serialPort.status = NOT_STARTED
if not serialPort.is_open:
    serialPort.open()

#Waiting for the Trigger "5"
while True:

    #Lesen Sie eine Zeile von der seriellen Schnittstelle
    received_data = serialPort.readline().decode().strip()
   
    #Check whether the received value corresponds to trigger "5
     if received_data == '5':    
        #START EXPERIMENT
        # from here on there is code that performs the stimuli for the experiment 

I´d appreciate any help I get!

Thanks in advance,
Pia

Hi @Pia1,

I would actually recommend adding following the guidance on the docs you’ve mentioned, but with the corrected import serial call at the start.

That should allow you to set up a routine that will end (and so start your experiment) when the trigger is received.

Thanks,

Kim