Files
cavity_LIBS/Spectrometer/devicemanager.cpp
T
2026-11-06 14:45:09 +08:00

313 lines
10 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#define _WINSOCKAPI_
#define _HAS_STD_BYTE 0
#include <winsock2.h> // 确保首先包含
#include <windows.h> // 然后包含windows.h
#include <QDebug>
#include "devicemanager.h"
#include <fstream>
#include "qdatetime.h"
#include "../Public/util.h"
#include <QFile>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#ifndef CAVITY_USE_ARIS_BRIDGE
#include "ziolinkprotocol.h"
#include "spectrometerdevice.h"
#endif
#include "spectrometer.h"
using namespace std;
DeviceManager* g_pDeviceManager = nullptr;
unsigned int DeviceManager::m_nDeviceNum = 0; //实际已连接的设备数量
std::vector<Spectrometer*> DeviceManager::m_pDeviceLst ;
DeviceManager::DeviceManager(QObject *parent) : QObject(parent), pixelCount(0)
{
qDebug() << "激光器串口连接" << endl;
m_pDeviceLst.resize(0); //new std::vector<NewDevice*>();
m_nDeviceNum = 0;
g_pDeviceManager = this;
#ifdef CAVITY_USE_ARIS_BRIDGE
// MSVC:通过 MinGW 桥接 DLL 使用真实 ARIS SDK
ArisBridge &bridge = ArisBridge::instance();
bridge.setDataHandler(&DeviceManager::onBridgeData);
if (bridge.isAvailable())
{
qDebug() << "ARIS 桥接 DLL 已加载(aris_bridge.dll)";
}
else
{
qDebug() << "ARIS 桥接 DLL 不可用:" << QString::fromStdString(bridge.lastError());
}
#else
m_pZio = new ZioLinkProtocol();
#endif
}
DeviceManager::~DeviceManager()
{
DeactivateDeviceLst();
#ifndef CAVITY_USE_ARIS_BRIDGE
delete m_pZio;
m_pZio = nullptr;
#endif
}
// 读取波长校准txt文件
std::vector<float> DeviceManager::readWavelengthCalibratedFile(const string &serialNumber) {
std::vector<float> result;
std::string filename = "wavelength_calibrated_files/wavelength_calibrated_" + serialNumber + ".txt";
std::ifstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("无法打开文件: " + filename);
}
qDebug() << "成功读取波长校准文件:" + QString::fromStdString(filename);
float value;
while (file >> value) {
result.push_back(value);
}
// 检查读取过程是否出错(排除正常到达文件末尾的情况)
if (!file.eof() && file.fail()) {
throw std::runtime_error("文件格式错误: " + filename);
}
return result;
}
// 用校准文件的波长覆盖设备波长;失败时退回设备自带波长
void DeviceManager::loadDeviceWavelengths(Spectrometer *device, const std::vector<float> &fallback)
{
try
{
std::vector<float> wavelengthListFixed = readWavelengthCalibratedFile(device->m_serialNumber);
if (!wavelengthListFixed.empty())
{
device->m_wavelengthList.assign(wavelengthListFixed.begin(), wavelengthListFixed.end());
return;
}
}
catch (...)
{
// 校准文件不存在或格式错误,走下面的回退分支
}
qDebug() << "波长未校准,光谱仪序列号=" + QString::fromStdString(device->m_serialNumber);
device->m_wavelengthList.assign(fallback.begin(), fallback.end());
}
std::vector<Spectrometer*> DeviceManager::DetectDeviceLst() //检测设备列表,并激活(打开设备)
{
qDebug() << "检测设备列表,并激活(打开设备)" << endl;
// 如果设备列表不为空,直接返回缓存的设备列表
if(m_pDeviceLst.size() > 0){
qDebug() << "返回已缓存的设备列表,设备数量:" << m_pDeviceLst.size();
return m_pDeviceLst;
}
qDebug() << "首次检测设备,开始连接...";
#ifdef CAVITY_USE_ARIS_BRIDGE
// ---------------- 桥接 DLL 后端(MSVC)----------------
ArisBridge &bridge = ArisBridge::instance();
if (!bridge.isAvailable())
{
qDebug() << "光谱仪不可用(桥接 DLL 未加载):"
<< QString::fromStdString(bridge.lastError());
m_nDeviceNum = 0;
return m_pDeviceLst;
}
const int deviceCount = bridge.connect();
m_nDeviceNum = (deviceCount > 0) ? static_cast<unsigned int>(deviceCount) : 0;
m_pDeviceLst.clear();
for (int i = 0; i < deviceCount; i++)
{
const ArisBridge::DeviceInfo info = bridge.deviceInfo(i);
Spectrometer* device = new Spectrometer();
device->m_serialNumber = info.serialNumber;
device->m_handle = static_cast<short>(i);
device->m_pixelsCount = static_cast<unsigned short>(info.pixelCount);
device->m_startPixels = static_cast<unsigned short>(info.startPixel);
device->m_stopPixels = static_cast<unsigned short>(info.endPixel);
loadDeviceWavelengths(device, info.wavelengths);
m_pDeviceLst.push_back(device);
qDebug() << "创建设备:" << QString::fromStdString(device->m_serialNumber)
<< ",句柄:" << i << ",像素:" << static_cast<int>(device->m_pixelsCount);
pixelCount += static_cast<int>(device->m_wavelengthList.size());
}
if (m_nDeviceNum > 0)
{
qDebug() << "设置触发模式";
bridge.setSoftwareTrigger();
}
qDebug() << "设备检测完成,总共创建" << m_nDeviceNum << "个设备";
return m_pDeviceLst;
#else
// ---------------- SDK / 兼容桩后端 ----------------
m_pZio->connectDevice();
m_nDeviceNum = m_pZio->getConnectedDeviceCount();
// 确保列表为空(防止重复添加
m_pDeviceLst.clear();
for(int i = 0; i < m_nDeviceNum; i++){
SpectrometerParameters params = m_pZio->getDevice(i)->getParameters();
Spectrometer* device = new Spectrometer();
device->m_serialNumber = params.serialNumber;
device->m_handle = i;
device->m_pixelsCount = static_cast<unsigned short>(params.pixelCount);
device->m_startPixels = static_cast<unsigned short>(params.startPixel);
device->m_stopPixels = static_cast<unsigned short>(params.endPixel);
loadDeviceWavelengths(device, params.wavelengths);
m_pDeviceLst.push_back(device);
qDebug() << "创建设备:" << QString::fromStdString(device->m_serialNumber) << ",句柄:" << i;
pixelCount += static_cast<int>(device->m_wavelengthList.size());
}
if(m_nDeviceNum > 0){
qDebug() << "设置触发模式";
m_pZio->setTriggerMode(TriggerMode::SOFTWARE);
}
qDebug() << "设备检测完成,总共创建" << m_nDeviceNum << "个设备";
return m_pDeviceLst;
#endif
}
void DeviceManager::DeactivateDeviceLst() //关闭所有设备
{
qDebug() << "关闭所有设备,当前设备数量:" << m_pDeviceLst.size();
// 先断开设备连接
#ifdef CAVITY_USE_ARIS_BRIDGE
ArisBridge::instance().disconnect();
#else
if(m_pZio) {
m_pZio->disconnectDevice();
}
#endif
// 删除所有设备对象
for(auto& device : m_pDeviceLst){
if(device != nullptr) {
qDebug() << "删除设备:" << QString::fromStdString(device->m_serialNumber);
delete device;
device = nullptr;
}
}
// 清空容器
m_pDeviceLst.clear();
m_nDeviceNum = 0;
qDebug() << "所有设备已关闭并清理";
}
void DeviceManager::onDetectDeviceLst()
{
DetectDeviceLst();
emit DetectDeviceFinish(m_pDeviceLst);
}
void DeviceManager::CloseAllDevice()
{
#ifdef CAVITY_USE_ARIS_BRIDGE
ArisBridge::instance().disconnect();
#else
m_pZio->disconnectDevice();
#endif
}
//获取设备列表
std::vector<Spectrometer*> DeviceManager::GetDeviceList()
{
if(m_pDeviceLst.size() == 0){
return DetectDeviceLst();
}
return m_pDeviceLst;
}
void DeviceManager::stopMeasure()
{
#ifdef CAVITY_USE_ARIS_BRIDGE
ArisBridge::instance().stopMeasure();
#else
m_pZio->stopMeasurement();
#endif
}
int DeviceManager::StartMeasure(int integrationTime, int unNrAverages)
{
#ifdef CAVITY_USE_ARIS_BRIDGE
ArisBridge::instance().startMeasure(integrationTime, unNrAverages);
#else
m_pZio->startMeasurement(this, integrationTime, unNrAverages);
#endif
bStartMeasure=true;
return 0;
}
// 把光谱数据转成 Qt 信号(可能在 SDK 采集线程里被调用,Qt 会自动排队到界面线程)
void DeviceManager::handleData(const std::string &spectrumSerialNumber,
const std::vector<float> &wavelengthList,
const std::vector<float> &intensityList)
{
QVector<float> qWavelengthList(wavelengthList.begin(), wavelengthList.end());
QVector<float> qIntensityList(intensityList.begin(), intensityList.end());
emit spectrumDataReady(QString::fromStdString(spectrumSerialNumber),
qWavelengthList,
qIntensityList);
}
#ifdef CAVITY_USE_ARIS_BRIDGE
// 桥接 DLL 的 C 回调 → 当前 DeviceManager 实例
void DeviceManager::onBridgeData(const std::string &spectrumSerialNumber,
const std::vector<float> &wavelengthList,
const std::vector<float> &intensityList)
{
if (g_pDeviceManager)
{
g_pDeviceManager->handleData(spectrumSerialNumber, wavelengthList, intensityList);
}
}
#else
void DeviceManager::onDataReceived(std::string spectrumSerialNumber,
const std::vector<float> &wavelengthList,
const std::vector<float> &intensityList)
{
handleData(spectrumSerialNumber, wavelengthList, intensityList);
}
void DeviceManager::onDataReceivedBanch(std::string spectrumSerialNumber,
const std::vector<float> &wavelengthList,
const std::vector<std::vector<float>> &spectrumList)
{
// 批量模式:逐帧转成单帧信号发出,界面处理逻辑无需改动
for (const auto &intensityList : spectrumList) {
onDataReceived(spectrumSerialNumber, wavelengthList, intensityList);
}
}
void DeviceManager::onError(std::string deviceIndex, const string &error)
{
qDebug() << "光谱仪出错了" << "deviceIndex=" << QString::fromStdString(deviceIndex)
<< "error is " << QString::fromStdString(error);
}
#endif