BioSemi serial port, triggers, and PsychoPy

Post Reply
ldacorro
Posts: 1
Joined: Thu Sep 19, 2024 5:58 pm

BioSemi serial port, triggers, and PsychoPy

Post by ldacorro »

I am having issues with the BioSemi serial port for triggers. Currently, I am using PsychoPy for my experiments and it appears that the triggers are being properly sent from PsychoPy to the EEG system (via USB serial port) based on the real-time EEG shown in ActiView. However, when I view the recorded bdf files either in Matlab or ActiReader (BioSemi's EEG bdf reader), I am unable to see the trigger events. Has anyone had this issue before and if so, how did you get around it? Below is my PsychoPy code. I based my trigger code off of this one: https://github.com/drmarcj/psychopy_biosemi_serial

Code: Select all

is_production = True  # True if hooked up to EEG machine, False if debugging locally

fix_cross_signal = 1


def get_arrow_signal(condition) -> int:
    """
    Get the EEG stimuli value for the given condition
    """
    match condition:
        case '<<<<<':  # left option 1
            return 2
        case '>><>>':  # left option 2
            return 3
        case '>>>>>':  # right option 1
            return 4
        case '<<><<':  # right option 2
            return 5
        case _:
            raise Exception(f"Unexpected condition: {condition}")


# there are 4 conditions, so the number of trials is 4n: num_conditions (4) * num_reps (n)
if is_production:
    n_reps_prac = 2  # (2 * 4 = 8) for prod
    n_reps_block = 60  # (60 * 4 = 240) for prod

    import serial

    port = serial.Serial('COM7', 115200)
else:  # debug, no EEG hardware
    # redefine these here to prevent accidental override in prod
    n_reps_prac = 1  # (1 * 4 = 4) for debug
    n_reps_block = 5  # (5 * 4 = 20) for debug
    
 def trigger_eeg_stim(window: Window, component: TextStim, stimulus_pulse_started: bool, stimulus_pulse_ended: bool,
                     stimulus_pulse_start_time: Timestamp, global_clock: Clock, signal: int):
    """
    Update/draw components on each frame
    Send trigger to port, converting to bytes
    Acceptable trigger values are 1-255 (integer values)

    Does nothing if not production (using EEG)
    """

    if is_production:  # requires EEG hardware
        if component.status == STARTED and not stimulus_pulse_started:
            signal_bytes = str(signal).encode()



            window.callOnFlip(port.write, signal_bytes)
            stimulus_pulse_start_time = global_clock.getTime()
            stimulus_pulse_started = True
        
        # set port back to zero after 5 ms
        if stimulus_pulse_started and not stimulus_pulse_ended:
            if global_clock.getTime() - stimulus_pulse_start_time >= 0.005: #this allows port to be flushed and reset for next trig
                window.callOnFlip(port.write, str.encode('0'))
                stimulus_pulse_ended = True


    return window, stimulus_pulse_started, stimulus_pulse_ended, stimulus_pulse_start_time   
    

Post Reply