273 lines
5.4 KiB
C++
273 lines
5.4 KiB
C++
#include "pch.h"
|
|
#include "IrisSensor_Gas_P0.h"
|
|
|
|
IrisSensor_Gas_P0::IrisSensor_Gas_P0()
|
|
{
|
|
m_pSerialPort = new QSerialPort;
|
|
m_iBaudRate = 115200;
|
|
}
|
|
|
|
IrisSensor_Gas_P0::~IrisSensor_Gas_P0()
|
|
{
|
|
delete m_pSerialPort;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::SendData_Chk(std::string sSend)
|
|
{
|
|
QByteArray qbaSend(sSend.c_str(), (int)sSend.length());
|
|
qint64 qi64Write = m_pSerialPort->write(qbaSend);
|
|
m_pSerialPort->waitForBytesWritten(200);
|
|
if (qi64Write != qbaSend.size())
|
|
{
|
|
qDebug() << "Err:Sensor_Gas write Failed.Exit Code:1" << qi64Write;
|
|
return qi64Write;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::RecvData_Chk(/*std::string sRecv*/)
|
|
{
|
|
QByteArray qbData;
|
|
qbData.clear();
|
|
qbData = m_pSerialPort->readAll();
|
|
|
|
int iCounter = 0;
|
|
while (qbData.size() < 3)
|
|
{
|
|
m_pSerialPort->waitForReadyRead(100);
|
|
QByteArray qbTemp = m_pSerialPort->readAll();
|
|
qbData.append(qbTemp);
|
|
|
|
if (iCounter > 3)
|
|
{
|
|
qDebug() << "Err:Sensor_Gas RecvData Failed,Not Enough Data.Exit Code:1" << qbData.size();
|
|
return 1;
|
|
}
|
|
iCounter++;
|
|
}
|
|
|
|
iCounter = 0;
|
|
if (qbData[0]==(char)0x06)
|
|
{
|
|
int iLength = qbData[2] + 3 + 1;
|
|
while (qbData.size()< iLength)
|
|
{
|
|
m_pSerialPort->waitForReadyRead(100);
|
|
qbData.append(m_pSerialPort->readAll());
|
|
iCounter++;
|
|
if (iCounter > 3)
|
|
{
|
|
qDebug() << "Err:Sensor_Gas RecvData Failed,Incomplete Data.Exit Code:2" << qbData.size();
|
|
return 3;
|
|
}
|
|
//return 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "Err:Sensor_Gas RecvData wrong header.Exit Code:2" << qbData.size();
|
|
}
|
|
|
|
m_sRecv.clear();
|
|
m_sRecv.resize(qbData.size());
|
|
|
|
for (int i=0;i< qbData.size();i++)
|
|
{
|
|
m_sRecv[i] = (unsigned char)qbData[i];
|
|
}
|
|
|
|
//QString qstrTest = m_sRecv.c_str();
|
|
//QString qstrTemp;
|
|
// qstrTemp.resize((int)m_sRecv.size());
|
|
// for (int i = 0; i < m_sRecv.size(); i++)
|
|
// {
|
|
// qstrTemp[i] = m_sRecv[i];
|
|
// }
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::ParseMeasuredData_Chk()
|
|
{
|
|
if (m_sRecv.size()!=0x1f+4)
|
|
{
|
|
qDebug() << "Err:Sensor_Gas ParseData Failed,Incorrect Data Length.Exit Code:1";
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
unsigned char uc12, uc13, uc14, uc15, uc16,uc17,uc18,uc19,uc28,uc29,uc30,uc31,uc32, uc33;
|
|
|
|
uc32 = m_sRecv[32];
|
|
uc33 = m_sRecv[33];
|
|
m_fTPTemperature = (uc32 * 256 + uc33) / 100;
|
|
|
|
uc30 = m_sRecv[30];
|
|
uc31 = m_sRecv[31];
|
|
m_fPB = (uc30 * 256 + uc31) / 10;
|
|
|
|
uc28 = m_sRecv[28];
|
|
uc29 = m_sRecv[29];
|
|
m_fPB = (uc28 * 256 + uc29) / 10;
|
|
|
|
uc16 = m_sRecv[16];
|
|
uc17 = m_sRecv[17];
|
|
uc18 = m_sRecv[18];
|
|
uc19 = m_sRecv[19];
|
|
m_ulCO2 = uc16*256*256*256 + uc17*256*256 + uc18*256 + uc19;
|
|
|
|
uc12 = m_sRecv[12];
|
|
uc13 = m_sRecv[13];
|
|
uc14 = m_sRecv[14];
|
|
uc15 = m_sRecv[15];
|
|
m_ulH2O = uc12 * 256 * 256 * 256 + uc13 * 256 * 256 + uc14 * 256 + uc15;
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::Initialize(std::string ucPortNumber)
|
|
{
|
|
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";
|
|
return 1;
|
|
}
|
|
|
|
bRes = m_pSerialPort->open(QIODevice::ReadWrite);
|
|
if (!bRes)
|
|
{
|
|
qDebug() << "Err:open Failed.Exit Code:2";
|
|
return 2;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::GetVersion()
|
|
{
|
|
QByteArray qbSend;
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x01);
|
|
qbSend.append(0x09);
|
|
qbSend.append((unsigned char)0xf4);
|
|
|
|
SendData_Chk(qbSend.toStdString());
|
|
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::GetMeasuredData(unsigned long &ulCO2, unsigned long &ulH2O, float &fTPTemperature, float &fPP, float &fPB)
|
|
{
|
|
QByteArray qbSend;
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x04);
|
|
qbSend.append(0x01);
|
|
qbSend.append(0x01);
|
|
qbSend.append(0x05);
|
|
qbSend.append((const char) 0x00);
|
|
qbSend.append((unsigned char)0xf3); //chksum
|
|
|
|
SendData_Chk(qbSend.toStdString());
|
|
|
|
RecvData_Chk();
|
|
|
|
ParseMeasuredData_Chk();
|
|
|
|
ulCO2 = m_ulCO2;
|
|
ulH2O = m_ulH2O;
|
|
fTPTemperature = m_fTPTemperature;
|
|
fPP = m_fPP;
|
|
fPB = m_fPB;
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::ZeroCalibration_N2()
|
|
{
|
|
QByteArray qbSend;
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x02);
|
|
qbSend.append((const char) 0x00);
|
|
qbSend.append((unsigned char)0xfa); //chksum
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::ZeroCalibration_Air()
|
|
{
|
|
QByteArray qbSend;
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x02);
|
|
qbSend.append((const char) 0x01);
|
|
qbSend.append((unsigned char)0xf9); //chksum
|
|
|
|
SendData_Chk(qbSend.toStdString());
|
|
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::SpanCalibration(char cChannel,unsigned int uiPPM)
|
|
{
|
|
unsigned char ucChksum=0x0;
|
|
QByteArray qbSend;
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x06);
|
|
qbSend.append(0x03);
|
|
qbSend.append(cChannel);
|
|
|
|
unsigned char *pResult = new unsigned char[4];
|
|
pResult[0] = (unsigned char)((uiPPM >> 24) & 0xFF);
|
|
pResult[1] = (unsigned char)((uiPPM >> 16) & 0xFF);
|
|
pResult[2] = (unsigned char)((uiPPM >> 8) & 0xFF);
|
|
pResult[3] = (unsigned char)(uiPPM & 0xFF);
|
|
|
|
for (int i=0;i<4;i++)
|
|
{
|
|
qbSend.append(pResult[i]);
|
|
}
|
|
|
|
for (int i=0;i<qbSend.size();i++)
|
|
{
|
|
ucChksum += qbSend[i];
|
|
}
|
|
ucChksum = ~ucChksum + 1;
|
|
|
|
qbSend.append(ucChksum); //chksum
|
|
|
|
delete[] pResult;
|
|
return 0;
|
|
}
|
|
|
|
int IrisSensor_Gas_P0::ResetCalibration(char cChannel)
|
|
{
|
|
unsigned char ucChksum = 0x0;
|
|
QByteArray qbSend;
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x02);
|
|
qbSend.append(0x04);
|
|
qbSend.append(cChannel);
|
|
|
|
for (int i = 0; i < qbSend.size(); i++)
|
|
{
|
|
ucChksum += qbSend[i];
|
|
}
|
|
ucChksum = ~ucChksum + 1;
|
|
|
|
qbSend.append(ucChksum);
|
|
|
|
|
|
|
|
return 0;
|
|
}
|