#### Howdy, Stranger!

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

Supported by

# [solved] Serial port trigger for EEG??

edited February 2014

Hello,

I would like to present pictures on a computer screen and get a trigger for each of my pictures on an EEG signal. However the computer I have does not have a parallel port but only a serial port. Is it possible to use OpenSesame eventhough?? If yes, how can I instal the serial port trigger plug in?
Regards

• edited 9:00AM
• edited 9:00AM

NB/ I have found this discussion http://forum.cogsci.nl/index.php?p=/discussion/496/open-sending-triggers-for-eeg-through-serial-port/p1 but I don't know how to write a script as I am using the "graphical interface" of OpenSesame... Can anybody help me pleeeease? :-)

• edited January 2014

Hi,

I'm afraid that up until now, there's no plug-in for sending triggers via the serial port yet, which means that you will not be able to do this solely via the graphical interface (GUI). This needn't be a big problem, though, as the little bits of scripting that you need you can actually do via the GUI.

You will need to do as follows:

1 Place an item called inline_script at the very start of your experiment. Please rename it to 'serial_setup'.

2 Copy the following code, and paste it in the Run Phase of the inline script that you've just created. Please do note to check what the address of your serial port is (usually it's 'COM1', but it could be e.g. 'COM2' or 'COM3' too).

# we will need to import the 'serial' module,
# which allows us to send and receive
# information via the serial port
import serial

# next, we will need to start a connection to
# the serial port
exp.trigger = serial.Serial('COM1')

# finally, we want to make sure that the
# connection to the serial port will be stopped
# when the experiment is aborted or finished
exp.cleanup_functions.append(exp.trigger.close)


3 Place another inline_script item at every moment that you want to send a trigger. In the Run Phase of this inline_script, paste the following code:

# this sends a trigger with value 1
# if you require a different value, please
# use a number between 0 and 255
exp.trigger.write(chr(1))


Alternatively, if you have defined the values of your triggers in a loop, let's say in the variable triggervalue, use the following script:

# first, get the trigger value
value = self.get("triggervalue")

# now, send a trigger with that value
exp.trigger.write(chr(value))


Good luck!

Edwin

• edited 9:00AM

UPDATE! If you do want to do things via the GUI, you could try using the plug-in that I've just put together. It hasn't been tested, as I do not have a PC with a serial port nearby, but it is very straightforward and should work.

Good luck!

• edited January 2014

Hello,
First of all thank you very much Skinnerwinner and Edwin for your answers!! >-

Edwin, thank you I have dowloaded the serial port without any problems but as I set the port adress to COM1 which is the port of my PC I had this error message:

Expyriment 0.6.4-opensesame-1 (Revision ff39d2489a7e; Python 2.7.5)
openexp.sampler._legacy.init_sound(): sampling freq = 48000, buffer size = 512
experiment.init_log(): using 'F:/subject-0.csv' as logfile (utf-8)
experiment.run(): experiment started at Tue Jan 14 10:14:53 2014
serial port 'COM1' initialized
serial port 'COM3' initialized
Traceback (most recent call last):
File "dist\libqtopensesame\qtopensesame.py", line 1393, in run_experiment
File "dist\libopensesame\experiment.py", line 287, in run
File "dist\libopensesame\sequence.py", line 55, in run
File "dist\libopensesame\loop.py", line 146, in run
File "dist\libopensesame\sequence.py", line 107, in prepare
File "C:\Program Files (x86)\OpenSesame\plugins\serial_port_trigger\serial_port_trigger.py", line 77, in prepare
File "dist\serial\serialwin32.py", line 30, in __init__
File "dist\serial\serialutil.py", line 260, in __init__
File "dist\serial\serialwin32.py", line 56, in open
SerialException: could not open port COM4: [Error 2] Le fichier spécifié est introuvable.
>>>


Do you have any idea what I did wrong.....?

Thank you!

Cheers ^:)^

• edited 9:00AM

