第一次提交
This commit is contained in:
156
Source/IrisSensor_WDA_P0.cpp
Normal file
156
Source/IrisSensor_WDA_P0.cpp
Normal file
@ -0,0 +1,156 @@
|
||||
#include "pch.h"
|
||||
#include "IrisSensor_WDA_P0.h"
|
||||
|
||||
IrisSensor_WDA_P0::IrisSensor_WDA_P0()
|
||||
{
|
||||
m_pSerialPort = new QSerialPort;
|
||||
m_iBaudRate = 9600;
|
||||
return;
|
||||
}
|
||||
|
||||
IrisSensor_WDA_P0::~IrisSensor_WDA_P0()
|
||||
{
|
||||
delete m_pSerialPort;
|
||||
return;
|
||||
}
|
||||
|
||||
int IrisSensor_WDA_P0::SendData_NChk(std::string sSend)
|
||||
{
|
||||
//std::string sSendLinux = sSend;
|
||||
//std::replace(sSendLinux.begin(), sSendLinux.end(), '\r', '\n');
|
||||
|
||||
QByteArray qbaSend(sSend.c_str(), (int)sSend.length());
|
||||
qint64 qi64Write = m_pSerialPort->write(qbaSend);
|
||||
#ifdef ZZ_FLAG_TEST
|
||||
// int a1 = qbaSend[10];
|
||||
// int a2 = qbaSend[11];
|
||||
// qDebug() << qbaSend[0]<< qbaSend[1]<<qbaSend[2] << qbaSend[3] << qbaSend[4] << qbaSend[5] << qbaSend[6] << qbaSend[7] << qbaSend[8] << qbaSend[9] /*<< qbaSend[10] << (char)qbaSend[11] */;
|
||||
// qDebug() << a1 << a2;
|
||||
#endif
|
||||
m_pSerialPort->waitForBytesWritten(50);
|
||||
if (qi64Write != qbaSend.size())
|
||||
{
|
||||
qDebug() << "Err:Sensor_WDA write Failed.Exit Code:1" << qi64Write;
|
||||
return qi64Write;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IrisSensor_WDA_P0::RecvData_NChk(/*std::string sRecv*/)
|
||||
{
|
||||
QByteArray qbData;
|
||||
qbData.clear();
|
||||
qbData = m_pSerialPort->readAll();
|
||||
|
||||
int iCounter = 0;
|
||||
while (qbData.size()<8)
|
||||
{
|
||||
m_pSerialPort->waitForReadyRead(10);
|
||||
QByteArray qbTemp = m_pSerialPort->readAll();
|
||||
qbData.append(qbTemp);
|
||||
|
||||
if (iCounter > 3)
|
||||
{
|
||||
qDebug() << "Err:Sensor_WDA RecvData Failed,Not Enough Data.Exit Code:1" << qbData.size();
|
||||
return 1;
|
||||
}
|
||||
iCounter++;
|
||||
}
|
||||
|
||||
iCounter = 0;
|
||||
while ((char)(qbData[qbData.size()-2]) != 0x0D && (char)(qbData[qbData.size() - 2]) != 0x0A)
|
||||
{
|
||||
m_pSerialPort->waitForReadyRead(50);
|
||||
qbData.append(m_pSerialPort->readAll());
|
||||
iCounter++;
|
||||
if (iCounter > 3)
|
||||
{
|
||||
qDebug() << "Err:Sensor_WDA RecvData Failed,Incomplete Data.Exit Code:2" << qbData.size();
|
||||
return 3;
|
||||
}
|
||||
//return 0;
|
||||
}
|
||||
|
||||
//m_sRecv.clear();
|
||||
//m_sRecv.assign(qbData.constData());
|
||||
|
||||
m_sRecv.clear();
|
||||
m_sRecv.resize(qbData.size());
|
||||
for (int i = 0; i < qbData.size(); i++)
|
||||
{
|
||||
m_sRecv[i] = (unsigned char)qbData[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IrisSensor_WDA_P0::ParseData_NChk()
|
||||
{
|
||||
//QString qstrTemp(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];
|
||||
}
|
||||
///////////////////////
|
||||
int iPos = qstrTemp.indexOf("=");
|
||||
int iPos1 = qstrTemp.lastIndexOf(",");
|
||||
qstrTemp = qstrTemp.left(iPos1);
|
||||
qstrTemp = qstrTemp.mid(iPos+1);
|
||||
|
||||
QString qstrSpeed, qstrDirection;
|
||||
iPos = qstrTemp.indexOf(",");
|
||||
qstrSpeed = qstrTemp.left(iPos);
|
||||
qstrDirection = qstrTemp.mid(iPos+1);
|
||||
|
||||
m_fWindSpeed = qstrSpeed.toFloat();
|
||||
m_fWindDirection = qstrDirection.toFloat();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IrisSensor_WDA_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_WDA_P0::GetSA_NChk(float &fSpeed, float &fAngle)
|
||||
{
|
||||
if (SendData_NChk("$01,WV?*//\r\n")!=0)
|
||||
{
|
||||
qDebug() << "Err:GetSA_NChk - SendData_NChk.Exit Code:1";
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (RecvData_NChk() != 0)
|
||||
{
|
||||
qDebug() << "Err:RecvData_NChk - SendData_NChk.Exit Code:1";
|
||||
return 2;
|
||||
}
|
||||
|
||||
ParseData_NChk();
|
||||
|
||||
fSpeed = m_fWindSpeed;
|
||||
fAngle = m_fWindDirection;
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user