Run c++ function from python

91 views Asked by At

I am trying to access GPIO of my IDS camera via python using pyueye. The original function is defined as: INT is_IO(HIDS hCam, UINT nCommand, void* pParam, UINT cbSizeOfParam). This is an example of usage:

Example 2

INT nRet = IS_SUCCESS;

 

IO_GPIO_CONFIGURATION gpioConfiguration;

 

// Set configuration of GPIO1 (OUTPUT LOW)

gpioConfiguration.u32Gpio = IO_GPIO_1;

gpioConfiguration.u32Configuration = IS_GPIO_OUTPUT;

gpioConfiguration.u32State = 0;

 

nRet = is_IO(hCam, IS_IO_CMD_GPIOS_SET_CONFIGURATION, (void*)&gpioConfiguration,

            sizeof(gpioConfiguration));

I'm trying to do it in python as follows:

from pyueye import ueye
from ctypes import pointer
gpioConfiguration = ueye.IO_GPIO_CONFIGURATION 
gpioConfiguration.u32Gpio = ueye.IO_GPIO_1
gpioConfiguration.u32Configuration = ueye.IS_GPIO_OUTPUT
gpioConfiguration.u32State = 1

pt = pointer(gpioConfiguration)
stat = ueye.is_IO(hCam, ueye.IS_IO_CMD_GPIOS_SET_CONFIGURATION,byref(gpioConfiguration),ueye.sizeof(gpioConfiguration))

But I get the error: TypeError: type must have storage info. Any ideas on what I need to change?

1

There are 1 answers

6
2e0byo On BEST ANSWER

The error is actually being thrown by your call to ctypes.pointer rather than pyueye. Specifically, gpioConfiguration is a _ctypes.PyCStructType rather than an instance. (This is what was meant by 'must have storage', i.e. you actually have to store something looking like that struct before you can get a pointer to it. This is likely a bit odd coming from c++, but ctypes thinks of structs as being like Classes you have to instantise. docs.

So if you really needed a pointer you would have to do:

myConfig = gpioConfiguration()
pt = pointer(myConfig)
# you probably want to set properties on the *instance*:
myConfig.u32Gpio = ueue.IO_GPIO_1
myConfig.u32State = 1
# and then use this instance later when passing `byref`

If you just need to pass these params by reference, see the section on byref (though this too needs an instance).

I don't have a camera to test with sadly (they look very good fun), so this is far as I can go. But hopefully you can get the semantics of ueye behaving for you.

Incidentally, you don't need semicolons in python (unlike in JS you really don't and nobody thinks you should put them in)