Oh, sorry, silly mistake of me. It's autodetecting, but the autodetect code is flawed (runs even if you don't specify it). I'll fix it when I'm near a computer again. In the meantime you could opt for the inline scripting option I explain above.

• edited January 2014

Okay thank you very much! =D>

I have thus add the inline script (at the very start of my expe and in my loop) and I have defined a variable named "code" which will be the value of my trigg (one code for each picture I present on the screen) so I have write in the script in my loop this:

# first, get the trigger value
value = self.get("code")

# now, send a trigger with that value
exp.trigger.write(chr(value))


Could you please confirm me it is the way to do it (because I thought variable should be named with [...] but when I do this it does not work so is that correct...? :-S

and a last question (hope so...) I would like my picture to be presented for 400 ms. With the parallel port I had to set the value of my picture at 0 and my trigg to 400 ms. How is it for the serial port, do I have to add a line in the script to set the duration (if so, which one....? :-? ) or do I have to set my picture to 400 ms...??

• edited 9:00AM

Ah, yes, this is something that often produces a bit of confusion. The notation with square brackets, [variable] van be used within OpenSesame's interface, as well as in OpenSesame syntax (which you see when you press the 'Edit script' button of an item).

In an inline_script item, though, Python scripting is used. Python has a different syntax, so you will have to use a different approach to obtain a variable's value, i.e. self.get("variable").

What you could do with the timing, is setting the sketchpad's duration to 0, add the inline_script directly after, and then put in an advanced delay item to have the system pause for 400 ms.

Good luck!

• edited January 2014

Hello Edwin,

Here is what I have done: set my sketchpad's duration to 0, add an inline-script just after and have writen "self.sleep(400)". Is that the correct way to put the advanced delay you are talking about? (I remind you that I would like to present my picture for 400ms and send a trigger right after ).
Many thanks!

• edited 9:00AM

Hi Pénélope,

The advanced_delay plug-in is a plug-in item that you can drag from the item toolbar into your experiment. So it's not really what Edwin meant, but using a self.sleep() command is fine as well.

One thing, perhaps to state the obvious, make sure to send the trigger before doing the sleep, like so:

# first, get the trigger value
value = self.get("code")
# now, send a trigger with that value
exp.trigger.write(chr(value))
# sleepy time
self.sleep(400)


Cheers,
Sebastiaan

• edited February 2014

Sorry, sort of forgot this. Finally fixed the plug-in: https://github.com/esdalmaijer/opensesame_serial_port_trigger

• edited April 2017

Hi everyone,
I tried both methods described here.
For the plugin, if I want to drag the serial_port_trigger item to the correct place in my experiment I get an error message (which I send):
Traceback (most recent call last): File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libqtopensesame\widgets\tree_overview.py", line 753, in dropEvent self.drop_event_item_new(data, e) File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libqtopensesame\widgets\tree_overview.py", line 672, in drop_event_item_new item, new_items = self.drop_get_item_snippet(data) File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libqtopensesame\widgets\tree_overview.py", line 560, in drop_get_item_snippet catch_exceptions=False) File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libqtopensesame\misc\qtitem_store.py", line 105, in new script=script) File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libopensesame\item_store.py", line 164, in new u"Failed to load plugin '%s'" % _type, exception=e) osexception: Failed to load plugin 'serial_port_trigger' item-stack: exception message: 'unicode' object is not callable time: Wed Apr 26 09:10:48 2017 exception type: TypeError Traceback: File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libopensesame\item_store.py", line 161, in new self.experiment, script, self.experiment.item_prefix()) File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libopensesame\plugins.py", line 362, in load_plugin item = item_class(item_name, experiment, string) File "C:\Program Files (x86)\OpenSesame\share\opensesame_plugins\serial_port_trigger\serial_port_trigger.py", line 134, in init serial_port_trigger.init(self, name, experiment, string) File "C:\Program Files (x86)\OpenSesame\share\opensesame_plugins\serial_port_trigger\serial_port_trigger.py", line 53, in init item.item.init(self, name, experiment, string) File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libopensesame\item.py", line 67, in init self.from_string(string) File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libopensesame\item.py", line 285, in from_string self.reset() TypeError: 'unicode' object is not callable
Does anyone have an idea why this happens?

