Get response via serial port

OS (e.g. Win10): Win11
PsychoPy version (e.g. 2024.2.4 Py 3.8): 2026.1.0.
Standard Standalone Installation? (y/n) : y
Do you want it to also run online? (y/n) n
What are you trying to achieve?:

I collect keypresses from an optoelectronic device used in an MEG scanner. Experiments in the lab are usually run with the help of the PsychoPhysics Toolbox. However, my experiment was programmed in PsychoPy and I do not want to redo it in the PsychoPhysics Toolbox.

The optoelectric keyboard is connected to the host PC via the serial port. This is where the problems begin. The experiment does not run, no matter how I try to retrieve data from the serial device. Most of the time, the program crashes because it cannot find the port, does not have permission to read from it (which is strange), or the serialPort.readline() command is not recognised.

What did you try to make it work?:

I followed the instructions as described Reading from serial port(receiving triggers from MRI device) - #2 by Becca or Communicating with fMRI — PsychoPy®.

Does anyone have a working example that I could use, or could someone give me some advice on how to set up and read from the serial port?

Best wishes Jens

I haven’t seen an actual physical serial port in quite a few years, if that’s what you mean, but we’ve used serial-over-USB devices quite a lot with no problems.

The only issues I’ve ever encountered were:

  • getting the drivers installed (e.g. the FTDI drivers run most serial-USB chips) but you’ve presumably done that, or don’t need it if this is a physical serial port
  • setting up the correct communication protocol (the baudrate, the number of bits, the stop-bit settings etc.) but hopefully you’ve got all those already if it’s working on PTB

The only permissions problem I can imagine is if another process already holds the device open and hasn’t shut down. You definitely can’t have 2 processes using the port at the same time.

And readline() not being recognised makes no sense at all, unless you’ve got a name conflict (like you’ve given the device the same name as the library). Could you send the full traceback?

best wishes,
Jon