Sending triggers from psychopy to Brain vision triggerbox

Hello,

I want to do an EEG experiment with Psychopy and Brainvision software (through BrainVision Triggerbox). I know how to use Builder but I am pretty amateur in coding. Since I must use a serial port for sending triggers, I could not use the Builder. My planned experiment is to playing sounds as the stimulus and sending the time of the stimulus to the EEG recording system. For a test of sending triggers, I just created a simple version of the experiment like this:

I have found this code suggested:

import serial
import time
import threading

Connected = True
PulseWidth = 0.01

def ReadThread(port):
    while Connected:
        if port.inWaiting() > 0:
            print "0x%X"%ord(port.read(1))

# Open the Windows device manager, search for the "TriggerBox VirtualSerial Port (COM6)"
# in "Ports /COM & LPT)" and enter the COM port number in the constructor.
port = serial.Serial("COM6")

# Start the read thread
thread = threading.Thread(target=ReadThread, args=(port,))
thread.start()

# Set the port to an initial state
port.write([0x00])
time.sleep(PulseWidth)

# Set Bit 0, Pin 2 of the Output(to Amp) connector
port.write([0x01])
time.sleep(PulseWidth)

# Reset Bit 0, Pin 2 of the Output(to Amp) connector
port.write([0x00])
time.sleep(PulseWidth)

# Reset the port to its default state
port.write([0xFF])
time.sleep(PulseWidth)

# Terminate the read thread
Connected = False
thread.join(1.0)

# Close the serial port
port.close()

But to be honest, I could not find in which section I should use it.
I would be happy if someone helps me.

1 Like

Hi Shiva,

have you got any feedback or find the way to make it work.

Thank you very much,
Leidy

Hi Leidy and Shiva,

Were you able to find solutions to make this work?

Best,
Sobana

Hi,

I had the same problem and I think it worked liked that:
(I used the builder with code)

#Begin Experiment
if expInfo['EEG'] == "y":
    import serial
    import time
    from psychopy import core, visual, event
    port = serial.Serial("COM3")

#Begin Routine
if expInfo['EEG'] == "y":
    triggerSent = False
    port.write([0])

#Each Frame
if expInfo['EEG'] == "y" and trVern1.status == STARTED and not triggerSent:
        if vernTrials.thisIndex == 0:
            port.write([10])
            triggerSent = True
        elif vernTrials.thisIndex == 1:
            port.write([11])
            triggerSent = True
        else:
            port.write([50])

However it’s a long time ago since I wrote this and I don’t remember exactly if it worked. Still hope it helps.

The if statement is not necessary, I only implemented it so that I can run the experiment with or without the trigger box.

Helen