Psychopy drive the hardware

Hello @mdc ,
I saw you provide some answers about hardware connection. I have a further question about it. Could you give me some guidances?

I’m trying to make a task by using Psychopy to trigger the operant box to deliver sugar once the touch-screen is touched. The company where we bought the operant box offered a DLL file which provides the user with the ability to talk to the driver of the hardware by using the Portread and Portwrite functions. For C++ users the header file IO.h and library IO.lib is also provided.

Since I’m quite new here, my question maybe too silly.
Question: How does the psychopy call the Portwrite function to make a task that if the animal touch the screen then a port can be turned on to deliver a sugar?

best wishes,
Chao

Hello,

If a header is provided, you can use the ‘ctypes’ library to interface with the DLL from Python. The documentation can be found here:

https://docs.python.org/3/library/ctypes.html

You need to write a function that calls portwrite using ctypes within your experiment code.

Hi @mdc,

I checked the documentation of ctypes. But I still didn’t figure out how it should work. Is it possible to provide me a simple code to try the function in psychopy? In the header file, it says:
extern “C” short WINAPI EXPORT PortRead(short rack, short port, short offset);
extern “C” void WINAPI EXPORT PortWrite(short rack, short port, short offset, short value);

best,
Chao

I don’t have anyway of testing anything out, however, you might do something like this:

from ctypes import *
iolib = CDLL("IO.dll")  # path to IO.DLL

# extern “C” short WINAPI EXPORT PortRead(short rack, short port, short offset);
PortRead = iolib.PortRead
PortRead.restype = c_short
PortRead.argtypes = [c_short, c_short, c_short]

# extern “C” void WINAPI EXPORT PortWrite(short rack, short port, short offset, short value);
PortWrite= iolib.PortWrite
# PortWrite.restype = None
PortWrite.argtypes = [c_short, c_short, c_short, c_short]

# call the function using whatever values you need for rack, port, offset, and value.
result = PortRead(rack, port, offset)
PortWrite(rack, port, offset, value)

Thank you! it works now.