Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

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 bytes object, which is a more primitive form of text than the str object that you're sending now. You can convert the trigger to a bytes like 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_port to exp .

    — Sebastiaan

  • Hi Sebastiaan!

    It works perfectly now! Thanks!

Sign In or Register to comment.