添加项目文件。
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
#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>
|
||||
#include "ziolinkprotocol.h"
|
||||
#include "spectrometerdevice.h"
|
||||
#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;
|
||||
|
||||
m_pZio = new ZioLinkProtocol();
|
||||
}
|
||||
|
||||
DeviceManager::~DeviceManager()
|
||||
{
|
||||
DeactivateDeviceLst();
|
||||
delete m_pZio;
|
||||
m_pZio = nullptr;
|
||||
}
|
||||
|
||||
// 读取波长校准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;
|
||||
}
|
||||
|
||||
std::vector<Spectrometer*> DeviceManager::DetectDeviceLst() //检测设备列表,并激活(打开设备)
|
||||
{
|
||||
qDebug() << "检测设备列表,并激活(打开设备)" << endl;
|
||||
// 如果设备列表不为空,直接返回缓存的设备列表
|
||||
if(m_pDeviceLst.size() > 0){
|
||||
qDebug() << "返回已缓存的设备列表,设备数量:" << m_pDeviceLst.size();
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
qDebug() << "首次检测设备,开始连接...";
|
||||
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;
|
||||
|
||||
try
|
||||
{
|
||||
std::vector<float> wavelengthListFixed = readWavelengthCalibratedFile(device->m_serialNumber);
|
||||
if (wavelengthListFixed.empty()) {
|
||||
qDebug() << "波长未校准,光谱仪序列号=" + QString::fromStdString(device->m_serialNumber);
|
||||
device->m_wavelengthList.assign(params.wavelengths.begin(), params.wavelengths.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
device->m_wavelengthList.assign(wavelengthListFixed.begin(), wavelengthListFixed.end());
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
qDebug() << "波长未校准,光谱仪序列号=" + QString::fromStdString(device->m_serialNumber);
|
||||
}
|
||||
|
||||
m_pDeviceLst.push_back(device);
|
||||
qDebug() << "创建设备:" << QString::fromStdString(device->m_serialNumber) << ",句柄:" << i;
|
||||
pixelCount += device->m_wavelengthList.size();
|
||||
}
|
||||
|
||||
if(m_nDeviceNum > 0){
|
||||
qDebug() << "设置触发模式";
|
||||
m_pZio->setTriggerMode(TriggerMode::SOFTWARE);
|
||||
}
|
||||
|
||||
qDebug() << "设备检测完成,总共创建" << m_nDeviceNum << "个设备";
|
||||
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
|
||||
void DeviceManager::DeactivateDeviceLst() //关闭所有设备
|
||||
{
|
||||
qDebug() << "关闭所有设备,当前设备数量:" << m_pDeviceLst.size();
|
||||
|
||||
// 先断开设备连接
|
||||
if(m_pZio) {
|
||||
m_pZio->disconnectDevice();
|
||||
}
|
||||
|
||||
// 删除所有设备对象
|
||||
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()
|
||||
{
|
||||
m_pZio->disconnectDevice();
|
||||
}
|
||||
|
||||
//获取设备列表
|
||||
std::vector<Spectrometer*> DeviceManager::GetDeviceList()
|
||||
{
|
||||
if(m_pDeviceLst.size() == 0){
|
||||
return DetectDeviceLst();
|
||||
}
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
|
||||
void DeviceManager::stopMeasure()
|
||||
{
|
||||
m_pZio->stopMeasurement();
|
||||
}
|
||||
|
||||
int DeviceManager::StartMeasure(int integrationTime, int unNrAverages)
|
||||
{
|
||||
m_pZio->startMeasurement(this, integrationTime, unNrAverages);
|
||||
bStartMeasure=true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DeviceManager::onDataReceived(std::string spectrumSerialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<float> &intensityList)
|
||||
{
|
||||
QVector<float> qWavelengthList = QVector<float>::fromStdVector(wavelengthList);
|
||||
QVector<float> qIntensityList = QVector<float>::fromStdVector(intensityList);
|
||||
emit spectrumDataReady(QString::fromStdString(spectrumSerialNumber),
|
||||
qWavelengthList,
|
||||
qIntensityList);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user