Only one serial component per experiment?

If this template helps then use it. If not then just delete and start from scratch.

OS Win10
PsychoPy 2022.2.4

What are you trying to achieve?:
Using multiple serial components in a single experiment (single routine even) to create multiple event markers at different times within a trial.

What did you try to make it work?:
Adding multiple serial components and setting them accordingly.

What specifically went wrong when you tried that?:

    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM5': PermissionError(13, 'Access is denied.', None, 5)
################# Experiment ended with exit code 1 [pid:7512] #################

From the access denied error above, it appears that each serial component is trying to access the same COM port but separately, so the second one crashes and causes the error. Is this a known issue with the serial component? Or have I just done something entirely wrong? (Hopefully the latter.)

Also… Setting Parity to None as I would when using a Code component for serial communication also crashes the experiment and I can only get it working when setting Parity to Off. Is this expected?

The :serial: Serial component just creates a serial.Serial object, which means you can only link one to a device at a time. Could you share the experiment you’re trying to do this with?

We could potentially add some code to Builder such that the serial.Serial object is only created once even with multiple components, and the other components become essentially just different names for the same object. To do this (and test that it works!) it would be helpful to have a real-world example of an experiment using multiple :serial: Serial components.

Many thanks for the explanation of how the Serial component works.

I’ll happily provide a .psyexp example when I’m back in the lab, but for now, the main usage would be to have multiple components within a routine, each sending different triggers/bytes at different defined times e.g., event 1 or 2 at the start of the routine to define condition, another event to define when a stimulus starts/ends (e.g., jittered 100ms after start of routine), and, in this case, another to trigger a TMS system at a specific post-stimulus time (2,3,4 or 5 seconds after stimulus starts).

Hi there,

I am currently producing an experiment for my masters that also requires more than one Serial component for adding multiple triggers to my EEG recording. I am using version 2022.2.5 of psychopy. Is it possible to only create the serial.Serial object once and then add more Serial components to label more triggers both within a routine and across routines (e.g. one routine is for a fixation cross that I would like to trigger and then another routine I want to trigger when stimulation occurs)? If this is possible, how can it be achieved?

@Becca do you have any knowledge surrounding this?

I really hope to hear back from you as I am rapidly running out of time to get my experiment working and any assistance would be greatly appreciated :slight_smile:

Your best bet is likely to use a Code component with your conditional and event-sending code on the Each Frame tab according to each event you want to send (and record with your EEG). For example, for an event to be sent 500ms after the Routine starts…

if t >= 500 :
    win.callOnFlip(serialPort.write,eventMarker)

Where serialPort is the name of your serial port object and eventMarker is a variable for the event marker/code you want to send (formatted appropriately according to your serial device and EEG hardware/software). For instance, eventMarker might need to be in hex format e.g., ‘\x01’ or a string-formatted number (using bytes() or .encode()).

To give an update on this, as of 2023.1.0 creating multiple :serial: Serial components only creates one serial.Serial object per port :slight_smile:

The object name is serial_ followed by the port number (e.g. serial_123445), but at the start of the experiment it is also stored by the variable name of your Component so you can refer to it in :code: Code components that way.