If I try the inline method I don't get an error message, so it may work, but I'm not sure. I opened notepad on the other computer (to which the trigger is send) and to my understanding (but maybe I'm false?) I should be able to see the triggers (1, 2, 3) in there if they are send. But I don't see any.

(I want to do eyetracking and send triggers for each trial, which then should show up in the eye-tracking logfile. However I can't check that currently, so therefore the notepad method... Because notepad should receive the triggers aswell, right?)

• Hi Pia,

This plugin is simply not compatible with OpenSesame 3. So, assuming that the plugin won't be updated, you have two options:

1. Use OpenSesame 2.9.7
2. Use the latest OpenSesame (now 3.1.6) and send triggers with a simple inline_script

I would go with option 2, because then you can make use of the latest OpenSesame; and using pyserial in an inline_script is not that difficult:

Cheers!
Sebastiaan

• edited April 2017

Hi Sebastiaan,
thank you!
I also tried to use an inline_script and it didn't give any error message but I can't see the triggers on the other computer. (I opend notepad to check if I receive the triggers, not sure that method works, but shouldn't I receive the triggers (which I send from the other computer where Open Sesame runs) in Notepad aswell?)
I used the inline_scripts by Edwin. I think that COM1 is the correct port.
(I want to do eyetracking and send triggers for each trial, which then should show up in the eye-tracking logfile. However I can't check that currently, so therefore the notepad method... Because notepad should receive the triggers aswell, right?)
Thanks,
Pia

• Hi Pia,

You'll have to be a bit more specific! I guess it's obvious (but just to be sure): If you start notepad, you just get an empty document. So I'm guessing you mean that you opened a specific file in notepad, and that you expected this file to contain triggers, but it didn't. Right?

So how exactly did you send the triggers, and what exactly did you expect to happen?

Also, how do you see the interaction between the eye tracker and EEG? Both devices keep their own logfile. Do you want to send the same trigger to both devices at the same time?

Cheers,
Sebastiaan

• edited April 2017

Hi Sebastiaan,

I have an eye-tracking experiment (no EEG-experiment, sorry for the confusion, I was just using this thread because I thought it makes no difference if I record EEG or eye-tracking on another computer) where I want to send triggers for the start of every trial.

So I copied the inline-script from Edwin. At the beginning of the experiment I have (copied from Edwin):
 import serial exp.trigger = serial.Serial('COM1') exp.cleanup_functions.append(exp.trigger.close)

And then, when I want to send the trigger I put:
exp.trigger.write(chr(1))

On the other computer (which will do the eye-tracking recording) I opend Notepad to see if I receive the trigger. I just opened an empty file to see if I receive the trigger (1, in this case). But I don't. Maybe this approach is false (but I did similarly in the past). If the eye-tracking program receives the trigger, notepad should be as well, or am I false? Unfortunately I can't check with the eye-tracking program at the moment, I therefore just wanted to make sure the other computer does indeed receive the trigger.

• Hi,
I now included a suggestion you gave elsewhere to write something to the debug window to test if the trigger had been send with the print statement just after exp.trigger.write(chr(1))
And it does show up there! (In the debug window, what I printed.) So I assume that the trigger is indeed send but I can't see it on the other computer yet.

• edited April 2017

Hi Pia,

Let's first take a step back.

A trigger is just a general term, which usually refers to a byte of information that is sent to an EEG recording device. (Although there's nothing wrong with using the term for other kinds of devices.) But triggers can be sent in many ways: through the parallel port, through the serial port, through a network, or through some kind of custom communication protocol.

So you first need to find out how you can connect with your eye tracker. What kind of eye tracker are you using? What does your eye tracking set up look like?

Cheers,
Sebastiaan

PS. I just closed two discussion that you opened about this general issue. For future reference: Please stick to one discussion for one question!