Sending triggers via serial port
Hi!
I'm currently working on an experiment with BioSemi EEG and I'm trying to send triggers via serial port, without success.
I've tried the following code to open connection:
import serial
exp.serial_port = serial.Serial('COM3', baudrate=115200, bytesize=serial.EIGHTBITS)
And I've tried the following code to send the trigger:
exp.serial_port.write(chr(1))
However, when I run the experiment, a error appeears:
unicode strings are not supported, please encode to bytes: '\x01'
I'm not sure what to do in order to send the triggers successfully. Can you help me please?
Thanks!
Ana
Comments
Hi @anabatista ,
The serial port expects a
bytesobject, which is a more primitive form of text than thestrobject that you're sending now. You can convert the trigger to abyteslike this:# Initialize the serial port import serial serial_port = serial.Serial('COM3', baudrate=115200, bytesize=serial.EIGHTBITS) # And send trigger code 1 trigger_code = 1 trigger_byte = bytes(chr(trigger_code), 'utf-8') serial_port.write(trigger_byte)Also note that you don't need to assign
serial_porttoexp.— Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan!
It works perfectly now! Thanks!