#include "pch.h"
#include "ATPControl_Serial_QT.h"
#include "ZZ_Math_HDRONLY.h"

ZZ_ATPControl_Serial_Qt::ZZ_ATPControl_Serial_Qt(QObject* parent /*= nullptr*/)
{
	m_pSerialPort = new QSerialPort;
	m_iBaudRate = 115200;
	//emit SignalInit_Self();
}

ZZ_ATPControl_Serial_Qt::~ZZ_ATPControl_Serial_Qt()
{
	if (m_pSerialPort != NULL)
	{
		delete m_pSerialPort;
	}

}

// int ZZ_ATPControl_Serial_Qt::ReInit()
// {
// 	m_pSerialPort->close();
// 	delete m_pSerialPort;
// 
// 	m_pSerialPort = new QSerialPort;
// 
// 	m_pSerialPort->setPortName("COM7");
// 	m_pSerialPort->setReadBufferSize(512);
// 	bool bRes = m_pSerialPort->setBaudRate(m_iBaudRate);
// 	bRes = m_pSerialPort->open(QIODevice::ReadWrite);
// }

// int ZZ_ATPControl_Serial_Qt::SetBaudRate(int iBaud)
// {
// 	m_iBaudRate = iBaud;
// 	return 0;
// }


int ZZ_ATPControl_Serial_Qt::Initialize(bool bIsUSBMode, std::string ucPortNumber, std::string strDeviceName)
{
	//connect(this, &ZZ_ATPControl_Serial_Qt::SignalInit_Self, this, &ZZ_ATPControl_Serial_Qt::Init_Self);
	//emit SignalInit_Self();

	QString qstrPortName = QString::fromStdString(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;
		printf("Err:setBaudRate Failed.Exit Code:1");
		return 1;
	}

	bRes = m_pSerialPort->open(QIODevice::ReadWrite);
	if (!bRes)
	{
		//qDebug() << "Err:open Failed.Exit Code:2";
		//std::cout << "Err.open Failed" << std::endl;
		printf("Err:open Failed.Exit Code:2");
		return 2;
	}

	// 	int testi;
	// 	GetDeviceAttribute(m_daDeviceAttr);
	// 	GetExposureTime(testi);
	// 	SetExposureTime(10000);
	// 	DataFrame test;
	// 	SingleShot(test);

	GetDeviceInfo(m_diDeviceInfo);
	GetExposureTime_Init();

	std::string::size_type szPostion = m_diDeviceInfo.strSN.find(strDeviceName);
	if (szPostion == std::string::npos)
	{
		printf("Err:FS serial number not match.Exit Code:3");
		//qDebug() << "Err:FS serial number not match.Exit Code:3";
		//return 3;
	}
	else
	{
		return 0;
	}
	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";
		printf("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";
		printf("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";
		printf("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";
		printf("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";
		printf("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";
		printf("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";
		printf("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";
		printf("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";
		printf("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";
		printf("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";
		printf("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";
		printf("Err:GetDeviceAttribute Failed,Communication error.Exit Code:1");
		return 1;
	}
	m_daDeviceAttr.iMaxIntegrationTimeInMS = (ZZ_U8)qbRecv[1] + (ZZ_U8)qbRecv[0] * 256;

	/// 
	int iTempExpTime = 0;
	GetExposureTime(iTempExpTime);

	iRes = SetExposureTime(m_daDeviceAttr.iMinIntegrationTimeInMS);
	if (iRes != 0)
	{
		//qDebug() << "Err:GetDeviceAttribute Failed,Call SetExposureTime error.Exit Code:2";
		printf("Err:GetDeviceAttribute Failed,Communication error.Exit Code:2");
		//return 2;
	}
	iRes = SingleShot(m_daDeviceAttr.iPixels);
	if (iRes != 0)
	{
		//qDebug() << "Err:GetDeviceAttribute Failed,Call SingleShot error.Exit Code:3";
		printf("Err:GetDeviceAttribute Failed,Communication error.Exit Code:3");
		return 3;
	}

	SetExposureTime(iTempExpTime);
	/// 
	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";
		printf("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";
		printf("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";
		printf("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::GetExposureTime_Init()
{
	QByteArray qbSend, qbRecv;
	qbSend.clear();
	qbRecv.clear();
	qbSend.append(GET_INTEGRATION_TIME);
	qbSend.resize(3);
	qbSend[1] = 0x00;
	qbSend[2] = 0x01;
	int iRes = SendCommand(qbSend);
	if (iRes != 0)
	{
		qDebug() << "Err:GetExposureTime Failed.Exit Code:1";
		return 1;
	}
	iRes = RecvData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:GetExposureTime Failed.Exit Code:2";
		return 2;
	}
	iRes = ParseData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:GetExposureTime Failed.Exit Code:3";
		return 3;
	}

	m_iExposureTime = (ZZ_U8)qbRecv[1] + (ZZ_U8)qbRecv[0] * 256;
	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(1000);
		QByteArray qbTemp = m_pSerialPort->readAll();
		qbData.append(qbTemp);

		if (iCounter > 150)
		{
			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(5000);
		qbData.append(m_pSerialPort->readAll());

		if (iCounter > 200)
		{
			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)
	{
		/*char cTemp[5];
		cTemp[0] = qbData[1026];
		qDebug()<< cTemp[0];
		cTemp[1] = qbData[1027];
		qDebug()<< cTemp[1];
		cTemp[2] = qbData[1028];
		qDebug()<< cTemp[2];
		cTemp[3] = qbData[1029];
		qDebug()<< cTemp[3];
		cTemp[4] = qbData[1030];
		qDebug()<< cTemp[4];*/

		qDebug() << "Err:RecvData Failed,Incorrect Check Sum.Exit Code:4" << "Total Recv:" << qbData.size() << "Check Sum:" << usCheckSum << "Not Equal To" << ucTemp;
		//qbData.clear();
		//return 4;
		return 0;
	}

	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::Init_Self()
{
	m_pSerialPort = new QSerialPort;
	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.lData, m_daDeviceAttr.iPixels);

		double dSum = 0;
		int iCount = m_daDeviceAttr.iPixels / 100;
		for (int i = 0; i < iCount; i++)
		{
			dSum += dfTemp.lData[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)
{
	m_iExposureTime = iExposureTimeInMS;

	QByteArray qbExposureTime, qbRecv;
	//qbExposureTime.append(SET_INTEGRATION_TIME);
	qbExposureTime.resize(3);
	qbExposureTime[0] = SET_INTEGRATION_TIME;
	qbExposureTime[1] = iExposureTimeInMS >> 8;
	qbExposureTime[2] = iExposureTimeInMS & 0xFF;

	int iRes = SendCommand(qbExposureTime);
	if (iRes != 0)
	{
		qDebug() << "Err:SetExposureTime Failed.Exit Code:2";
		return 2;
	}
	iRes = RecvData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:SetExposureTime Failed.Exit Code:3";
		return 3;
	}
	iRes = ParseData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:SetExposureTime Failed.Exit Code:4";
		return 4;
	}

	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;
	// 	int iRes = SendCommand(qbSend);
	// 	if (iRes != 0)
	// 	{
	// 		qDebug() << "Err:GetExposureTime Failed.Exit Code:1";
	// 		return 1;
	// 	}
	// 	iRes = RecvData(qbRecv);
	// 	if (iRes != 0)
	// 	{
	// 		qDebug() << "Err:GetExposureTime Failed.Exit Code:2";
	// 		return 2;
	// 	}
	// 	iRes = ParseData(qbRecv);
	// 	if (iRes != 0)
	// 	{
	// 		qDebug() << "Err:GetExposureTime Failed.Exit Code:3";
	// 		return 3;
	// 	}
	// 
	// 	iExposureTimeInMS = (ZZ_U8)qbRecv[1] + (ZZ_U8)qbRecv[0] * 256;
	iExposureTimeInMS = m_iExposureTime;
	return 0;
}

int ZZ_ATPControl_Serial_Qt::SingleShot(DataFrame& dfData)
{
	dfData.usExposureTimeInMS = m_iExposureTime;

	QByteArray qbSend, qbRecv;
	qbSend.clear();
	qbRecv.clear();
	qbSend.append(SYNC_GET_DATA);
	qbSend.resize(3);
	// 	qbSend[1] = 0x00;
	// 	qbSend[2] = 0x01;
	qbSend[1] = m_iExposureTime >> 8;
	qbSend[2] = m_iExposureTime & 0xFF;
	int iRes = SendCommand(qbSend);
	if (iRes != 0)
	{
		qDebug() << "Err:SingleShot Failed.Exit Code:1";
		return 1;
	}
	iRes = RecvData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:SingleShot Failed.Exit Code:2";
		return 2;
	}
	iRes = ParseData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:SingleShot Failed.Exit Code:3";
		return 3;
	}

	ZZ_U16 usData[4096] = { 0 };

	if ((ZZ_U8)qbRecv[0] != 0)
	{
		qDebug() << "Err:SingleShot Failed.Exit Code:1";
		return 1;
	}
	else
	{
		//int aaa = qbRecv.size();
		int iDataSizeInPixel = (qbRecv.size() - 1) / 2;
		memcpy(usData, qbRecv.data() + 1, iDataSizeInPixel * 2);
		for (size_t i = 0; i < iDataSizeInPixel; i++)
		{
			dfData.lData[i] = qToBigEndian(usData[i]);
		}
		// 		for (int i = 0; i < iDataSizeInPixel; i++)
		// 		{
		// 			dfData.lData[i] = usData[i];
		// 		}
	}

	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;
	int iRes = SendCommand(qbSend);
	if (iRes != 0)
	{
		qDebug() << "Err:SingleShotP Failed.Exit Code:1";
		return 1;
	}
	iRes = RecvData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:SingleShotP Failed.Exit Code:2";
		return 2;
	}
	iRes = ParseData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:SingleShot Failed.Exit Code:3";
		return 3;
	}

	if ((ZZ_U8)qbRecv[0] != 0)
	{
		qDebug() << "Err:SingleShotP Failed.Exit Code:4";
		return 4;
	}
	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)
{
	fTemperature = 0;

	QByteArray qbSend, qbRecv;
	qbSend.clear();
	qbRecv.clear();
	qbSend.append(GET_TEC_TEMP);
	qbSend.resize(3);
	qbSend[1] = 0x00;
	qbSend[2] = 0x01;
	int iRes = SendCommand(qbSend);
	if (iRes != 0)
	{
		qDebug() << "Err:GetDeviceTemperature Failed.Exit Code:1";
		return 1;
	}
	iRes = RecvData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:GetDeviceTemperature Failed.Exit Code:2";
		return 2;
	}
	iRes = ParseData(qbRecv);
	if (iRes != 0)
	{
		qDebug() << "Err:GetDeviceTemperature Failed.Exit Code:3";
		return 3;
	}

	QString qstrTemp = qbRecv.data();
	fTemperature = qstrTemp.toFloat();

	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;
	// }
