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)

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);

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
    continueRoutine = False # end this routine and move on

Hope this helps!