# Triggers from PsychoPy to triggerbox from brain products

**URL:** <https://discourse.psychopy.org/t/triggers-from-psychopy-to-triggerbox-from-brain-products/26725>\
**Category:** Other\
**Created:** [January 7, 2022, 1:33pm UTC](https://discourse.psychopy.org/t/triggers-from-psychopy-to-triggerbox-from-brain-products/26725 "2022-01-07T13:33:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swijeakumar](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@swijeakumar](https://discourse.psychopy.org/u/swijeakumar)\
**Post date:** [January 7, 2022, 1:33pm UTC](https://discourse.psychopy.org/t/triggers-from-psychopy-to-triggerbox-from-brain-products/26725/1 "2022-01-07T13:33:22Z")

</div>

Good afternoon,

We are unable to get our triggers to work for our NIRSScout system by Brain Products. Our connection is the following:  
Stimultation Windows laptop (running Psychopy) → USB cable → Brain Products TriggerBox → TriggerBox “Amp” output → BP-245-1565 → NIRScout trigger cable → NIRScout trigger input on the machine.

I see some queries about this on the forum but did not see a definitive solution. Can someone help us with this?

The code that Brain Products gave us is :

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()

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 8, 2022, 5:37pm UTC](https://discourse.psychopy.org/t/triggers-from-psychopy-to-triggerbox-from-brain-products/26725/2 "2022-01-08T17:37:15Z")

</div>

Hi @swijeakumar,

could you surround the code example you share with triple backticks (```)? It will make it more readable. 🙂  
It would also be helpful if you were a bit more specific in what you mean by being unable to get the triggers to work. The code you pasted is a little hard to read, but it looks like it is defining a function to send triggers with - how do you use that function in your code, do you get any errors if you try to do so or no errors but also no recorded triggers?

---

<div class="post-metadata">

**Author:** ![swijeakumar](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@swijeakumar](https://discourse.psychopy.org/u/swijeakumar)\
**Post date:** [January 10, 2022, 5:07pm UTC](https://discourse.psychopy.org/t/triggers-from-psychopy-to-triggerbox-from-brain-products/26725/3 "2022-01-10T17:07:30Z")

</div>

Hi there,

We have run into a more specific and strange issue now - the first 20 or so triggers appear on our neuroimaging software and then no other triggers appear. I have pasted the code below. I could also send you my task if you could take a look or chat on Teams?

I inserted a custom code component in the Trial Routine and added the following bits of code to the different tabs:

Under ‘Begin Experiment’:

````auto
import time
import threading
Connected = True
PulseWidth = 0.5
            
# 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("COM3")

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

Under 'Begin Routine':

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

# 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)```

Then I inserted a new custom code component at the very end of the experiment and added the following code under 'End Experiment':

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

# Close the serial port
port.close()```

Any advice on what we are doing wrong would be massively appreciated.

Best,
Sobana
````

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 14, 2022, 5:11pm UTC](https://discourse.psychopy.org/t/triggers-from-psychopy-to-triggerbox-from-brain-products/26725/4 "2022-01-14T17:11:51Z")

</div>

Hi @swijeakumar, sorry I respond after a delay. The issue you report now indeed sounds strange.  
I never sent triggers in the same way as you do, so I may be of little help here, but I will try. 🙂 First of all I would try to see if sending the triggers through a python script gives you the same problem. So you could reformat the code snippet you pasted so that, for example, it sends one trigger every 250 ms, sending triggers from 1 to 255 (sequentially). If this fails (at some point) you will at least know: a) that PsychoPy is not part of the problem; b) how many triggers are received (the value of the last received trigger).
