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!

pabflore
Posts: 1
Joined: Tue Nov 26, 2024 10:47 am

Re: send triggers via USB trigger interface with python

Post by pabflore »

I’ve been working on similar setups and recently developed a set of utility scripts and examples that might complement your solution. The code was developed to be as much self-explanatory as possible, since its for a colleague. The code is just a guiding tool, and I hope it can help others, since hardware interface is not something not many are used to. There are also some examples on how to define and send bytes with python.

All the code, examples and documentation are in the following repository:
https://github.com/PipaFlores/Serial-Interface

There are two utility classes, one for the Biosemi trigger interface and another for an Arduino implementation.

Regarding the Biosemi, the code is designed to be used as following:

Code: Select all

import time
from SerialTriggerBiosemi import BiosemiTrigger

## To be used with the BIOSEMI trigger interface

# Initialize serial port, with an initial delay of 3 seconds
biosemi_trigger = BiosemiTrigger("COM7", initial_delay=3) # change to the correct port


# Test the connection by sending three 8 ms pulses to trigger 1 every one second
biosemi_trigger.test_trigger(signal_byte=0b00000001)
time.sleep(1)
biosemi_trigger.test_trigger(signal_byte=0b00000001)
time.sleep(1)
biosemi_trigger.test_trigger() # Since the signal is not specified, the default signal (1) is used

Post Reply