DLL-based devices


#1

Hi everyone. I am starting with Python For the Lab and I am dealing with a device that is controlled with a ddl library. It is very well documented but I am a bit lost on where to start to make it run in python.

Any suggestions?

Best,
Martin


#2

Martín!
First of all, welcome to Python For The Lab!

If you could share the documentation of the DLL, that would be of great help.
Anyhow, you have two examples working with DLL’s of cameras, here for a Photonics Science, here for a Hamamatsu. Remember that when dealing with libraries, Windows and Linux behave differently. Since you said DLL, I assume you are on Windows. The code below would help you kick-start:

First, import

import ctypes

Then, you need to load the library, you have some options; each block shows some of them, so you have to try to see what works out for you:

libHandle = ctypes.LoadLibrary('path/to/dll')
libHandle = ctypes.windll.kernel32.LoadLibrary('ṕath/to/dll)

and then actually load the DLL:

dll = ctypes.CDLL(None, libHandle)
dll = ctypes.WinDLL(None, libHandle)  

Or, the standard procedure, without the Library Handle:

dll = ctypes.CDLL('/path/to/dll')
dll = ctypes.WinDLL('/path/to/dll')

Once you have the dll loaded, you can use the functions as:

dll.DeviceFunction(arguments)

Since dll are normally C programs, a lot of the behavior is very different than what you would find in Python programs. For example, some functions take pointers as arguments, and not variables. The output of some functions can also be stored in arguments, and not necessarily as the return. To achieve that, you would have to define the variable with an explicit C-type and pass it with a byref argument. Imagine you want to measure a temperature:

temp = ctypes.c_int32(0)
dll.MeasureTemp(ctypes.byref(temp))
print(temp.value)

With this, you can already start understanding how to use the DLL. More info in the official Python documentation.


#3

I’ve found a better example of working with the DLL’s from Thorlabs, in the SmartScan program.