Wilbert's website at SocSci

> Software> Python> SR Buttonbox

python/srbox.html 2016-02-01

Srbox library

If you wish to use the sr buttonbox (also known as e-Prime buttonbox), please download libsrbox.py. The following example shown how to change the lights on the sr buttonbox and how to receive buttonpresses. Make sure libsrbox.py is in the same folder as your experiment or in a standard Python library folder. Change /dev/ttyS0 to the name of the serial port on your computer (for instance COM2).

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time, libsrbox
print ("testing srbox")
srbox = libsrbox.libsrbox(time, "/dev/ttyS0")

print ("testing light")
srbox.send(chr(int('11111111', 2))) # 255 all lights on
time.sleep(.2)
srbox.send(chr(int('11111110', 2))) # 254 one light off
time.sleep(.2)
srbox.send(chr(int('11111101', 2)))
time.sleep(.2)
srbox.send(chr(int('11111011', 2)))
time.sleep(.2)
srbox.send(chr(int('11110111', 2)))
time.sleep(.2)
srbox.send(chr(int('11101111', 2)))
time.sleep(.2)
srbox.send(chr(int('11100000', 2))) # 32 + 64 + 128 = 224, all lights off

print ("please press a button")
print("you pressed: {}".format(srbox.get_button_press()))

Srbox in core Python

With slightly more elaborate code you can use core Python with the sr buttonbox. No need to install anything

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial, sys
dev = ("/dev/ttyS0", "COM1")[sys.platform=='win32']   # device name
srbox = serial.Serial(dev, timeout=0, baudrate=19200) # open device

# start sending button presses
srbox.flushOutput()
srbox.flushInput()
srbox.write('\xA0')

# all lights on but one
srbox.write(chr(int('11111011', 2)))

# wait for button press
a = chr(0)
while len(a) == 0 or ord(a) == 0:
	a = srbox.read(1)

print("#{}#".format(ord(a)))

# stop sending button presses
srbox.flushOutput()
srbox.flushInput()
srbox.write('\x20')

srbox.write('\x60') # turn off all lights