Sending triggers in oddball paradigm through serial port

Win10:
PsychoPy version (1.94.x):
As the tiltle asking about, I have problem in sending triggers through triggerbox to computuer to moniter and record the ERP. There was no mark on my ERP data ,and here is what I have tried:




And I don’t know how to caculate time in frame, so I used the time.sleep(0.4).

Sincerely Hope you can help me figure it out and thanks in adcance.

You shouldn’t use any functions like that in a Builder script (i.e. ones which impose any sort of delay or sleep period). Builder scripts maintains a timing system by actively drawing to the display on every screen refresh, typically at 60 Hz. By sleeping for 400 ms, you will break that drawing cycle (in this case, causing Builder to lose track of at least 24 screen refreshes on a 60 Hz display). This might cause unexpected timing errors for the other component you have created.

Builder maintains its own variable called t that keeps track of the time elapsed in seconds since the start of the current routine (as long as you don’t put it to sleep). So you can check the time and send the pulse, but also need to check that you do it only once. So put this in the “begin routine” tab, deleting the code that is there currently:

trigger_sent = False

and then put this in the “each frame” tab (so that the code will run on each screen refresh):

if not trigger_sent and t >= 0.4:
    port.write(bytes(1)) # or whatever is appropriate
    trigger_sent = True
1 Like

Thanks for your instruction, and I will try again.

Hi, Michael,I’d like to apologize for my delay feedback after such a long time because of the school closing.When I tried those code, there was another error report:


How can I fix it?
I’m really appreciate for your any sugesstion.

This probably isn’t anything to do with your code per se but seems to be related to the encoding that is being used to represent text in your .psyexp file.

If you open your .psyexp file in a text editor, what does the second line look like? Should contain something like this:

<PsychoPy2experiment encoding="utf-8" version="2020.1.1">

It will be interesting to see if somehow the encoding is GBK instead of UTF.

The other (and perhaps first) thing to try is remove all Chinese characters in your .psyexp file name and the folders leading to it, as some of those characters probably end up inside the .psyexp file too.

It’s strage that the first two lines are indeed :
#!/usr/bin/env python

-- coding: utf-8 --

But after changing the file name and moving it to the non-Chinese character file path.The error report changed to:


The position 732 is

It was I put code in ‘each frame’ tab .

No, I’m talking about your .psyexp file, not the generated .py file.

i.e. I think the problem is actually occurring when Builder attempts to create your .py file from your .psyexp file, and it is the latter that somehow still contains some odd characters. So I think you should be opening your .psyexp file in a ext editor and looking for the issue there.

The .psyexp file is in XML format so it will have lots of <> brackets surrounding everything. It looks quite different to Python code.