You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wheeldetect/src/RasterSDG20/Analysis/RasterAnalysis.cpp

268 lines
5.0 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "Rasteranalysis.h"
QRasterAnalysis::QRasterAnalysis(QObject *parent)
: QObject(parent), m_tsk(true), _initdone(false)
{
this->requestData.clear();
analysisTsk = new QMyThread(this);
analysisTsk->setUser(true);
analysisTsk->loadfunc(this, &QRasterAnalysis::analysis_task);
analysisTsk->start();
}
QRasterAnalysis::~QRasterAnalysis()
{
if (analysisTsk)
{
if (analysisTsk->isRunning())
{
m_tsk = false;
analysisTsk->stop();
analysisTsk->quit();
analysisTsk->wait();
}
}
}
/*初始化*/
void QRasterAnalysis::init(bool AutoEcho /*= false*/, bool LogData /*= false*/)
{
if (_initdone)
{
return;
}
_AutoEcho = AutoEcho;
_LogData = LogData;
_initdone = true;
}
/*数据打包和发送*/
void QRasterAnalysis::SendFrame(Raster_ComFrame m_ComFrame)
{
quint8 *pTxContent, *checksum;
quint16 TxLen;
quint16 crc_sum;
if (!_initdone)
{
init();
}
pTxContent = (quint8 *)malloc(BUFFLEN);
TxLen = 0;
pTxContent[TxLen++] = 0xCA;
pTxContent[TxLen++] = 0x01;
pTxContent[TxLen++] = m_ComFrame.cmd >> 8;
pTxContent[TxLen++] = m_ComFrame.cmd & 0xFF;
pTxContent[TxLen++] = m_ComFrame.len >> 8;
pTxContent[TxLen++] = m_ComFrame.len & 0xFF;
if (m_ComFrame.len)
{
memcpy(&pTxContent[TxLen], m_ComFrame.pData, m_ComFrame.len);
TxLen += m_ComFrame.len;
}
crc_sum = Get_Crc8(&pTxContent[2], TxLen - 2);
pTxContent[TxLen++] = crc_sum >> 8;
pTxContent[TxLen++] = crc_sum & 0xFF;
sendData2COM(TxLen, pTxContent);//进一步打包发送的数据
free(pTxContent);
}
/*把数据打包好以信号的形式发送出去*/
void QRasterAnalysis::sendData2COM(int size, quint8 *data)
{
QByteArray m_data;
for (int i = 0; i < size; i++)
{
m_data.append(data[i]);
}
emit (pullData2COM(m_data));//把数据打包到信号里面发送出去
}
/*数据接收槽函数*/
void QRasterAnalysis::recvDataByCOM(QByteArray data)
{
mMtLock.lock();
requestData.append(data);
mMtLock.unlock();
}
/*数据解析线程 在该类创建的时候就启动 用于解析接收到的数据*/
void QRasterAnalysis::analysis_task(void)
{
quint8 RxDataIndex;
quint8 RxIndex = 0;
quint16 RxTimeOut = 0;
quint16 RxSum, CalSum;
quint16 RxLen;
quint8 *pRxContent;
Raster_ComFrame m_RxFrame;
pRxContent = (quint8 *)malloc(BUFFLEN);
RxDataIndex = 0;
RxIndex = 0;
CalSum = 0;
while (m_tsk)
{
if (RxTimeOut != 0)
{
RxTimeOut--;
}
else
{
//接收超时,重置本次接收
RxIndex = 0;
RxDataIndex = 0;
CalSum = 0;
}
quint8 recvdatatemp = 0;
while (this->takeData(recvdatatemp, 1))
{
RxTimeOut = RX_TIMEOUT;
switch (RxIndex)
{
case RX_HEAD1:
{
if (recvdatatemp == 0xF4)
{
RxIndex++;
RxDataIndex = 0;
pRxContent[RxDataIndex++] = recvdatatemp;
}
}
break;
case RX_DEVICE:
m_RxFrame.adr = recvdatatemp;
RxIndex++;
pRxContent[RxDataIndex++] = recvdatatemp;
break;
case RX_CMD:
RxIndex++;
pRxContent[RxDataIndex++] = recvdatatemp;
m_RxFrame.cmd = recvdatatemp;
break;
//-----------------------以上为固定协议头部分------------------------
case RX_LEN1:
{
pRxContent[RxDataIndex++] = recvdatatemp;
RxIndex++;
m_RxFrame.len = recvdatatemp;
if (m_RxFrame.len == 0)//当数据长度为0时直接跳转至接收校验码
{
RxIndex++;
}
else
{
RxLen = m_RxFrame.len;
}
}
break;
case RX_DATA:
{
pRxContent[RxDataIndex++] = recvdatatemp;
if (!(--RxLen))
{
RxIndex++;
}
}
break;
case RX_SUM1:
{
RxSum = recvdatatemp << 8;
RxIndex++;
}
break;
case RX_SUM2:
{
RxSum += recvdatatemp;
CalSum = Get_Crc8(pRxContent, (m_RxFrame.len + CMDLEN_LEN));
if (RxSum == CalSum)
{
m_RxFrame.pData = pRxContent + CMDLEN_LEN;/*数据校验正确 将执行用户定义的解析函数*/
if (_analysiseupdateFun)
_analysiseupdateFun(m_RxFrame);
else
{
int a = 0;
}
RxTimeOut = 0;//
RxIndex = 0;
RxDataIndex = 0;
}
else
{
RxIndex = 0;
RxDataIndex = 0;
CalSum = 0;
RxTimeOut = 0;
}
break;
}
default:
{
RxIndex = 0;
RxDataIndex = 0;
CalSum = 0;
RxTimeOut = 0;
}
break;
}
}
analysisTsk->msleep(10);
}
if (NULL != pRxContent){
free(pRxContent);
pRxContent = NULL;
}
}
/*队列中取出数据*/
bool QRasterAnalysis::takeData(quint8 &data, int nLen /*= 1*/)
{
mMtLock.lock();
bool bFlag = false;
if (nLen <= getRxDataSize()) {
QByteArray m_temp = requestData.left(nLen);
char *temp = m_temp.data();
quint8 m_data = *temp;
data = *temp;
requestData.remove(0, nLen);
bFlag = true;
}
mMtLock.unlock();
return bFlag;
}
/*获取接收fifo中的大小*/
int QRasterAnalysis::getRxDataSize(void)
{
return this->requestData.size();
}
/*获取接收fifo中的数据*/
QByteArray QRasterAnalysis::getRxBufData(void)
{
return requestData;
}
/*清空fifo中的数据*/
bool QRasterAnalysis::cleanRxDataBuf(void)
{
requestData.clear();
return true;
}
/*CRC8校验
ptr:要校验的数组
len:数组长度
返回值:CRC8码*/
quint16 Get_Crc8(quint8 *ptr, quint16 len)
{
quint16 crc=0;
quint16 sum=0;
while (len--)
{
crc = crc + (*ptr++);
}
crc = (0xFFFF - crc);
sum = crc >> 8;
return (sum+(crc << 8));
}