mirror of
http://172.16.0.230/r/SIF/TowerOptoSifAndSpectral.git
synced 2025-10-18 11:29:43 +08:00
new by zz
This commit is contained in:
@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.3)
|
||||
|
||||
project(TowerOptoSifAndSpectral)
|
||||
|
||||
set(QT Core Network WebSockets SerialPort)
|
||||
set(QT Core Network WebSockets SerialPort Widgets)
|
||||
|
||||
find_package(Qt5 REQUIRED ${QT})
|
||||
|
||||
@ -10,8 +10,9 @@ find_package(Qt5 REQUIRED ${QT})
|
||||
file(GLOB_RECURSE HDR_LIST "source/*.h")
|
||||
file(GLOB_RECURSE SRC_LIST "source/*.cpp")
|
||||
|
||||
|
||||
include_directories("source")
|
||||
include_directories("source/ATP")
|
||||
include_directories("source/Logger")
|
||||
|
||||
add_executable( TowerOptoSifAndSpectral
|
||||
main.cpp
|
||||
@ -21,6 +22,6 @@ add_executable( TowerOptoSifAndSpectral
|
||||
qt5_use_modules(TowerOptoSifAndSpectral ${QT})
|
||||
|
||||
|
||||
set_target_properties(TowerOptoSifAndSpectral PROPERTIES AUTOMOC ON)
|
||||
|
||||
|
||||
#Qt5_DIR("C:\Qt\Qt5.9.0")
|
||||
#include_directories("C:/Qt/Qt5.9.0/5.9/msvc2017_64/include")
|
21
main.cpp
21
main.cpp
@ -1,17 +1,24 @@
|
||||
#include<iostream>
|
||||
#include<QString>
|
||||
#include <QDebug>
|
||||
#include "pch.h"
|
||||
#include "ATPControl_Serial_QT.h"
|
||||
#include <QtCore/QCoreApplication>
|
||||
#include "Logger.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
QString qstrTest="This is a test message";
|
||||
//////////////////////////////////////////////////////////////////////////logger
|
||||
QT_LOG::ZZ_InitLogger(QCoreApplication::applicationDirPath() + "/Log/");
|
||||
QString qstrTest="This is a test message 2";
|
||||
qDebug() << qstrTest;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////test
|
||||
ZZ_ATPControl_Serial_Qt m_ctrlATP;
|
||||
QByteArray qbTest;
|
||||
m_ctrlATP.ATPInitialize(7);
|
||||
m_ctrlATP.GetDeviceAttribute();
|
||||
m_ctrlATP.RecvData(qbTest);
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,208 @@
|
||||
#include "pch.h"
|
||||
#include "ATPControl_Serial_QT.h"
|
||||
|
||||
ZZ_ATPControl_Serial_Qt::ZZ_ATPControl_Serial_Qt()
|
||||
{
|
||||
|
||||
m_pSerialPort = new QSerialPort;
|
||||
//connect(m_pSerialPort, &QSerialPort::readyRead, this, &ZZ_ATPControl_Serial_Qt::ReadMessage);
|
||||
m_iBaudRate = 115200;
|
||||
}
|
||||
|
||||
ZZ_ATPControl_Serial_Qt::~ZZ_ATPControl_Serial_Qt()
|
||||
{
|
||||
|
||||
if (m_pSerialPort != NULL)
|
||||
{
|
||||
delete m_pSerialPort;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int ZZ_ATPControl_Serial_Qt::SetBaudRate(int iBaud)
|
||||
{
|
||||
m_iBaudRate = iBaud;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int ZZ_ATPControl_Serial_Qt::ATPInitialize(ZZ_U8 ucPortNumber)
|
||||
{
|
||||
QString qstrPortName = QString("COM%1").arg(ucPortNumber);
|
||||
|
||||
m_pSerialPort->setPortName(qstrPortName);
|
||||
m_pSerialPort->setReadBufferSize(512);
|
||||
bool bRes = m_pSerialPort->setBaudRate(m_iBaudRate);
|
||||
if (!bRes)
|
||||
{
|
||||
qDebug() << "Err:setBaudRate Failed.Exit Code:1";
|
||||
//std::cout << "Err.setBaudRate Failed" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bRes = m_pSerialPort->open(QIODevice::ReadWrite);
|
||||
if (!bRes)
|
||||
{
|
||||
qDebug() << "Err:open Failed.Exit Code:2";
|
||||
//std::cout << "Err.open Failed" << std::endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ZZ_ATPControl_Serial_Qt::ATPClose()
|
||||
{
|
||||
m_pSerialPort->close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
int ZZ_ATPControl_Serial_Qt::GetDeviceInfo()
|
||||
{
|
||||
|
||||
QByteArray qbSend, qbRecv;
|
||||
qbSend.append(GET_PN_NUMBER);
|
||||
SendCommand(qbSend);
|
||||
RecvData(qbRecv);
|
||||
ParseData(qbRecv);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ZZ_ATPControl_Serial_Qt::GetDeviceAttribute()
|
||||
{
|
||||
QByteArray qbSend,qbRecv;
|
||||
qbSend.append(GET_WAVELENGTH_CALIBRATION_COEF);
|
||||
qbSend.resize(2);
|
||||
qbSend[0] = 0x00;
|
||||
qbSend[1] = 0x01;
|
||||
SendCommand(qbSend);
|
||||
RecvData(qbRecv);
|
||||
//ParseData(qbRecv);
|
||||
|
||||
float a[4];
|
||||
memcpy(a, (ZZ_U8*)(qbRecv[5]+16), 4 * 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ZZ_ATPControl_Serial_Qt::SendCommand(QByteArray qbCommand)
|
||||
{
|
||||
int iSize = qbCommand.size() + 3;
|
||||
QByteArray qbSend;
|
||||
qbSend.resize(4);
|
||||
qbSend[0] = (ZZ_U8)0xAA;
|
||||
qbSend[1] = 0x55;
|
||||
qbSend[2] = iSize / 256;
|
||||
qbSend[3] = iSize % 256;
|
||||
qbSend.append(qbCommand);
|
||||
|
||||
int iSum = 0;
|
||||
for (int i = 0; i < iSize - 1; i++)
|
||||
{
|
||||
iSum = iSum + qbSend[i + 2];
|
||||
}
|
||||
|
||||
qbSend.append(iSum % 256);
|
||||
|
||||
qint64 qi64Write= m_pSerialPort->write(qbSend);
|
||||
if (qi64Write != qbSend.size())
|
||||
{
|
||||
qDebug() << "Err:write Failed.Exit Code:1";
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ZZ_ATPControl_Serial_Qt::RecvData(QByteArray &qbData)
|
||||
{
|
||||
qbData.clear();
|
||||
qbData = m_pSerialPort->readAll();
|
||||
|
||||
int iCounter = 0;
|
||||
while (qbData.size() < 4)
|
||||
{
|
||||
m_pSerialPort->waitForReadyRead(50);
|
||||
QByteArray qbTemp = m_pSerialPort->readAll();
|
||||
qbData.append(qbTemp);
|
||||
|
||||
if (iCounter > 10)
|
||||
{
|
||||
qDebug() << "Err:RecvData Failed,Not Enough Data.Exit Code:1"<<qbData.size();
|
||||
return 1;
|
||||
}
|
||||
iCounter++;
|
||||
}
|
||||
|
||||
if ((ZZ_U8)qbData[0] != (ZZ_U8)0xaa || (ZZ_U8)qbData[1] != (ZZ_U8)0x55)
|
||||
{
|
||||
qDebug() << "Err:RecvData Failed,Wrong Header.Exit Code:2" << qbData.size();
|
||||
return 2;
|
||||
}
|
||||
|
||||
iCounter = 0;
|
||||
int iLength = qbData[2] * 256 + qbData[3] + 2;
|
||||
while (qbData.size() < iLength)
|
||||
{
|
||||
m_pSerialPort->waitForReadyRead(50);
|
||||
qbData.append(m_pSerialPort->readAll());
|
||||
|
||||
if (iCounter > 100)
|
||||
{
|
||||
qDebug() << "Err:RecvData Failed,Incomplete Data.Exit Code:3" << qbData.size();
|
||||
return 3;
|
||||
}
|
||||
iCounter++;
|
||||
}
|
||||
|
||||
if (qbData.size() > iLength)
|
||||
{
|
||||
qbData.remove(iLength - 1, qbData.size() - iLength);
|
||||
}
|
||||
int iCheckSumLength = iLength - 3;
|
||||
ZZ_U16 usCheckSum = 0;
|
||||
for (int i = 0; i < iCheckSumLength; i++)
|
||||
{
|
||||
usCheckSum += qbData[i+2];
|
||||
}
|
||||
usCheckSum = usCheckSum % 256;
|
||||
ZZ_U8 ucTemp = qbData[qbData.size() - 1];
|
||||
if ((ZZ_U8)usCheckSum != ucTemp)
|
||||
{
|
||||
qDebug() << "Err:RecvData Failed,Incorrect Check Sum.Exit Code:4" << qbData.size();
|
||||
qbData.clear();
|
||||
return 4;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ZZ_ATPControl_Serial_Qt::ParseData(QByteArray &qbData)
|
||||
{
|
||||
if (qbData.size() < 6)
|
||||
{
|
||||
qDebug() << "Err:ParseData Failed,Not Enough Data.Exit Code:1" << qbData.size();
|
||||
return 1;
|
||||
}
|
||||
qbData.remove(0, 5);
|
||||
qbData.remove(qbData.size() - 1, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//void ZZ_ATPControl_Serial_Qt::ReadMessage()
|
||||
//{
|
||||
// QByteArray qbTemp, qbTemp1;
|
||||
// qbTemp = m_pSerialPort->readAll();
|
||||
// while (qbTemp.size()<2)
|
||||
// {
|
||||
// m_pSerialPort->waitForReadyRead(50);
|
||||
// qbTemp1 = m_pSerialPort->readAll();
|
||||
// qbTemp.append(qbTemp1);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
//return;
|
||||
// }
|
||||
|
@ -1,16 +1,22 @@
|
||||
#pragma once
|
||||
#include "pch.h"
|
||||
#include "ZZ_Types.h"
|
||||
using namespace ZZ_MISCDEF;
|
||||
using namespace ZZ_MISCDEF::ATP;
|
||||
|
||||
class ZZ_ATPControl_Serial_Qt
|
||||
class QSerialPort;
|
||||
|
||||
class ZZ_ATPControl_Serial_Qt//:public QObject
|
||||
{
|
||||
//Q_OBJECT
|
||||
public:
|
||||
ZZ_ATPControl_Serial_Qt();
|
||||
virtual ~ZZ_ATPControl_Serial_Qt();
|
||||
|
||||
public:
|
||||
int ATPInitialize( ZZ_U8 usIndex);
|
||||
int SetBaudRate(int iBaud);
|
||||
|
||||
int ATPInitialize( ZZ_U8 ucPortNumber);
|
||||
int ATPClose();
|
||||
|
||||
int SingleShot(ATPDataFrame &dfData);
|
||||
@ -19,13 +25,29 @@ public:
|
||||
|
||||
int SetExposureTime(int iExposureTimeInMS);
|
||||
|
||||
int GetWaveLength(float *pfWaveLength);
|
||||
int GetAttribute();
|
||||
//int GetWaveLength(float *pfWaveLength);
|
||||
int GetDeviceInfo();
|
||||
int GetDeviceAttribute();
|
||||
int GetDeviceListInfo(); //use type name to enum
|
||||
int GetDeviceTemperature(float &fTemperature);
|
||||
|
||||
int PerformAutoExposure(float fMinScaleFactor, float fMaxScaleFactor, float &fPredictedExposureTime);
|
||||
|
||||
#ifdef _DEBUG
|
||||
public:
|
||||
#else //
|
||||
private:
|
||||
int SendCommand();
|
||||
#endif
|
||||
//port
|
||||
int m_iBaudRate;
|
||||
QSerialPort *m_pSerialPort;
|
||||
|
||||
//ATP
|
||||
ATPDeviceInfo m_adiDeviceInfo;
|
||||
ATPDeviceAttribute m_adaDeviceAttr;
|
||||
int SendCommand(QByteArray qbCommand);
|
||||
int RecvData(QByteArray &qbData);
|
||||
int ParseData(QByteArray &qbData);
|
||||
//private slots :
|
||||
//void ReadMessage();
|
||||
};
|
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
namespace ZZ_MISCDEF
|
||||
{
|
||||
@ -12,30 +12,30 @@ namespace ZZ_MISCDEF
|
||||
{
|
||||
const int MAX_SPECTRUM_SIZE = 4096;
|
||||
|
||||
const int GET_MODEBOARD_TEMP = 0x01;
|
||||
const int GET_MODULECIRCUIT_TEMP = 0x01;
|
||||
const int GET_PN_NUMBER = 0x03;
|
||||
const int GET_SN_NUMBER = 0x04;
|
||||
const int GET_MANUFACTURE_DATA = 0x06;
|
||||
const int GET_MANUFACTOR_INFO = 0x09;
|
||||
const int GET_MANUFACTURE_INFO = 0x09;
|
||||
const int GET_PIXEL_LENGTH = 0x0a;
|
||||
const int GET_TEC_TEMP = 0x13;
|
||||
const int SET_TEC_TEMP = 0x12;
|
||||
const int GET_OPTICFLAT_TEMP = 0x35;
|
||||
const int GET_CIRCUIT_BOARD_TEMP = 0x36;
|
||||
const int SET_INTEGRAL_TIME = 0x14;
|
||||
const int GET_INTEGRAL_TIME = 0x41;
|
||||
const int GET_MAX_INTEGRAL_TIME = 0x42;
|
||||
const int GET_MIN_INTEGRAL_TIME = 0x43;
|
||||
const int ASYNCHRONOUS_COLLECT_DARK = 0x23;
|
||||
const int ASYNCHRONOUS_START_COLLECTION = 0x16;
|
||||
const int ASYNCHRONOUS_READ_DATA = 0x17;
|
||||
const int GET_OPTICS_TEMP = 0x35;
|
||||
const int GET_CIRCUITBOARD_TEMP = 0x36;
|
||||
const int SET_INTEGRATION_TIME = 0x14;
|
||||
const int GET_INTEGRATION_TIME = 0x41;
|
||||
const int GET_MAX_INTEGRATION_TIME = 0x42;
|
||||
const int GET_MIN_INTEGRATION_TIME = 0x43;
|
||||
const int ASYNC_COLLECT_DARK = 0x23;
|
||||
const int ASYNC_START_COLLECTION = 0x16;
|
||||
const int ASYNC_READ_DATA = 0x17;
|
||||
const int SET_AVERAGE_NUMBER = 0x28;
|
||||
const int SYNCHRONIZATION_GET_DATA = 0x1e;
|
||||
const int SYNCHRONIZATION_GET_DARK = 0x2f;
|
||||
const int SYNC_GET_DATA = 0x1e;
|
||||
const int SYNC_GET_DARK = 0x2f;
|
||||
const int EXTERNAL_TRIGGER_ENABLE = 0x1f;
|
||||
const int SET_XENON_LAMP_DELAY_TIME = 0x24;
|
||||
const int GET_WAVELENGTH_CALIBRATION_COFF = 0x55;
|
||||
const int GET_STAT_OF_LAMPOUT = 0x60;
|
||||
const int GET_WAVELENGTH_CALIBRATION_COEF = 0x55;
|
||||
const int GET_STAT_LAMPOUT = 0x60;
|
||||
const int SET_GPIO = 0x61;
|
||||
//const int SYNCHRONIZATION_GET_DARK = 0x23
|
||||
|
||||
@ -52,6 +52,21 @@ namespace ZZ_MISCDEF
|
||||
float fTemperature;
|
||||
double dTimes = 0;
|
||||
}ATPDataFrame;
|
||||
|
||||
typedef struct tagATPDeviceInfo
|
||||
{
|
||||
std::string strPN;
|
||||
std::string strSN;
|
||||
}ATPDeviceInfo;
|
||||
|
||||
typedef struct tagATPDeviceAttribute
|
||||
{
|
||||
int iPixels;
|
||||
int iMaxIntegrationTime;
|
||||
int iMinIntegrationTime;
|
||||
float fWaveLength[4096];
|
||||
|
||||
}ATPDeviceAttribute;
|
||||
}
|
||||
|
||||
}
|
||||
|
90
source/Logger/Logger.h
Normal file
90
source/Logger/Logger.h
Normal file
@ -0,0 +1,90 @@
|
||||
//
|
||||
// Created by xin on 2021/8/17.
|
||||
//edit by zz.
|
||||
//fixed code page problem;added a new initialize function; --20211101
|
||||
#pragma once
|
||||
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QDateTime>
|
||||
#include "qmutex.h"
|
||||
#include "QtMsgHandler"
|
||||
|
||||
namespace QT_LOG
|
||||
{
|
||||
static int m_LogLevel = 1;
|
||||
static QString m_LogFile = QString("%1.log").arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmss"));
|
||||
QMutex m_LogMutex;
|
||||
|
||||
void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||
{
|
||||
if (type < m_LogLevel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
QString log_info;
|
||||
switch (type)
|
||||
{
|
||||
case QtDebugMsg:
|
||||
log_info = QString("%1:%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),msg);
|
||||
break;
|
||||
|
||||
case QtWarningMsg:
|
||||
log_info = QString("%1[Warning]:%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),msg);
|
||||
break;
|
||||
|
||||
case QtCriticalMsg:
|
||||
log_info = QString("%1[Critical]:%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),msg);
|
||||
break;
|
||||
|
||||
case QtFatalMsg:
|
||||
log_info = QString("%1[Fatal]:%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),msg);
|
||||
abort();
|
||||
}
|
||||
|
||||
m_LogMutex.lock();
|
||||
|
||||
QFile outFile(m_LogFile);
|
||||
outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
|
||||
QTextStream ts(&outFile);
|
||||
ts << log_info << endl;
|
||||
outFile.close();
|
||||
|
||||
m_LogMutex.unlock();
|
||||
}
|
||||
void logInit(QString logFile = "",int logLevel = 0)
|
||||
{
|
||||
|
||||
#ifndef DEBUG
|
||||
if ((logLevel < 0) || (logLevel > 3))
|
||||
{
|
||||
m_LogLevel = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_LogLevel = logLevel;
|
||||
}
|
||||
|
||||
if (!logFile.isEmpty())
|
||||
{
|
||||
m_LogFile = logFile+"/"+m_LogFile;
|
||||
}
|
||||
|
||||
qInstallMessageHandler(customMessageHandler);
|
||||
//qInstallMsgHandler(customMessageHandler);
|
||||
#endif
|
||||
}
|
||||
|
||||
//added by IRIS_ZZ initialize from main
|
||||
void ZZ_InitLogger(QString qstrDir)
|
||||
{
|
||||
QDir qdLogFolder;
|
||||
qdLogFolder.mkdir(qstrDir);
|
||||
qDebug() << QT_LOG::m_LogFile;
|
||||
QT_LOG::logInit(qstrDir);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
9
source/pch.h
Normal file
9
source/pch.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <QtCore/QCoreApplication>
|
||||
#include <QString>
|
||||
#include <QDebug>
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include <QDir>
|
||||
#include <QObject>
|
@ -1 +0,0 @@
|
||||
#pragma once
|
Reference in New Issue
Block a user