psychopy.org | Reference | Downloads | Github

Using TCP or UDP to control PsychoPy experiments

Hi is there some sample code describing the use of TCP or UDP in controlling as well as reading data from PsychoPy?.

I’m new to PsychoPy. Just migrating to it after > 15 years of running visual behavior + physiology experiments using a (home-brew) C++ program that monitored behavior (eye position etc) presented visual stimuli (using a Cambridge Systems board) and triggered imaging (intrinsic signal optical imaging). I would like to still use my old software for behavior, timing etc while presenting stimuli and running some routines (e.g. staircase to get thresholds). The easiest would be to communicate between the C++ machine and PsychoPy (on a Linux machine) via TCP or UDP, which could trigger either low level (‘display grating’) or high level (‘run staircase’) routines on PsychoPy and get some communication back from PsychoPy

So my question:
1: Are there examples I can use to quickly build up code (using the Coder)?
2: Is there any preference for TCP vs UDP

Thanks!
Aniruddha

There is nothing specific to PsychoPy here: just look at a general guide to programming sockets in Python, e.g.

If you have done this before in C++, I imagine the principles will all seem very familiar. It only takes a few lines to create a socket and send data on it, the only tricky thing sometimes is remembering to turn strings into bytes:

That is usually determined by the demands of what is expected at the receiver end. But I’d say if you have a choice, go with UDP. It is easier to program: you don’t need to maintain a connection, all you need to send to a target is its IP address and the port number it is listening on. Conventional wisdom says UDP packets aren’t as reliable as communicating via TCP, as receipt isn’t guaranteed (UDP is designed for streaming applications like audio and video, where if a pocket is dropped, it just gets ignored. TCP is more complex, and its meta data means packets can be reconstructed into the correct sequence if they are received out of order). But in a lab environment, where you can ensure there isn’t much competing traffic on the network, UDP packets will be received, and promptly. And you are just generally sending small commands or data values, and not constructing a large data structure, which is what TCP is best for.
I’ve found UDP to be very reliable in controlling an eye tracker, for example.

Great, thanks very much. That’s really helpful.

Aniruddha