NIR SDK Secondary Development Guide on Raspberry Pi (Linux arm64)
SummaryHow to build and call the NIR spectrometer SDK on a Raspberry Pi (Linux arm64) with Python, including the wrapper.py functions, installation steps and a complete example.
NIR SDK Secondary Development Guide on Raspberry Pi (Linux arm64)
Overview
This article describes how to read wavelength and intensity data from a NIR spectrometer using Python on a Raspberry Pi (arm64 architecture, Ubuntu 64-bit). The SDK is provided as a C dynamic library libwrapper.so, along with a Python extension package wrapper.py; users only need to import the module to quickly complete data acquisition.
1. Basic Functions of wrapper.py
wrapper.py has encapsulated four core functions; its source loads the dynamic library via ctypes and defines the function prototypes:
import ctypes
# Load the shared library
libwrapper = ctypes.CDLL("lib/libwrapper.so")
# Define function prototypes
libwrapper.dlpConnect.argtypes = []
libwrapper.dlpConnect.restype = ctypes.c_int
libwrapper.dlpOpenByUsb.argtypes = [ctypes.c_int]
libwrapper.dlpOpenByUsb.restype = ctypes.c_int
libwrapper.dlpGetWavelengths.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.c_int]
libwrapper.dlpGetWavelengths.restype = ctypes.c_int
libwrapper.dlpGetIntensities.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.c_int]
libwrapper.dlpGetIntensities.restype = ctypes.c_int
# Wrap as Python functions
def dlpConnect():
'''Connect to the device and return the number of connected devices.'''
return libwrapper.dlpConnect()
def dlpOpenByUsb(index):
'''Open the device at the specified index via USB.'''
def dlpGetWavelengths(wls, wlsNum):
'''Get wavelength values into the wls buffer.'''
def dlpGetIntensities(activeIndex, intensities, wlsNum):
'''Get intensity values into the intensities buffer.'''
Brief function descriptions:
| Function | Purpose | Return value |
|---|---|---|
dlpConnect() |
Enumerate connected devices | Number of devices |
dlpOpenByUsb(index) |
Open the device at the specified index | >=0 success, <0 failure |
dlpGetWavelengths(wls, num) |
Read 228 wavelength points | >=0 success |
dlpGetIntensities(idx, buf, num) |
Read 228 intensity values | >=0 success |
2. Module Installation
Users will receive the following files:
wrapper.py— the wrapped Python modulesetup.py— the extension installation scriptlib/libwrapper.so— the C dynamic librarymain.py— the calling example
Installation Steps
Create a folder python_demo_v1.1, copy the four files above into it, and set read/write permissions:
# Build the extension package
python3 setup.py build
# Install with administrator privileges
sudo python3 setup.py install
After installation, simply import wrapper in your Python program to call it.
3. Complete Calling Example
import ctypes
import wrapper
PIXEL_NUM = 228 # The 900-1700nm module has 228 data points
# 1. Connect to the device and get the device count
result = wrapper.dlpConnect()
print(f"Connected devices: {result}
")
# 2. Open the device
index = 0
result = wrapper.dlpOpenByUsb(index)
if result >= 0:
print("HID USB opened successfully!
")
else:
print("HID USB open failed!
")
# 3. Read wavelengths
wls_buf = (ctypes.c_double * PIXEL_NUM)()
result = wrapper.dlpGetWavelengths(wls_buf, PIXEL_NUM)
if result >= 0:
for i in range(5):
print(f"{wls_buf[i]:.2f}") # Print the first 5 wavelength values
# 4. Read intensity values
intensities_buf = (ctypes.c_int * PIXEL_NUM)()
activeIndex = 0
result = wrapper.dlpGetIntensities(activeIndex, intensities_buf, PIXEL_NUM)
if result >= 0:
for i in range(5):
print(f"{wls_buf[i]:.2f}: {intensities_buf[i]}")
Step-by-step Code Walkthrough
Step 1 — Import the module: import wrapper imports the installed SDK extension package.
Step 2 — Define the number of data points: the DLP NIR module outputs a fixed 228 pixels.
Step 3 — Connect and open: dlpConnect() returns the number of plugged-in USB devices; dlpOpenByUsb(0) opens the first device.
Step 4 — Create C-type buffers: (ctypes.c_double * PIXEL_NUM)() creates a memory block compatible with a C array.
Step 5 — Read data: call dlpGetWavelengths and dlpGetIntensities to fill the buffers.
Step 6 — Run as administrator:
sudo python3 main.py
Note:
1. Because USB HID device operations are involved, the program must be run with administrator privileges.
2. Reading wavelengths and intensities involves a scan time proportional to the configured averaging count — the larger the averaging count, the longer each scan takes.
4. FAQ
4.1 "udev" related information not found
udev is the daemon used in Linux systems to manage device nodes. Because the SDK involves USB HID interface calls, the system needs libudev-dev:
sudo apt-get update
sudo apt-get install libudev-dev
4.2 libwrapper.so file not found
Placing libwrapper.so in the lib/ subdirectory loads it correctly, but placing it in the same directory as wrapper.py may raise a "file not found" error. Keeping the lib/libwrapper.so directory structure is recommended.
This article was compiled by Pynect.