| Reference | Downloads | Github

Reading from parallel port

I recently started using PsychoPy (on Ubuntu 18.04) and have some trouble getting access to the parallel port. I try to use the parallel functions to read data from a button box. I have a built-in parallel port and its address is /dev/parport0.

This is what I am trying to do:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from psychopy import parallel, core

p = parallel.ParallelPort(address = '/dev/parport0')
timer = core.CountdownTimer(5)
while timer.getTime() > 0:
    input = p.readData()
    if (input)>0:

I don’t get any error messages, it just receives only zeros in variable input.
Before I had some issues opening the port but as I am now in the lp group which can access the parallel ports without root, I have no more error messages. But still I cannot detect anything. The button boxes are working, as I can detect the presses in Presentation using Windows.
I have tried PsychoPy versions 2.8 and 3.0 - no difference.
Maybe there is something obvious I am missing? Or has anyone some experience with accessing parallel ports?
I would be so glad!
Thank you in advance!


I just figured it out, so just in case anyone wants to do something similar:

For writing data it was fine to use the default class the psychopy version of parallel brings (from psychopy import parallel).
For reading from my particular device I used the original parallel class instead: (import parallel). Then there are several other functions for the object, especially the PPDATADIR(out) which can reverse the direction of the port. Having the out parameter set to 0, turns off the driver so that the peripheral device drives the signal. Then it was possible to read data using PPRDATA() function or convert this to single pin data using p.PPRDATA() >> (pinNumber - 2)) + 1 as it can be seen in the psychopy version of parallel.

Complete code looked like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import parallel
pinNumber = 2 # one data pin the device uses
p = parallel.Parallel() # with default port = 0, you can also give another port as argument
p.PPDATADIR(0) # sets port to reverse mode (default out = 1)
p.PPRDATA() >> (pinNumber - 2)) + 1 #read single pin data

Of course again I implemented something related to how often it should be checked and then saved it somewhere, but just to show what worked for me.

Maybe there is an easier way to do this in psychopy but at least it worked :slight_smile:

1 Like

Glad you got something working!