Howdy, Stranger!

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

Supported by

[solved] Sending different triggers via serial port

edited July 2013 in OpenSesame

Dear community,

thanks again for helping me with my other problem. I now want to send two different triggers via serial port to our EEG. I therefore prepared two inline scripts:

exp.serial_port.write(chr(1))

and

exp.serial_port.write(chr(2))

This should send two different triggers to the EEG, right? Unfortunately neither BioTrace (our recording software) nor Brain Vision Analyzer (for analysis) show two different triggers. I'm not sure whether the python code is wrong or our software is unable to recognize different triggers.

Best wishes,

Daniel

Comments

  • edited 10:40AM

    Hi Daniel,

    This should send two different triggers to the EEG, right?

    Indeed it should.

    I'm not sure whether the python code is wrong or our software is unable to recognize different triggers.

    The Python code is correct, but there could be a lot of different reasons for why you don't see the trigger in the BioTrace. The most obvious one is that the code, although valid, is not actually executed. You can find this out by printing something to the debug window after sending the trigger:

    exp.serial_port.write(chr(2))
    print 'I just sent trigger 2! (or did I ... ?)'
    

    Another possibility is that you are sending triggers to the wrong port. When setting up a connection, you have to specify whether you want to use COM1, COM2, etc. If you choose the incorrect port, your triggers will go nowhere.

    Could it be either of those two things?

    Cheers,
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited June 2013

    A rather nasty error we've encountered in our labs, is that sometimes triggers are sent, but not recognized by the system you're sending them to. In our case, we tried sending parallel port triggers to a BioPac system. The system did not seem to respond. After measuring the parallel port voltage directly from the pins, we discovered that a signal was being transferred. Also, the BioPac did respond to the same type of triggers in a different setup. We concluded that the parallel port output of our computer was simply too weak to be recognized as a trigger by the BioPac (< 3.5 V).

    I have to admit that serial ports are a somewhat different system, but it still might be a good idea to manually check (using a voltmeter on the pins of your cable) whether your computer might actually be giving an output, when you can't find any problems in your code.

  • edited 10:40AM

    Thanks for your replies! I forgot to mention (shame on me) that I can see the triggers in both programes. So sorry for sending you into the wrong direction :-(

    The actual problem is that the programes can't differentiate between trigger 1 and trigger 2. According to sebastiaan my code is right, so probably BioTrace is just unable to recognize two different triggers.

    Instead of

    Trigger 1
    

    or

    Trigger 2
    

    it shows

    Trigger
    

    I'll have a chat about this with the BioTrace developers. Maybe they have a solution.

  • edited 10:40AM

    Today I managed to send several different triggers to our EEG by using the port output test of Presentation (trigger 2, trigger 3...):

    image

    however, with OpenSesame different triggers aren't recognized (trigger 0, trigger 0...):

    image

    Do you guys have any idea how I can fix it?

    Best wishes,
    Daniel

  • edited June 2013

    Hi Daniel,

    The following code ...

    # Open connection
    import serial
    exp.serial_port = serial.Serial('COM1') # Can also be COM2, COM3, etc.
    # Send byte
    exp.serial_port.write(chr(1)) # Send the byte 1 as a character
    # Close connection
    exp.serial_port.close()
    

    ... will send byte 1 via the serial port to COM1. pyserial expects a character, which is why you need to send chr(1) rather than simply 1. But that's not important: Characters and bytes are equivalent, just different interpretations of the same 8 bits.

    Since this code snippet is pretty straight-forward and serial port communication has been extensively tested and used, I think the source of the problem might be that the BioTrace simply expects something different than what you are sending.

    For example, the constructor (the serial.Serial() part) takes many optional parameters, such as the baudrate, etc. If the BioTrace expects a specific configuration, this is a property of the system that you simply have to know.

    Similarly, the BioTrace might expect a specific type of data, and not just a byte-value. Again, this is a property of the system that you have to know.

    I would first check the Presentation settings to see whether and how they have specified properties of the connection: What is the baudrate, etc.? If you can't figure it out, I would ask the BioTrace developers for an example Python snippet.

    Cheers,
    Sebastiaan

    PS. A quick test would be to send all possible values and see how the BioTrace responds:

    for i in range(256):
        exp.serial_port.write(chr(i))
        self.sleep(200)
    

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 10:40AM

    Hi!

    Thanks again for your reply. The required values are (according to the Nexus Trigger Interface user manual):

    Data bits: 8
    Start bits: 1
    Stop bits: 1
    Baudrate: 115200
    Parity: none
    Flow control: none
    

    Where do I have to insert these values? Sorry to ask but I'm really not a professional python programmer ;-)

    Thanks for your help!

    Daniel

  • edited 10:40AM

    You can pass these as keywords to the constructor, like so:

    import serial
    serial_port = serial.Serial('COM1', baudrate=115200, bytesize=serial.EIGHTBITS)
    

    baudrate is a keyword, and you can find a full list of keywords here:

    Some things may not be entirely clear, so you may have to try out a few settings. Also, it's probably a good idea to also familiarize yourself a bit with Python:

    Cheers!

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 10:40AM

    Hi Sebastiaan,

    I added a small modification and it works just perfect now:

    import serial
    exp.serial_port = serial.Serial('COM1', baudrate=115200, bytesize=serial.EIGHTBITS)
    

    Again: many thanks to you!

  • edited 10:40AM

    Great! Glad to hear you got it resolved.

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

Sign In or Register to comment.