# Trying to send key press via serial port to EEG Software

**URL:** <https://discourse.psychopy.org/t/trying-to-send-key-press-via-serial-port-to-eeg-software/32928>\
**Category:** Builder\
**Created:** [January 24, 2023, 7:05pm UTC](https://discourse.psychopy.org/t/trying-to-send-key-press-via-serial-port-to-eeg-software/32928 "2023-01-24T19:05:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve\_o](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@steve\_o](https://discourse.psychopy.org/u/steve_o)\
**Post date:** [January 24, 2023, 7:05pm UTC](https://discourse.psychopy.org/t/trying-to-send-key-press-via-serial-port-to-eeg-software/32928/1 "2023-01-24T19:05:03Z")

</div>

**OS** : Win11  
**PsychoPy version** : 2022.2.5  
**Standard Standalone?** yes  
**What are you trying to achieve?:**

In my Lab we are trying to send key presses as stimuli via a serial Port connection to our EEG-Software (BrainVision Recorder). I know that there have been other Posts with this exact same problem, yet when trying to implement those solutions the Trials crashed after starting.

**What did you try to make it work?:**

I already tried to implement the solutions from these two posts:  
[Post 1](https://discourse.psychopy.org/t/send-trigger-biosemi-actiview-for-every-key-press/27435)  
[Post 2](https://discourse.psychopy.org/t/sending-multiple-triggers-in-one-routine-using-serial-port/27427)

Unfortunately I couldn’t get those solutions to work…

This is the current configuration:

Note: key\_resp \> Data is set to “all keys”

**Begin experiment:**  
import serial  
import time

Only used at begin and end of experiment…  
PulseWidth = 0.01

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

Set trigger to zero  
port.write([0x00])  
time.sleep(PulseWidth)

**Begin routine:**  
pulse\_started = False  
pulse\_ended = False  
CountedKeys = 0

**Each frame:**

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/0/d/0dc4a6a735526f57c084b46d2563911805c52bce.png)

**What specifically went wrong when you tried that?:**

**Alerts:**  
Alert 4210:JavaScript Syntax Error in ‘Begin JS Experiment’ tab. See ‘Line 1: Unexpected token’ in the ‘Begin JS Experiment’ tab.

**Stdout:**  
Traceback (most recent call last):  
File “C:\Users-----\Documents\psychopy-------\trust-distrust - Backup\_lastrun.py”, line 505, in   
if stimulus.status == STARTED and not pulse\_started:# where “mything” refers to a visual thing you are trying to sync the trigger to (so you could have one of these for different component’s in the same routine  
NameError: name ‘stimulus’ is not defined  
################# Experiment ended with exit code 1 [pid:4860] #################

I have no idea about Python and just tried to implement the existing solutions…

Any help would be kindly appreciated 🙂
