mirror of
http://172.16.0.230/r/SIF/TowerOptoSifAndSpectral.git
synced 2025-10-18 03:19:43 +08:00
592 lines
13 KiB
C++
592 lines
13 KiB
C++
#include "pch.h"
|
|
#include "ATPControl_Serial_QT.h"
|
|
#include "ZZ_Math_HDRONLY.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::Initialize(bool bIsUSBMode, ZZ_U8 ucPortNumber, std::string strDeviceName)
|
|
{
|
|
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;
|
|
}
|
|
|
|
void ZZ_ATPControl_Serial_Qt::Close()
|
|
{
|
|
m_pSerialPort->close();
|
|
}
|
|
int ZZ_ATPControl_Serial_Qt::GetDeviceInfo(DeviceInfo &Info)
|
|
{
|
|
QByteArray qbSend, qbRecv;
|
|
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(GET_PN_NUMBER);
|
|
int iRes = SendCommand(qbSend);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = RecvData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = ParseData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
m_diDeviceInfo.strPN = qbRecv.data();
|
|
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(GET_SN_NUMBER);
|
|
iRes = SendCommand(qbSend);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = RecvData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = ParseData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceInfo Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
m_diDeviceInfo.strSN = qbRecv.data();
|
|
|
|
|
|
Info = m_diDeviceInfo;
|
|
return 0;
|
|
}
|
|
|
|
int ZZ_ATPControl_Serial_Qt::GetDeviceAttribute(DeviceAttribute &Attr)
|
|
{
|
|
QByteArray qbSend, qbRecv;
|
|
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(GET_MIN_INTEGRATION_TIME);
|
|
int iRes = SendCommand(qbSend);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = RecvData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = ParseData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
m_daDeviceAttr.iMinIntegrationTimeInMS = (ZZ_U8)qbRecv[1] + (ZZ_U8)qbRecv[0] * 256;
|
|
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(GET_MAX_INTEGRATION_TIME);
|
|
iRes = SendCommand(qbSend);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = RecvData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = ParseData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
m_daDeviceAttr.iMaxIntegrationTimeInMS = (ZZ_U8)qbRecv[1] + (ZZ_U8)qbRecv[0] * 256;
|
|
|
|
|
|
iRes = SetExposureTime(m_daDeviceAttr.iMinIntegrationTimeInMS);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Call SetExposureTime error.Exit Code:2";
|
|
return 2;
|
|
}
|
|
iRes = SingleShot(m_daDeviceAttr.iPixels);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Call SingleShot error.Exit Code:3";
|
|
return 3;
|
|
}
|
|
|
|
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(GET_WAVELENGTH_CALIBRATION_COEF);
|
|
qbSend.resize(3);
|
|
qbSend[1] = 0x00;
|
|
qbSend[2] = 0x01;
|
|
iRes = SendCommand(qbSend);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = RecvData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
iRes = ParseData(qbRecv);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:GetDeviceAttribute Failed,Communication error.Exit Code:1";
|
|
return 1;
|
|
}
|
|
float fWaveLengthCoef[4];
|
|
memcpy(fWaveLengthCoef, qbRecv.data()+16, 4 * sizeof(float));
|
|
for (int i=0;i< m_daDeviceAttr.iPixels;i++)
|
|
{
|
|
m_daDeviceAttr.fWaveLengthInNM[i] = fWaveLengthCoef[0] * i*i*i + fWaveLengthCoef[1] * i*i + fWaveLengthCoef[2] * i + fWaveLengthCoef[3];
|
|
}
|
|
|
|
Attr = m_daDeviceAttr;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ZZ_ATPControl_Serial_Qt::SetDeviceTemperature(float fTemperature)
|
|
{
|
|
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"<< qi64Write;
|
|
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;
|
|
}
|
|
|
|
int ZZ_ATPControl_Serial_Qt::PerformAutoExposure(float fMinScaleFactor, float fMaxScaleFactor, float &fPredictedExposureTime)
|
|
{
|
|
using namespace ZZ_MATH;
|
|
int iDeviceDepth = 65535;
|
|
|
|
bool bFlagIsOverTrying = false;
|
|
bool bFlagIsLowerMinExposureTime = false;
|
|
bool bFlagIsOverMaxExposureTime = false;
|
|
bool bFlagIsAutoExposureOK = false;
|
|
bool bFlagIsAutoExposureFailed = false;
|
|
|
|
bool bIsValueOverflow = false;
|
|
bool bIsLastValueOverflow = false;
|
|
|
|
float fExposureTime = 0;
|
|
float fTempExposureTime = 0;
|
|
double fLastExposureTime = 0.1;
|
|
int iRepeatCount = 0;
|
|
|
|
int iRes = SetExposureTime(2000);//need change to load from files
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:PerformAutoExposure Failed.Exit Code:1";
|
|
return 1;
|
|
}
|
|
|
|
while (!bFlagIsAutoExposureOK && !bFlagIsAutoExposureFailed)
|
|
{
|
|
DataFrame dfTemp;
|
|
|
|
if (iRepeatCount++ > 30)
|
|
{
|
|
bFlagIsAutoExposureFailed = true;
|
|
bFlagIsOverTrying = true;
|
|
break;
|
|
}
|
|
|
|
fExposureTime = (float)m_daDeviceAttr.iMinIntegrationTimeInMS;
|
|
fTempExposureTime = fExposureTime;
|
|
|
|
iRes = SingleShot(dfTemp);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:PerformAutoExposure Failed.Exit Code:2";
|
|
return 2;
|
|
}
|
|
|
|
HeapSort(dfTemp.usData, m_daDeviceAttr.iPixels);
|
|
|
|
double dSum = 0;
|
|
int iCount = m_daDeviceAttr.iPixels / 100;
|
|
for (int i = 0; i < iCount; i++)
|
|
{
|
|
dSum += dfTemp.usData[i];
|
|
}
|
|
double dTemp = dSum / iCount;
|
|
|
|
if (dTemp >= iDeviceDepth * 0.99)
|
|
{
|
|
bIsValueOverflow = true;
|
|
if (!bIsLastValueOverflow)
|
|
{
|
|
fExposureTime = (float)(fLastExposureTime + fExposureTime) / 2;
|
|
}
|
|
else
|
|
{
|
|
fExposureTime = fExposureTime / 2;
|
|
}
|
|
}
|
|
|
|
else if (iDeviceDepth * fMaxScaleFactor >= dTemp && dTemp >= iDeviceDepth * fMinScaleFactor)
|
|
{
|
|
bFlagIsAutoExposureOK = 1;
|
|
}
|
|
else if (dTemp > iDeviceDepth * fMaxScaleFactor)
|
|
{
|
|
bIsValueOverflow = true;
|
|
if (!bIsLastValueOverflow)
|
|
{
|
|
fExposureTime = (float)(fLastExposureTime + fExposureTime) / 2;
|
|
}
|
|
else
|
|
{
|
|
fExposureTime = fExposureTime * 3 / 4;
|
|
}
|
|
}
|
|
else if (dTemp < iDeviceDepth * fMinScaleFactor)
|
|
{
|
|
bIsValueOverflow = false;
|
|
if (bIsLastValueOverflow)
|
|
{
|
|
fExposureTime = (float)(fLastExposureTime + fExposureTime) / 2;
|
|
}
|
|
else
|
|
{
|
|
double dFactor;
|
|
dFactor = dTemp / (iDeviceDepth * fMaxScaleFactor);
|
|
fExposureTime = (float)(fExposureTime / dFactor);
|
|
}
|
|
if (/*fExposureTime > 100 || */fExposureTime < 10)
|
|
{
|
|
bFlagIsAutoExposureOK = false;
|
|
bFlagIsAutoExposureFailed = true;
|
|
bFlagIsLowerMinExposureTime = true;
|
|
}
|
|
}
|
|
bIsLastValueOverflow = bIsValueOverflow;
|
|
fLastExposureTime = fTempExposureTime;
|
|
|
|
if (fExposureTime > 13000)
|
|
{
|
|
bFlagIsAutoExposureOK = false;
|
|
bFlagIsAutoExposureFailed = true;
|
|
fPredictedExposureTime = 13000;
|
|
iRes = SetExposureTime(13000);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:PerformAutoExposure Failed.Exit Code:3";
|
|
return 3;
|
|
}
|
|
bFlagIsOverMaxExposureTime = true;
|
|
break;
|
|
}
|
|
|
|
iRes = SetExposureTime((int)fExposureTime);
|
|
if (iRes != 0)
|
|
{
|
|
qDebug() << "Err:PerformAutoExposure Failed.Exit Code:4";
|
|
return 3;
|
|
}
|
|
}
|
|
fPredictedExposureTime = fExposureTime;
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
// int ZZ_ATPControl_Serial_Qt::SetExtShutter(int iShutterUP0, int iShutterDOWN1, int iShutterDOWN2, int iShutterDOWN3)
|
|
// {
|
|
// qDebug() << "stub code not implemented";
|
|
// return -1;
|
|
// }
|
|
|
|
int ZZ_ATPControl_Serial_Qt::SetExposureTime(int iExposureTimeInMS)
|
|
{
|
|
QByteArray qbExposureTime,qbRecv;
|
|
qbExposureTime.append(SET_INTEGRATION_TIME);
|
|
qbExposureTime.resize(3);
|
|
qbExposureTime[1] = iExposureTimeInMS >> 8;
|
|
qbExposureTime[2] = iExposureTimeInMS & 0xFF;
|
|
|
|
SendCommand(qbExposureTime);
|
|
RecvData(qbRecv);
|
|
ParseData(qbRecv);
|
|
|
|
if ((ZZ_U8)qbRecv[0] != 0)
|
|
{
|
|
qDebug() << "Err:SetExposureTime Failed.Exit Code:1" ;
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ZZ_ATPControl_Serial_Qt::GetExposureTime(int &iExposureTimeInMS)
|
|
{
|
|
QByteArray qbSend, qbRecv;
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(GET_INTEGRATION_TIME);
|
|
qbSend.resize(3);
|
|
qbSend[1] = 0x00;
|
|
qbSend[2] = 0x01;
|
|
SendCommand(qbSend);
|
|
RecvData(qbRecv);
|
|
ParseData(qbRecv);
|
|
return 0;
|
|
}
|
|
|
|
int ZZ_ATPControl_Serial_Qt::SingleShot(DataFrame &dfData)
|
|
{
|
|
QByteArray qbSend, qbRecv;
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(SYNC_GET_DATA);
|
|
qbSend.resize(3);
|
|
qbSend[1] = 0x00;
|
|
qbSend[2] = 0x01;
|
|
SendCommand(qbSend);
|
|
RecvData(qbRecv);
|
|
ParseData(qbRecv);
|
|
|
|
if ((ZZ_U8)qbRecv[0] != 0)
|
|
{
|
|
qDebug() << "Err:SingleShot Failed.Exit Code:1";
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
int iDataSizeInPixel = (qbRecv.size() - 1) / 2;
|
|
memcpy(dfData.usData, qbRecv.data() + 1, iDataSizeInPixel * 2);
|
|
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ZZ_ATPControl_Serial_Qt::SingleShot(int &iPixels)
|
|
{
|
|
QByteArray qbSend, qbRecv;
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(SYNC_GET_DATA);
|
|
qbSend.resize(3);
|
|
qbSend[1] = 0x00;
|
|
qbSend[2] = 0x01;
|
|
SendCommand(qbSend);
|
|
RecvData(qbRecv);
|
|
ParseData(qbRecv);
|
|
|
|
if ((ZZ_U8)qbRecv[0] != 0)
|
|
{
|
|
qDebug() << "Err:SingleShot Failed.Exit Code:1";
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
iPixels = (qbRecv.size() - 1) / 2;
|
|
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// int ZZ_ATPControl_Serial_Qt::SingleShotDark(ATPDataFrame &dfData)
|
|
// {
|
|
// SetExtShutter(0,0,0,0);
|
|
// SingleShot(dfData);
|
|
// return 0;
|
|
// }
|
|
|
|
int ZZ_ATPControl_Serial_Qt::GetDeviceTemperature(float &fTemperature)
|
|
{
|
|
QByteArray qbSend, qbRecv;
|
|
qbSend.clear();
|
|
qbRecv.clear();
|
|
qbSend.append(GET_TEC_TEMP);
|
|
qbSend.resize(3);
|
|
qbSend[1] = 0x00;
|
|
qbSend[2] = 0x01;
|
|
SendCommand(qbSend);
|
|
RecvData(qbRecv);
|
|
ParseData(qbRecv);
|
|
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;
|
|
// }
|