205 lines
5.5 KiB
C++
205 lines
5.5 KiB
C++
// ===========================================================================
|
||
// arisbridge.cpp —— MSVC 侧动态加载 aris_bridge.dll 并调用其纯 C 接口
|
||
// ===========================================================================
|
||
|
||
#include "arisbridge.h"
|
||
|
||
#include <windows.h>
|
||
|
||
#include <cstdio>
|
||
|
||
namespace {
|
||
const char *const kBridgeFileName = "aris_bridge.dll";
|
||
}
|
||
|
||
ArisBridge *ArisBridge::s_instance = nullptr;
|
||
|
||
ArisBridge &ArisBridge::instance()
|
||
{
|
||
// 函数内静态:第一次使用时加载 DLL,之后复用
|
||
static ArisBridge bridge;
|
||
return bridge;
|
||
}
|
||
|
||
ArisBridge::ArisBridge()
|
||
{
|
||
load();
|
||
}
|
||
|
||
ArisBridge::~ArisBridge()
|
||
{
|
||
if (m_available)
|
||
{
|
||
disconnect();
|
||
}
|
||
m_available = false;
|
||
s_instance = nullptr;
|
||
if (m_module)
|
||
{
|
||
FreeLibrary(static_cast<HMODULE>(m_module));
|
||
m_module = nullptr;
|
||
}
|
||
}
|
||
|
||
bool ArisBridge::load()
|
||
{
|
||
// 优先从 exe 所在目录加载,避免被 PATH 里的同名 DLL 抢先
|
||
char exePath[MAX_PATH] = {0};
|
||
GetModuleFileNameA(nullptr, exePath, MAX_PATH);
|
||
std::string directory(exePath);
|
||
const std::string::size_type pos = directory.find_last_of("\\/");
|
||
if (pos != std::string::npos)
|
||
{
|
||
directory.erase(pos);
|
||
}
|
||
const std::string fullPath = directory + "\\" + kBridgeFileName;
|
||
|
||
HMODULE module = LoadLibraryA(fullPath.c_str());
|
||
if (!module)
|
||
{
|
||
module = LoadLibraryA(kBridgeFileName);
|
||
}
|
||
if (!module)
|
||
{
|
||
m_lastError = "加载 " + fullPath + " 失败(错误码 "
|
||
+ std::to_string(static_cast<long>(GetLastError())) + ")";
|
||
return false;
|
||
}
|
||
m_module = module;
|
||
|
||
m_connect = reinterpret_cast<FnConnect>(GetProcAddress(module, "aris_bridge_connect"));
|
||
m_disconnect = reinterpret_cast<FnDisconnect>(GetProcAddress(module, "aris_bridge_disconnect"));
|
||
m_deviceCount = reinterpret_cast<FnDeviceCount>(GetProcAddress(module, "aris_bridge_device_count"));
|
||
m_deviceInfo = reinterpret_cast<FnDeviceInfo>(GetProcAddress(module, "aris_bridge_device_info"));
|
||
m_deviceWavelengths = reinterpret_cast<FnDeviceWavelengths>(GetProcAddress(module, "aris_bridge_device_wavelengths"));
|
||
m_setDataCallback = reinterpret_cast<FnSetDataCallback>(GetProcAddress(module, "aris_bridge_set_data_callback"));
|
||
m_setSoftwareTrigger = reinterpret_cast<FnNoArg>(GetProcAddress(module, "aris_bridge_set_software_trigger"));
|
||
m_startMeasure = reinterpret_cast<FnStartMeasure>(GetProcAddress(module, "aris_bridge_start_measure"));
|
||
m_stopMeasure = reinterpret_cast<FnNoArg>(GetProcAddress(module, "aris_bridge_stop_measure"));
|
||
|
||
if (!m_connect || !m_disconnect || !m_deviceCount || !m_deviceInfo
|
||
|| !m_deviceWavelengths || !m_setDataCallback || !m_setSoftwareTrigger
|
||
|| !m_startMeasure || !m_stopMeasure)
|
||
{
|
||
m_lastError = "aris_bridge.dll 的导出函数不完整(版本不匹配?)";
|
||
FreeLibrary(module);
|
||
m_module = nullptr;
|
||
return false;
|
||
}
|
||
|
||
s_instance = this;
|
||
m_setDataCallback(&ArisBridge::dataTrampoline);
|
||
m_available = true;
|
||
return true;
|
||
}
|
||
|
||
void __cdecl ArisBridge::dataTrampoline(const char *serial,
|
||
const float *wavelengths, int wavelengthCount,
|
||
const float *intensities, int intensityCount)
|
||
{
|
||
ArisBridge *self = s_instance;
|
||
if (!self || !self->m_handler)
|
||
{
|
||
return;
|
||
}
|
||
|
||
std::string serialNumber = serial ? serial : "";
|
||
std::vector<float> wave;
|
||
if (wavelengths && wavelengthCount > 0)
|
||
{
|
||
wave.assign(wavelengths, wavelengths + wavelengthCount);
|
||
}
|
||
std::vector<float> spectrum;
|
||
if (intensities && intensityCount > 0)
|
||
{
|
||
spectrum.assign(intensities, intensities + intensityCount);
|
||
}
|
||
|
||
self->m_handler(serialNumber, wave, spectrum);
|
||
}
|
||
|
||
int ArisBridge::connect()
|
||
{
|
||
if (!m_available)
|
||
{
|
||
return -1;
|
||
}
|
||
return m_connect();
|
||
}
|
||
|
||
void ArisBridge::disconnect()
|
||
{
|
||
if (m_available && m_disconnect)
|
||
{
|
||
m_disconnect();
|
||
}
|
||
}
|
||
|
||
int ArisBridge::deviceCount() const
|
||
{
|
||
if (!m_available || !m_deviceCount)
|
||
{
|
||
return 0;
|
||
}
|
||
return m_deviceCount();
|
||
}
|
||
|
||
ArisBridge::DeviceInfo ArisBridge::deviceInfo(int index) const
|
||
{
|
||
DeviceInfo info;
|
||
if (!m_available || !m_deviceInfo)
|
||
{
|
||
return info;
|
||
}
|
||
|
||
char serial[256] = {0};
|
||
int startPixel = 0;
|
||
int endPixel = 0;
|
||
int pixelCount = 0;
|
||
if (m_deviceInfo(index, serial, static_cast<int>(sizeof(serial)),
|
||
&startPixel, &endPixel, &pixelCount) != 0)
|
||
{
|
||
return info;
|
||
}
|
||
|
||
info.serialNumber = serial;
|
||
info.startPixel = startPixel;
|
||
info.endPixel = endPixel;
|
||
info.pixelCount = pixelCount;
|
||
|
||
const int capacity = (pixelCount > 0) ? pixelCount : 8192;
|
||
std::vector<float> wavelengths(static_cast<size_t>(capacity), 0.0f);
|
||
const int got = m_deviceWavelengths(index, wavelengths.data(), capacity);
|
||
if (got > 0)
|
||
{
|
||
wavelengths.resize(static_cast<size_t>(got));
|
||
info.wavelengths.swap(wavelengths);
|
||
}
|
||
return info;
|
||
}
|
||
|
||
void ArisBridge::setSoftwareTrigger()
|
||
{
|
||
if (m_available && m_setSoftwareTrigger)
|
||
{
|
||
m_setSoftwareTrigger();
|
||
}
|
||
}
|
||
|
||
int ArisBridge::startMeasure(int integrationTimeUs, int averaging)
|
||
{
|
||
if (!m_available || !m_startMeasure)
|
||
{
|
||
return -1;
|
||
}
|
||
return m_startMeasure(integrationTimeUs, averaging);
|
||
}
|
||
|
||
void ArisBridge::stopMeasure()
|
||
{
|
||
if (m_available && m_stopMeasure)
|
||
{
|
||
m_stopMeasure();
|
||
}
|
||
}
|