Button box from a serial port

Dear all,

I am now using a button box to collect responses from participants.

The button box has a serial port that is converted to a USB connection.

So far, this is how I am reading the button box in:

import serial
import serial.tools.list_ports as port_list
#ports = list(port_list.comports()) # search for the devices
#for p in ports: print (p)
    
ser = serial.Serial('/dev/ttyUSB0', 19200, timeout=1)

if(ser.isOpen() == False): #open the serial port only if NOT open yet
    ser.open()

ser.flushInput() #erase all info in the box about previous button-presses

At this point, however, how can I be able to collect the responses from the button box? I was not able to find any guidelines online.

I would like to be a little bit more specific on the issue, I think it might be relevant for more people.

I am trying to implement this serial port thing into Psychopy.

I have two routines.

  1. first routine is named "welcome".
    It contains a text component (text) lasting 0.5s and a code component code_1.
    code_1 has a Begin Experiment code as follow
from binascii import hexlify
import serial
import serial.tools.list_ports as port_list

ports = list(port_list.comports()) 

count=1

ser = serial.Serial('/dev/ttyUSB0', 19200, timeout=0)
welcomeClock = core.Clock()

if(ser.isOpen() == False): #open the serial port only if NOT open yet
    ser.open()

ser.flush()

def get_stimulation_onset_time():
    # We can use this to keep track of stimulation onset.
    global stimulation_onset_time
    stimulation_onset_time = core.getTime()
  1. second routine is named trial.
    It has a text component (stimulus1) with no stop duration; and a code component code_2.

code_2 has the following Begin Routine code:

ser.flush()
rt_measurement_started = False

a Each Frame code:

if not rt_measurement_started and stimulus1.status == STARTED:
     #Will set the global variable `stimulation_onset_time` to the exact time the visual stimulus `stimulus` is being presented.
    win.callOnFlip(get_stimulation_onset_time)
    rt_measurement_started = True
    ser.flush()

while count==1:
    for line in ser.read():
        hex = hexlify(line)
        num=int(hex, 16)
        num=num-1
        responseTime = core.getTime()
        print(num)
        print(responseTime)
        count= count+1
        continueRoutine = False
        break

and a End Routine code:

if count>1:
    rt = responseTime
    button = num
else:
    rt = None
    button = None

thisExp.addData('RT', rt)
thisExp.addData('Response_Button', button)

Something is really not working.

When the experiment starts, text remains on screen for an indefinite time (more than 0.5) and as soon as I make a button press, the experiment closes (without any error tough) and stimulus1 is never displayed.
The functions print(num) and print(responseTime) are at least partially working as I get the correct value of the button pressed and the time which however I don´t know if it´s correct.

Hello @GioP. I am going through a similar problem- I am trying to get psychopy recognize 8 buttons responses that are connected to the computer with a serial port (“COM1”) and a USB connection. I have learned that psychopy does recognize the serial port. However, I haven’t manage to find a code to get the responses/buttons recognized.

Did you managed to solve your issue and have any suggestion? I would appreciate any comment.

Best,
Mauri