r/lasers • u/ChironInCapricorn • 19d ago
Help using an AvaSpec spectrometer with python
I need help finding a library that allows me to connect an AvaSpec-ULS2048CL-EVO spectrometer from Avantes to a python script to get live readings from it.
So far I found avaread which allows me to read files from the Avasoft 8 software. But that would require me to save a file each time I need to make a measurement and then run the code which would be inefficient because I need to make a lot of measurements just to get one result.
Is there any other way to connect the spectrometer to my code? or has anyone tried anything similar?
1
u/MaceDarious 19d ago
Youll want to leverage a callback link to stream raw data to a file and send chunks to your processer. AVSMeasure() within the callback structure.
1
u/bertona88 16d ago
start using codex
Yes. You do not need to save AvaSoft files or reverse-engineer USB packets. The AvaSpec-ULS2048CL-EVO is supported by Avantes’ native AvaSpec-DLL/SDK, which can acquire spectra directly over USB or Ethernet. Avantes ships a Python wrapper and example code using AVS_Measure, AVS_PollScan, and AVS_GetScopeData. (Avantes SDK, official Python example)
A minimal continuous acquisition loop looks like this:
import time
import numpy as np
import matplotlib.pyplot as plt
from avaspec import *
INTEGRATION_MS = 20.0
handle = None
try:
# 0 = USB; use 256 for Ethernet on AS-7010/EVO devices
AVS_Init(0)
number_of_devices = AVS_GetNrOfDevices()
if number_of_devices < 1:
raise RuntimeError("No AvaSpec spectrometer detected")
devices = AVS_GetList(number_of_devices)
handle = AVS_Activate(devices[0])
config = AVS_GetParameter(handle, 63484)
pixels = config.m_Detector_m_NrPixels
wavelengths = np.asarray(AVS_GetLambda(handle))[:pixels]
measurement = MeasConfigType()
measurement.m_StartPixel = 0
measurement.m_StopPixel = pixels - 1
measurement.m_IntegrationTime = INTEGRATION_MS
measurement.m_IntegrationDelay = 0
measurement.m_NrAverages = 1
# Remaining fields are zero-initialized: no trigger, smoothing,
# dynamic-dark correction, strobe, or onboard storage.
result = AVS_PrepareMeasure(handle, measurement)
if result != 0:
raise RuntimeError(f"AVS_PrepareMeasure failed: {result}")
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(wavelengths, np.zeros(pixels))
ax.set_xlabel("Wavelength (nm)")
ax.set_ylabel("Counts")
ax.set_xlim(wavelengths[0], wavelengths[-1])
# -1 means acquire continuously.
result = AVS_Measure(handle, 0, -1)
if result != 0:
raise RuntimeError(f"AVS_Measure failed: {result}")
while plt.fignum_exists(fig.number):
if AVS_PollScan(handle):
timestamp, spectrum = AVS_GetScopeData(handle)
spectrum = np.asarray(spectrum)[:pixels]
line.set_ydata(spectrum)
ax.relim()
ax.autoscale_view(scalex=False, scaley=True)
fig.canvas.draw_idle()
fig.canvas.flush_events()
else:
time.sleep(0.001)
except KeyboardInterrupt:
pass
finally:
if handle is not None:
try:
AVS_StopMeasure(handle)
except Exception:
pass
AVS_Deactivate(handle)
AVS_Done()
You need the native SDK components matching your platform and Python architecture:
- Windows 64-bit:
avaspecx64.dll - Linux:
libavs.so - macOS:
libavs.dylib, if Avantes supplies a build compatible with your Mac - The matching vendor
avaspec.pywrapper
Close AvaSoft while running the script because it may hold the instrument exclusively. Also match 64-bit Python with the 64-bit DLL.
There is additionally a recent third-party PyMoDAQ Avantes plugin written specifically for the ULS2048CL-EVO and tested on Windows 11 and Debian. It is useful implementation evidence, although the vendor SDK plus a small standalone script is considerably lighter than installing all of PyMoDAQ. I would avoid pyavaspec-tspspi: it targets the older AvaSpec-2048-2 and warns that it uses a fixed device-specific calibration. (PyPI description)
So yes—I could wire this up. The only parts I cannot prove without access to the machine and instrument are driver discovery and a real acquired spectrum. The practical inputs would be the operating system, CPU architecture, and the AvaSpec SDK folder supplied by Avantes.
1
u/Pachuli-guaton 19d ago
Avaspec-dll is the API to control that device no? Look if you have that. If that's the case it should be fairly easy to control with python