From 617a16002f842e932bc9ac998ca03da7a6c85e1b Mon Sep 17 00:00:00 2001 From: zhangzhuo Date: Wed, 3 Nov 2021 09:39:17 +0800 Subject: [PATCH] new by zz --- CmakeLists.txt | 9 +- main.cpp | 21 ++- source/ATP/ATPControl_Serial_QT.cpp | 201 +++++++++++++++++++++++++++- source/ATP/ATPControl_Serial_QT.h | 32 ++++- source/ATP/ZZ_Types.h | 47 ++++--- source/Logger/Logger.h | 90 +++++++++++++ source/pch.h | 9 ++ source/test/test.cpp | 0 source/test/test.h | 1 - 9 files changed, 375 insertions(+), 35 deletions(-) create mode 100644 source/Logger/Logger.h create mode 100644 source/pch.h delete mode 100644 source/test/test.cpp delete mode 100644 source/test/test.h diff --git a/CmakeLists.txt b/CmakeLists.txt index 0d49eab..0f111c7 100644 --- a/CmakeLists.txt +++ b/CmakeLists.txt @@ -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") \ No newline at end of file diff --git a/main.cpp b/main.cpp index 3d5f3bc..dc67308 100644 --- a/main.cpp +++ b/main.cpp @@ -1,17 +1,24 @@ -#include -#include -#include +#include "pch.h" #include "ATPControl_Serial_QT.h" -#include +#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(); } diff --git a/source/ATP/ATPControl_Serial_QT.cpp b/source/ATP/ATPControl_Serial_QT.cpp index 6ff2311..fe86fb5 100644 --- a/source/ATP/ATPControl_Serial_QT.cpp +++ b/source/ATP/ATPControl_Serial_QT.cpp @@ -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"<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; + // } diff --git a/source/ATP/ATPControl_Serial_QT.h b/source/ATP/ATPControl_Serial_QT.h index 4d11ee7..9a648c1 100644 --- a/source/ATP/ATPControl_Serial_QT.h +++ b/source/ATP/ATPControl_Serial_QT.h @@ -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(); }; \ No newline at end of file diff --git a/source/ATP/ZZ_Types.h b/source/ATP/ZZ_Types.h index e525b3b..1d50280 100644 --- a/source/ATP/ZZ_Types.h +++ b/source/ATP/ZZ_Types.h @@ -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; } } diff --git a/source/Logger/Logger.h b/source/Logger/Logger.h new file mode 100644 index 0000000..cfbcf02 --- /dev/null +++ b/source/Logger/Logger.h @@ -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 +#include +#include +#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); + } +}; + + + diff --git a/source/pch.h b/source/pch.h new file mode 100644 index 0000000..dac58d0 --- /dev/null +++ b/source/pch.h @@ -0,0 +1,9 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include \ No newline at end of file diff --git a/source/test/test.cpp b/source/test/test.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/source/test/test.h b/source/test/test.h deleted file mode 100644 index 6f70f09..0000000 --- a/source/test/test.h +++ /dev/null @@ -1 +0,0 @@ -#pragma once