pyserial
pyserial copied to clipboard
Sending triggers to psychopy through EEG BioSemi serial port
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
you should ask in forums for help.
your code is bad. i tried this:
get this error.
port.write(trigger)
NameError: name 'trigger' is not defined
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
don't have a biosemi trigger cable. what's that? i think it's not a pyserial issue.
its an EEG cable. its a pyserial issue.
trigger is not defined! the code will not run. otherwise it's not your real code.
the "trigger" is in a csv file. as I told you it works in my pc but sometimes triggers are not recorded properly
there is no csv read, there is no serial read. trigger is not set. so it's not the real code. ---> nothing to solve
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.
i'm using pycharm. you should add some print() to see what happens.
is print() the same as write()? and where can i use print?
no it's not the same
can you recomend where I can use print() ? please
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.
thanks, I have ask. but they havent replied. I ask two months ago and still not answer
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.
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