Recording response from external device

Hello everyone,

I am currently setting up an experiment in PsychoPy where participants in an fMRI scanner use a CurrentDesign 4-button response device to answer questions. Each button on the device corresponds to an answer option (1, 2, 3, or 4). The responses from this 4-button device are correctly saved in a text file on the computer, and we can see the values (1, 2, 3, 4) in the file.

However, I am facing an issue where PsychoPy does not recognize the input from the 4-button device during the experiment. I have tried using different components such as the keyboard response, joystick response, and button response in the PsychoPy Builder, but none of these seem to detect the responses from the 4-button device. I would like PsychoPy to detect the button response, move to the next question, and save the response.

I do not have expertise in coding, but with the help of ChatGPT, I tried to modify the code as indicated below:

I tried to define the button map at the beginning of the code:

from psychopy import core, event

button_map = {
‘1’: ‘button1’,
‘2’: ‘button2’,
‘3’: ‘button3’,
‘4’: ‘button4’
}

responses = [ ]

Then for each frame, I added this code:

keys = event.getKeys() # For keyboard-like input

for key in keys:
if key in button_map:
responses.append(button_map[key])
thisExp.addData(‘response’, button_map[key])
thisExp.addData(‘rt’, t)
continueRoutine = False # End routine on response (adjust as needed)

if responses:
thisExp.addData(‘responses’, responses)
else:
thisExp.addData(‘responses’, ‘None’)

Although the program did not give any error, Psychopy did not recognize the button response.

I also tried to define the input methods at the beginning of the code:

import serial

ser = serial.Serial(‘COM3’, 1200)

and in the each frame tab, I added this code:

if ser.in_waiting:
key = ser.read().decode(‘utf-8’).strip()
if key in button_map:
responses.append(button_map[key])
thisExp.addData(‘response’, button_map[key])
thisExp.addData(‘rt’, t)
continueRoutine = False # End routine on response

Although the program did not give any error, Psychopy did not recognize the button response again.

Has anyone encountered a similar issue or have any suggestions on how to get PsychoPy to recognize and record the responses from this specific 4-button device? Any insights or solutions would be greatly appreciated.

Thank you for your consideration,

Kind regards,
Asya Evcil