Migration from legacy SDK¶
This page is for customers with existing code written against the legacy Bentham SDK - the
pyBen Python module, the bendll.h/dllerror.h/dlltoken.h C headers, or the
BI_*.vi LabVIEW wrapper VIs - who want to move to this SDK.
The guide covers the languages that both SDKs target: Python, C, C++ and LabVIEW.
What does not change¶
Both SDKs are wrappers around the same BI_* functions exported by the BenHW DLL, so the
hardware side of your application carries over unchanged:
Your system configuration (
.cfg) and setup (.atr) files are used as they are.The call sequence is the same: build the system model, load the setup, initialise, park, zero calibrate, select wavelength, measure, close. See SDK Model.
Every token and error code defined in the legacy
dlltoken.h/dllerror.hexists in this SDK with the same name and the same numeric value. See Tokens and Errors.Component IDs passed to
get/setand other functions are the IDs in your.cfgfile, as before.
What changes is how you load the library, how results come back, and how errors are reported.
Python: pyBen to benhw¶
Installation¶
Uninstall the pyBen wheel or MSI installer and install benhw from PyPI. benhw
requires Python 3.12 or later and bundles the BenHW DLLs, so nothing needs to be copied onto
PATH.
pip uninstall pyBen
pip install benhw
Overview of the differences¶
Topic |
|
|
|---|---|---|
Import |
|
|
Calling a function |
Module-level function: |
Method on an instance: |
Function names |
|
Unchanged from |
Return value |
A dict: |
The value itself ( |
Errors |
Check |
An exception derived from |
Tokens |
Raw integers, e.g. |
Named constants, e.g. |
DLL loading |
On |
On |
Before and after¶
Legacy pyBen code:
import pyBen
r = pyBen.build_system_model("system.cfg")
if r["Error"] != 0:
raise RuntimeError(r["description"])
pyBen.load_setup("system.atr")
pyBen.initialise()
pyBen.park()
pyBen.zero_calibration(300.0, 800.0)
pyBen.select_wavelength(555.0, 0)
result = pyBen.automeasure()
if result["Error"] == 0:
print(result["reading"])
wl = pyBen.get("mono", 11, 0)["value"] # 11 = MonochromatorCurrentWL
pyBen.close()
The same code using benhw:
import time
from benhw import BenHW, tokens, SdkException
hw = BenHW()
try:
hw.build_system_model("system.cfg")
hw.load_setup("system.atr")
hw.initialise()
hw.park()
hw.zero_calibration(300.0, 800.0)
settle_ms = hw.select_wavelength(555.0)
time.sleep(settle_ms / 1000)
reading = hw.automeasure()
print(reading)
wl = hw.get("mono", tokens.MonochromatorCurrentWL, 0)
except SdkException as e:
print(f"BenHW error {e.error_code}: {e.message}")
finally:
hw.close()
Error handling¶
Remove the ["Error"] checks. Every method either succeeds or raises:
SdkExceptionis the base class; it haserror_code(theBI_*value) andmessage.Each error code has its own subclass, e.g.
AdcOverloadExceptionforBI_ADC_OverloadorInvalidComponentExceptionforBI_invalid_component. These are importable frombenhworbenhw.exceptions. See Errors for the full mapping.When the DLL returns the generic
BI_error(-1), the wrapper callsBI_report_error()for you and raises the more specific exception if one is available. The legacy pattern of callingreport_error()after a failure is no longer needed.build_system_modelno longer returns the description string; if the configuration cannot be built, the DLL’s message is themessageof the raisedSdkException.
from benhw import exceptions
try:
reading = hw.automeasure()
except exceptions.AdcOverloadException:
hw.autorange()
reading = hw.automeasure()
Functions whose signature or result changed¶
Most functions only need the pyBen. prefix replaced and the ["key"] lookup removed.
The following need more attention:
|
|
|---|---|
|
|
|
|
|
|
|
Returns a result object with list attributes |
|
Returns a |
|
|
|
|
|
Returns a |
|
Returns the detailed error code as an |
|
Integers still work, but use the names in |
benhw also exposes functions that pyBen never wrapped, including get_str/set_str,
multi_automeasure, multi_park, get_min_step,
get_max_bw, logging (start_log, stop_log, get_log) and SCPI_query/SCPI_write.
See Python API for the complete list.
C: bendll.h to benhw.h¶
The function names and arguments are the BI_* functions you already call, so most C code
compiles after changing the includes and the loader calls. Install the headers and DLLs as
described in C API.
Overview of the differences¶
Topic |
Legacy headers |
|
|---|---|---|
Includes |
|
|
Loading the DLL |
Call |
Automatic on the first |
Unloading the DLL |
|
|
DLL used |
|
|
Settle delay type |
|
|
|
|
|
Function not found in DLL |
|
That call returns |
Before and after¶
Legacy code:
#include <windows.h>
#include "dllerror.h"
#include "dlltoken.h"
#include "bendll.h"
int main(void)
{
char error[256];
long settle_delay = 0;
double reading;
if (init() != LOADED_DLL)
return 1;
if (BI_build_system_model("system.cfg", error) != BI_OK) { /* ... */ }
BI_load_setup("system.atr");
BI_initialise();
BI_park();
BI_select_wavelength(555.0, &settle_delay);
Sleep(settle_delay);
BI_automeasure(&reading);
close(); /* legacy close() only unloads the DLL */
return 0;
}
The same code using benhw.h:
#include <stdio.h>
#include "benhw.h"
int main(void)
{
char error[256];
int settle_delay = 0;
double reading;
if (!benhw_load(NULL)) /* optional: fail early if the DLL is missing */
return 1;
if (BI_build_system_model("system.cfg", error) != BI_OK)
{
printf("%s\n", error);
return 1;
}
BI_load_setup("system.atr");
BI_initialise();
BI_park();
BI_select_wavelength(555.0, &settle_delay);
Sleep(settle_delay);
BI_automeasure(&reading);
BI_close(); /* release the system model */
benhw_unload();
return 0;
}
Notes¶
Remove the legacy loader helpers. Delete calls to
init()andclose(): they were helper functions defined inbendll.h, not DLL exports. The legacyclose()only calledFreeLibrary; it never calledBI_close(). CallBI_close()yourself before unloading.Change the settle-delay variable type. Use
intinstead oflongforBI_select_wavelength,BI_multi_select_wavelengthandBI_component_select_wl.Ship the cdecl or 64-bit DLL, not
benhw32_stdcall.dll.benhw.hlooks for the DLL next to your executable, then in adllssubfolder, then on the normal DLL search path. To use a specific file, callbenhw_load("C:\\path\\to\\benhw64.dll")before any other call. Your program can now be built as 64-bit.Error and token constants (
BI_OK,BI_error,MonochromatorCurrentWL, …) keep their names and values. Error checks such asif (result != BI_OK)are unchanged.If another part of your process has already loaded the DLL, use
benhw_load_from_module(hmodule)instead ofbenhw_load().
C++: bendll.h to benhw.hpp¶
C++ code that used the legacy C headers can follow the C migration
above and keep calling BI_* functions. Alternatively, switch to the benhw::BenHW class
in benhw.hpp (in the same package), which replaces error-code checks and output buffers
with return values and exceptions:
#include <iostream>
#include <thread>
#include <chrono>
#include "benhw.hpp"
int main()
{
benhw::BenHW hw; // loads the DLL; unloads it when hw goes out of scope
try {
hw.build_system_model("system.cfg");
hw.load_setup("system.atr");
hw.initialise();
hw.park();
hw.zero_calibration(300.0, 800.0);
int settle_ms = hw.select_wavelength(555.0);
std::this_thread::sleep_for(std::chrono::milliseconds(settle_ms));
double reading = hw.automeasure();
std::cout << reading << "\n";
double wl = hw.get("mono", MonochromatorCurrentWL, 0); // tokens.h constant
hw.close();
} catch (const benhw::SdkException& e) {
std::cerr << "BenHW error " << e.error_code() << ": " << e.what() << "\n";
return 1;
}
return 0;
}
The method names are the C function names without the BI_ prefix. The
exception classes and result types (e.g. get_zero_calibration_info_result)
match those of the Python SDK. See C++ API.
LabVIEW: BI_*.vi wrappers to the .NET assembly¶
The legacy LabVIEW examples were VIs (BI_SystemSetup.vi, BI_MoveToWavelength.vi,
BI_TakeReading.vi, BI_Get.vi, BI_Set.vi, …) that called the DLL through Call Library
Function Nodes and returned integer error codes. This SDK replaces them with the .NET
assembly BenHW.dll, which LabVIEW calls through .NET Constructor and Invoke Nodes. Install it
and create the constructor as described in LabVIEW.
Overview of the differences¶
Topic |
Legacy VIs |
.NET assembly |
|---|---|---|
Calling a function |
Call Library Function Node on the BenHW DLL |
Invoke Node on the reference from the |
Function names |
|
PascalCase, without |
Outputs |
Pointer parameters wired to indicators |
Return value of the Invoke Node |
Errors |
Integer return value compared with |
|
DLL selection |
Path configured in each Call Library Function Node |
Single |
Common replacements¶
Legacy VI / DLL call |
.NET method (Invoke Node) |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
Not needed: the error is in error out. |
|
|
|
|
A scan (BI_Scan.vi) becomes a loop of SelectWavelength, a wait for the returned settle
delay, and Automeasure. The full method list is in .NET API.
Notes¶
Errors arrive in the error cluster. When a method throws, LabVIEW reports a .NET exception in the Invoke Node’s error out; the exception message identifies the BenHW error. Wire error in/error out through your sequence, so a failure stops the following calls, in place of the legacy checks on each return code.
Match the DLL to LabVIEW’s bitness. Wire the path of
benhw64.dlltodllPathfor 64-bit LabVIEW, orbenhw32_cdecl.dllfor 32-bit LabVIEW. Both are in the downloaded zip.Requires the .NET Framework 4.8 runtime.
Tokens are entered as numbers, as before. Values are unchanged; see Tokens.