psychopy.org | Reference | Downloads | Github

Triggering from Serial port

Dear PhycopPy developers,
I need to adapt a script to trigger from a Serial Port. Could you provide a demo or some test script so I can understand the syntax and how the trigger data is retrieved?
Thank you very much in advance.
Best wishes,
Catarina

Hello,

General purpose communication with serial devices using Python is usually done with the PySerial library.

https://pythonhosted.org/pyserial/

Something like this will write a byte to the port and read the response.

import serial

def main():
    # open the port
    port = serial.Serial("COM1", 9600)

    port.write('a')
    port.flush()

    while not port.inWaiting():
        print(port.readline())

    port.close()
    return 0

if __name__ == "__main__":
    main()

Dear mdc,

Thank you for the response. I have trial-ed this out on our scanner (also changed the port to COM3 which is the active port) and unfortunately I didn’t get any print of an ‘a’. I was then made aware that I should be using the serial port option anyway.

In alternative we have a National Instruments Card that is connected to the stimulus computer. I have also installed the drivers for this (https://pypi.python.org/pypi/PyDAQmx) but how can I now communicate with my stimulus psychopy code?

Thank you very much in advance.
Best wishes,
Catarina

Hello,

The code I provided is an example. You need to replace ‘a’ with whatever bytes the device is expecting.

I’m not familiar with PyDAQmx, but you should be able to call the API within your experiment’s main loop as you would any other Python module. The authors of PyDAQmx provide documentation here: http://pythonhosted.org/PyDAQmx/usage.html