0
votes

I am trying to get information from a sensor through its DLL with Python.
I never worked with ctypes or C in general before so I lack some knowlege about this, hopefully somebody can help me.
Calling the functions work so far but I cannot get the functions to give back string information. What works so far:

from ctypes import *

libc = windll.LoadLibrary("MEDAQLib.dll")

version = create_string_buffer(15)
libc.GetDLLVersion(version, 15)
print(version.value.decode())

This returns the version number of the DLL and works correctly.
Now I try to get some information about the sensor in the same way.

# Create sensor instance and open connection
sensor = libc.CreateSensorInstance(42)
libc.OpenSensorRS232(sensor, b'COM4')

# Try to get information from sensor
name = create_string_buffer(32)
libc.ExecSCmdGetString(sensor, b'Get_Info', b'SA_Sensor', name, 32)
print(name.value.decode())

The ExecSCmdGetString function failes with OSError: exception: access violation reading 0x0000000000000020

Declaration for GetDLLVersion:
ERR_CODE GetDLLVersion (char *versionStr, uint32_t maxLen);
Parameter:
uint32_t maxLen
Description:
Length of string buffer. If the version info is longer as maxLen it is truncated. The string is null terminated.

Declaration for ExecSCmdGetString:
ERR_CODE ExecSCmdGetString (uint32_t instanceHandle, const char *sensorCommand, const char *paramName, char *paramValue, uint32_t *maxLen);
Parameter:
uint32_t * maxLen
Description:
The buffer must be allocated by the application. The size of the buffer is specified at maxLen. If the resulting string is larger than maxLen, it is truncated and ERR_NOMEMORY (-19) is returned. The real length of the string (maybe truncated) is returned in maxLen too. If paramValue is nullptr, the length of the containing string is returned in maxLen.

So obviously I cannot just specify the maxLen as an integer anymore.
But how do I have to do it then?

I also tried to just make a pointer:

name = c_char_p()
libc.ExecSCmdGetString(sensor, b'Get_Info', b'SA_Sensor', byref(name))

But then how do I specify maxLen?

1

1 Answers

0
votes

Like this:

paramValue = create_string_buffer(32)
maxLen = c_uint32(32)
errCode = libc.ExecSCmdGetString(sensor, b'Get_Info', b'SA_Sensor', paramValue, byref(maxLen))

Your first attempt didn't work because maxLen is a uint32_t *, but you just passed it a uint32_t. Wrapping it in byref() fixes this.