Serial Port EEGbiosemi

Hi Stephanie,

OK the top one looks pretty good/close! (at least that is how I was used to seeing my triggers)

Please could you try the attached? I made an edit to the code in the Test1 routine (you can see my comments - let me know how you get on.

Becca
LexDecisionPhD.psyexp (81.2 KB)

Hi Becca,
So I tried your code,
even though it recorded all of the stimuli (80 in total) still there are problems with the triggers.

so there is an extra number there which is 230. I realize that is in some Spanish words and only happened with 3 words. what do you think this can be?
Also, is it a serial port thing that recognizes 1 as 49 and 2 as 50? Can that make any trouble at the moment of a amazing the data with EEGLAB? because when I used a p.port, it recognized the triggers like in the excel file.

Hi There,

OK it feels like we are getting closer! What happens if you write the integers instead of writing a string?

Try replacing:
port.write(b’1’)
with:
port.write(1)

I see this write method takes bytes and strings pySerial API — pySerial 3.4 documentation

But if you use integers does it appear in the data as expected?

I am less sure on what the stray trigger is - does the total number of triggers correspond to the total number of stimuli you expect? or are these extra?

Thanks,
Becca

Note: here is another method to try writing to the port Sending markers to EEG through serial port - #2 by Michael I think this was the solution for this user Sending markers to EEG through serial port - #12 by lindayu

Hi Becca,
So, to sum up, the new code
port.write(1) does not work.

I ended up adding this port.write(str.encode(chr(trigger))) codeand all of the 80 words are recorded. but still, sometimes i get 204 as a trigger. with this script my triggers are the same as in the excell file.
port.write(trigger)
if trigger ==1:
port.write(1)
elif trigger ==2:
port.write(2)
elif trigger ==3:
port.write(3)
port.write(str.encode(chr(trigger)))

OK how frequently is the 224 occurring? is the sum of the other triggers the sum of your trials?

Thanks,
Becca

Hi Becca,
So sometimes it happens 3 times during the whole experiment, sometimes more.
All my trials are recorded. When the 224 appears, this replaces one of the other triggers, so instead of 1, 2 or 3 is 224. This number because is replacing one of the other triggers, does not add other trials. So I end up having 80 trials recorded (what I need) but with some 224 in between.
Also, this only happens when I have 3 triggers or more.

interesting - OK to worst comes to worst triggers can be posthoc labelled with their corresponding condition in the analysis - but I know that is not what we want ideally.

Looking on the actiview support site I see that someone had a similar issue and they fixed it using the a write byte approach rather than a write integer method Trigger Codes Missing Sporadically - BioSemi.nl

In this post @Michael translates a trigger code to bytes Sending markers to EEG through serial port - #2 by Michael could we try that?

So it would be something like:

port.write(trigger.to_bytes(length = 1, byteorder = 'little')) 

Yes, i tried that and it didnt work.

Hmm ok I am afraid it is a bit tricky for me to advise without being able to test your specific set up - it sounds as though you have tried all the correct steps!

When you mention that it didn’t work - what happened exactly? was an error thrown? or was it just the same pattern as before?

Thanks,
Becca

Hi becca
This didn’t work
port.write(trigger.to_bytes(length = 1, byteorder = ‘little’))
When I only have the task with two triggers, the script works perfectly. But when I have three or more triggers sometimes I have 224 as a trigger

Hello,

I wonder if you need a flush statement after the trigger is sent? like this Triggering from Serial port - #2 by mdc

Hi Becca
This flush statement should be in begin routine?

I think immediately after you make your call to write

Becca

Hi Becca,
So the Flush didnt work,
i dont know if i put it in the correct place though, I have it like this
port.write(trigger)


if trigger ==1:
    port.write(1)
elif trigger ==2:
    port.write(2)
elif trigger ==3:
    port.write(3)

port.write(str.encode(chr(trigger)))
port.flush()

Hi Becca, Hola Stepanie_Aedo

I wanted to mention to you that PsychoPy serial() function is based on pySerial app.

This app can only send characters converted to bytes in the format set by default in the “to_byte” function.
That simply means that when ‘1’ is sent to the serial port an actual binary number “49” corresponding to the ASCII code of ‘1’ as a character is sent to the port.
If you want to send numbers to mark certain type of events you have to convert them first to_byte then send to the port.
You could probably try something like this:

val = 1
val_inbytes= val.to_bytes(1, byteorder=‘big’)
port.write( val_inbytes)

You may eliminate the if statement this way.

We are distributors of Biosemi products for Latin America

www.delarosaresearch.com

Stephanie, Feel free to contact me if you need any further assistance.

Best,

Claudio de la Rosa

1 Like

Hi there,
I ask for your help with the trigger sending via Serial Port EEGbiosemi. I’m using Windows 10Pro and Psychopy v2022.2.5 sith USB Serial Port recognize as COM3, and the same USB serial port showed here by Stephanie_Aedo (the only difference is the bits per seconds which in my case are = 9600).
I have been trying to run in my experiment all the solutions I have seen here in the forum. However, I’am at a loss to understand the multiple errors that appear and have decided to follow a tip I saw around here: to run a simple experiment first so I can indentify the main error. I took this one: trigger_test

I have it set up like this

…And the errors it yields are:

#### Running: C:\Users\LabCognicionyEmocion\PAULINA\trigger_test_lastrun.py ####
2937.1665     INFO     Loaded monitor calibration from ['2019_09_12 14:31']
pygame 2.1.0 (SDL 2.0.16, Python 3.8.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
2.3924     WARNING     We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['sounddevice', 'pyo', 'pygame'] (in that order).
Traceback (most recent call last):
1.1035     WARNING     Monitor specification not found. Creating a temporary one...
  File "C:\Users\LabCognicionyEmocion\PAULINA\trigger_test_lastrun.py", line 355, in <module>
    win.flip()
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\window.py", line 1222, in flip
    callEntry['function'](*callEntry['args'], **callEntry['kwargs'])
  File "C:\Program Files\PsychoPy\lib\site-packages\serial\serialwin32.py", line 306, in write
    raise PortNotOpenError()
serial.serialutil.PortNotOpenError: Attempting to use a port that is not open
################ Experiment ended with exit code 1 [pid:10304] #################
2968.8930     INFO     Loaded monitor calibration from ['2019_09_12 14:31']

I would be grateful for any guidance in this regard.
Thanks in advance

Dear @Becca or @Stephanie_Aedo , could you please help me?

Hi, try and change the bits per second. Unfortunately I ended up using a different programme to do my experiments.
but for what I can see, is a port related problem. Try and change the bits per second to baudrate = 115200) and see if this helps.

Best of luck