0
votes

I'm trying to use a sensor by Senis through my own Python code by using ctypes with the company's provided dll.

The manual descirbes the function declaration for getting the sensor measurement as follows:

typedef struct {
int dimSize;
int elt[x];
} TD2;
typedef TD2 **TD2Hdl;

int get_sensor_values (int* device_number, unsigned long* timestamp, TD2Hdl values )

I have tried to use this dll declaration with my own code:

from ctypes import *

class TD2Hdl(Structure):
    _fields_ = [("dimSize", c_int),
                ("elt", POINTER(c_int))]
    
if __name__ == '__main__':
    
    senDLL = cdll.LoadLibrary("3mtslib.dll")
    
    num = c_int(0)
    ts = c_ulong(0)
    td = TD2Hdl(0, None)

    senDLL.open_device(byref(c_int(0)))
    senDLL.get_sensor_values(byref(c_int(num)), byref(ts), td)

I have also tried allocating different sizes of int arrays for the 'elt' variable through such instances as

td = TD2Hdl(0, (c_int*8)(*arr))

With different array sizes. However, I get an access violation reading with anything I try.

I will highly appreciate any help in understanding what I'm missing with how to allocate memory for structs passed to ctypes functions.

I validated the function name with dumpbin and to be clear, the rest of the functions work well.

Thank you!

1

1 Answers

0
votes

That's a strange typedef for TD2Hdl, but assuming it is due to the C code returning an allocated structure, the following demonstrates working Python code. I've made a dummy DLL to allocate and populate the structure for demo purposes.

test.c

#include <stdlib.h>

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

typedef struct {
int dimSize;
int elt[1];  // 'x' wasn't legal, so assume size 1 and allocate more memory as needed.
} TD2;
typedef TD2 **TD2Hdl;

API void open_device(int* device_number) {
    *device_number = 5;
}

API int get_sensor_values(int* device_number, unsigned long* timestamp, TD2Hdl values) {
    int dimSize = 10;     // could be any size
    *device_number += 1;  // just to prove the pointer content can change
    *timestamp = 123;
    *values = malloc(sizeof(TD2) + (dimSize - 1) * sizeof(int));
    (*values)->dimSize = dimSize;
    for(int i = 0; i < dimSize; ++i)
        (*values)->elt[i] = i;
    return 1;
}

test.py

from ctypes import *

class TD2(Structure):
    _fields_ = (('dimSize', c_int),
                ('elt', c_int * 1))  # correct way to make an array

    def __repr__(self): # make class be able to display itself
        return f'TD2(dimSize={self.dimSize})'

dll = CDLL('./test')

# Match arguments and return value to C types.  Note the **TD2 equivalent
dll.open_device.argtypes = POINTER(c_int),
dll.open_device.restype = None
dll.get_sensor_values.argtypes = POINTER(c_int), POINTER(c_ulong), POINTER(POINTER(TD2))
dll.get_sensor_values.restype = c_int

n = c_int()
dll.open_device(byref(n)) 
print(f'from open: n={n.value}')

ts = c_ulong()
td2 = POINTER(TD2)() # storage for the allocated pointer
ret = dll.get_sensor_values(byref(n), byref(ts), byref(td2))
print(f'from get: n={n.value} ts={ts.value} {td2.contents}')

# This is easier to do in C by indexing off the end of the array, but
# Python doesn't allow that, so to safely read the array in Python the
# address of the 'elt' can be cast to a pointer to an array of the
# correct size, then dereferenced and converted to a Python list
# to see the values.
data = list(cast(byref(td2.contents.elt), POINTER(c_int * td2.contents.dimSize)).contents)
print(data)

Output:

from open: n=5
from get: n=6 ts=123 TD2(dimSize=10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]