Receiving trigger from fMRI Serial Port

Hi all,

I am trying to receive triggers from an MRI that uses a serial port trigger. I have read this PsychoPy webpage: Communicating with fMRI — PsychoPy v2024.2.2

Based on this, I tried using the example code component with: “from psychopy.hardware.serial import SerialPort”

However, I get an error saying: "ModuleNotFoundError: No module named ‘psychopy.hardware.serial’.

As such, I was wondering if anyone has a solution for the correct module to import? Any help would be greatly appreciated!

I might also suggest the webpage be updated if it is not up to date or if it needs more information to get this code to work.

Thank you in advance!

Do you received any answer?

Nope! I never received an answer.

I did figure out a workaround in my case, but it does seem like the “psychopy.hardware.serial import SerialPort” is not currently working.

If I may ask, how did you solve it?

I’m currently trying to have my experiment to work with a serial port trigger but I have limited test time and it’s a bit frustrating that their documentation and previous solutions are not properly working…

I agree that is definitely frustrating that the PsychoPy documentation does not seem to be valid. In my case, the serial port was actually a pstbox, so I was able to utilize ioHub and receive triggers and button responses this way. My code resembles the code from this post on E-prime button boxes (Using an E-Prime Button Box - #7 by richard). I’ve also heard from others at my institution that using the serial python library also works with some specific settings in place, though I have limited experience with this.

Hope this helps (and hope the PsychoPy team can update the fMRI trigger documentation at some point soon)!

I’ve brought this to the attention of the team, so hopefully it will get updated soon. It’s not my area, so not something I can help with directly. Have you tried the new Builder components?

From my understanding of the documentation, the Serial Out component works by sending the trigger to another device (like an EEG), not receiving one.

In our case what we were looking for is a proper way to start the experiment after receiving a trigger from the fMRI. In my case I’ve been testing the serial python library but I’m still struggling to detect any input from the serial port.

How about this code from Recording information from an Arduino via serial port — PsychoPy v2024.2.4

Begin Experiment

import serial
import time
port = serial.Serial('COM4', 9600) # Change to correct COM port and baud rate.
time.sleep(1) #Give the device some time to wake up

Begin Routine

res = []

Each Frame

res.append(port.readline())

End Experiment

port.close()

Thanks for bringing it to our attention about the docs! These are, as you’ve pointed out, outdated now. Once we’ve got a working solution here I can update those.

As @wakecarter suggests, instead of:

from psychopy.hardware.serial import SerialPort

could you please try:

import serial

and let me know if that works?

Thanks,

Kim

Hi Kimberley, thanks for your answer! I tried using Begin Experiment:

import serial
port = serial.Serial('COM4', 9600)

and Each Frame:

input = port.readline()

with a text component to display the input during the experiment, but no input was received.

The same serial port works well with another software, so we don’t know where the problem might be. I think we’ll probably resort to doing a manual trigger when the fMRI acquisition begins and take that into account when we analyze the results.

Thanks again!

In many of our setups we have the port read just at the beginning of the routine to launch things.

Before you resort to manual triggering, do you have any response boxes on the system that are otherwise catching the TR pulse trigger? Like a Current Design 932 box, PST Chronos, Biopac, or Cedrus?

It might be possible to use those as a keyboard emulator to start your experiment.