pyserial icon indicating copy to clipboard operation
pyserial copied to clipboard

Sending triggers to psychopy through EEG BioSemi serial port

Open martinsteph opened this issue 3 years ago • 16 comments

Hi Everyone, I hope someone can help. Im trying to send triggers (from an csv or excel file) using a BioSemi EEG trigger USB cable to psychopy. This is the code I have begin experiment import serial port = serial.Serial("COM5", baudrate = 115200) Begin Routine port.write(trigger) if trigger ==6: port.write(6) elif trigger==7: port.write(7) elif trigger==3: port.write(3) elif trigger==4: port.write(4) port.write(str.encode(chr(trigger))) The main problem here is that even though it records all of my trials (80 in total) sometimes trigger 6 for example changes to 224 or trigger 4 changes to 192. what can i do? Thanks

martinsteph avatar Sep 15 '21 13:09 martinsteph

you should ask in forums for help.

your code is bad. i tried this: grafik

get this error. port.write(trigger) NameError: name 'trigger' is not defined

harryberlin avatar Sep 15 '21 15:09 harryberlin

you need the trigger cable from BioSemi to run the code. It works for me with the USB trigger cable, but still, i have the same error, different trigger number

martinsteph avatar Sep 15 '21 16:09 martinsteph

don't have a biosemi trigger cable. what's that? i think it's not a pyserial issue.

harryberlin avatar Sep 15 '21 16:09 harryberlin

its an EEG cable. its a pyserial issue.

martinsteph avatar Sep 15 '21 16:09 martinsteph

trigger is not defined! the code will not run. otherwise it's not your real code.

harryberlin avatar Sep 15 '21 16:09 harryberlin

the "trigger" is in a csv file. as I told you it works in my pc but sometimes triggers are not recorded properly

martinsteph avatar Sep 15 '21 16:09 martinsteph

there is no csv read, there is no serial read. trigger is not set. so it's not the real code. ---> nothing to solve

harryberlin avatar Sep 15 '21 17:09 harryberlin

do you know how to use psychopy? everything is already in the programme before doing the code. the csv file needs to be uploaded first at psychopy before doing the code. That is why it works on my pc. I have the CSV in my laptop, uploaded in psychopy and i just need to check the code.

martinsteph avatar Sep 15 '21 17:09 martinsteph

i'm using pycharm. you should add some print() to see what happens.

harryberlin avatar Sep 15 '21 17:09 harryberlin

is print() the same as write()? and where can i use print?

martinsteph avatar Sep 15 '21 17:09 martinsteph

no it's not the same

harryberlin avatar Sep 15 '21 17:09 harryberlin

can you recomend where I can use print() ? please

martinsteph avatar Sep 15 '21 18:09 martinsteph

you should ask a psychopy forum. print is a basic usual python thing. you say the issue is a pyserial topic, but you never tried to debug your program.

harryberlin avatar Sep 15 '21 18:09 harryberlin

thanks, I have ask. but they havent replied. I ask two months ago and still not answer

martinsteph avatar Sep 15 '21 18:09 martinsteph

the original code above sends numbers which is not really the idea for Serial.write() it expects bytes so depending on what you want to send it may be port.write('4') (the ASCII character which is 0x34) or port.write('\x04') (the byte value 4)

if the data on the serial line has occasional "bit errors" it can also be that the wiring is not optimal and that you have EMI. if this is the case, routing the cables differently or using shielded cables. or shorter cables may help.

zsquareplusc avatar Sep 30 '21 23:09 zsquareplusc

Hey i have tried both, and the dont work. this is the error i get File "C:\Program Files\PsychoPy_new\lib\site-packages\serial\serialutil.py", line 63, in to_bytes raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) TypeError: unicode strings are not supported, please encode to bytes: '\t' when i just have port.write(bytes(trigger_audio)) if trigger_audio == 9: port.write(9) port.write(str.encode(chr(trigger_audio))) it works ok, but still, sometimes the 9 changes to 130

martinsteph avatar Oct 01 '21 09:10 martinsteph