Reading from serial port(receiving triggers from MRI device)

I am running a fMRI task experiment and I would like to receive trigger from MRI device so I can sync my MRI imaging device with my psychopy task.

our lab’s MRI device is from SIEMENS, it use serial port for sending trigger pulse to task computer.
in order to synchronize psychopy and MRI we need to Read code from serial port in our psychopy.

At the moment, I don’t have any idea how to add serial port library and write the related codes in psychopy.

P.S. #1 : As extra information, we use the following code for our task in psychtoolbox (which is MATLAB base)

IOPort(‘CloseAll’);
triger_config = ‘BaudRate=57600 StopBits=1 Parity=None DataBits=8’;
[triger_handle, errmsg] = IOPort(‘OpenSerialPort’, ‘COM3’, triger_config);
% wait_for_triger(triger_handle);
IOPort(‘Purge’, triger_handle);
triger_data = 0;
while (triger_data ~= 115)
if (IOPort(‘BytesAvailable’, triger_handle))
[triger_data, when, errmsg] = IOPort(‘Read’, triger_handle, 1, 1);
end
end

can anyone please help me?

Hi There,

These docs and these docs might be useful for understanding serial ports with psychopy in general. Chapter 19 of this textbook also has some info.

My recommendations would be to make your experiment in Builder view. Then
add a code component to your experiment :code:. In the Begin Experiment tab use

from psychopy.hardware.serial import SerialPort
triggers = SerialPort('COM3', baudrate=57600)
trigger = '1'#expected character from the port

Then in the Each Frame tab use:

if thisTrigger in self.read(self.inWaiting()):
    continueRoutine = False # end this routine and move on

Hope this helps!

Hi Becca,

I know it´s been nearly a year since your answer, but i tried what you suggested and I always get this error message (see below). is this an old command that has been replaced by a newer one?

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

Hi @Pia1,

Could you please try replacing from psychopy.hardware.serial import SerialPort with import serial and let me know if that works?

Thanks,

Kim