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

Hi, I hope you can help,
i have a similar problem that I haven’t solve yet. i have 3 different triggers, but when I write the code sometimes trigger 3 changes to 224 or trigger 2 to 192.
it records all the 80 trials, but with this different numbers. What can I do?
this is the code
begin experiment
import serial
port = serial.Serial(port = ‘COM4’, baudrate = 115200)

Begin routine
port.write(trigger)
if trigger ==1:
port.write(1)
elif trigger ==2:
port.write(2)
elif trigger ==3:
port.write(3)
port.write(str.encode(chr(trigger)))

end experiment
port.close()