send triggers via USB trigger interface with python

Post Reply
eegalbano
Posts: 1
Joined: Tue Mar 14, 2023 10:38 am

send triggers via USB trigger interface with python

Post by eegalbano »

Hi!

We bought the USB trigger interface last year.

https://www.biosemi.com/faq/USB%20Trigg ... 0cable.htm

We could install it, and it works fine, but we never managed to send all 8 bytes, that is, numeric codes between 1 and 255. That is, we had python code that worked only for 1 thru 127. This was good enough for previous experiments, but for a new study, it would be convenient to have access to all codes (1 thru 255).

We spent a couple of frustrating hours trying to figure out how to write python code to send whatever code we want via the USB trigger. We succeeded and want to share the good news so that other people do not have to waste their time on this.

# Import serial package
import serial

# Set up port
port = serial.Serial("COM4", baudrate=115200)

# Create dictionary to define conditions
# This is optional but convenient
condition_dict = {'low' : 1, 'high': 255}

# Assume that we are in condition "low" and want to send a trigger.
nowcond = condition_dict['low']

# Define trigger as int
# Note: With int(), nowcond could be a string such as "1" or "255"
trigger = int(nowcond)

# turn integer into binary
# https://www.geeksforgeeks.org/how-to-co ... in-python/
port.write(trigger.to_bytes(1, 'big'))
# '1' to get 1 byte
# byte starts from the right
# 'big' because higher bit is to the left

We had a momentarily feeling like we were geniuses, until we were hit by the simple elegance of python. One line of code to do something we have wanted to do for the past few years (while still working with parallel ports).

Best of luck!

Post Reply