-
Notifications
You must be signed in to change notification settings - Fork 25
/
datasreceivethread.cpp
63 lines (54 loc) · 1.4 KB
/
datasreceivethread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "datasreceivethread.h"
#include "abstractmodbusdevice.h"
#include "databuffer.h"
#include "./Global/algorithm.h"
#include "./Global/global.h"
DatasReceiveThread::DatasReceiveThread(QObject *parent) :
QThread(parent)
{
n_maxSize = 0;
m_pModbusDevice = NULL;
m_pDataBuf = NULL;
m_nTimeoutMS = 10;
}
DatasReceiveThread::~DatasReceiveThread()
{
}
void DatasReceiveThread::setModbusDevice(AbstractModbusDevice *pDevice)
{
m_pModbusDevice = pDevice;
}
void DatasReceiveThread::setDataBuffer(DataBuffer *pBuf)
{
m_pDataBuf = pBuf;
}
void DatasReceiveThread::run()
{
DataInfo info;
info.bSend = false;
//接收数据
m_bRunning = true;
char buf[1024] = {0};
while (m_bRunning)
{
int temp = m_pModbusDevice->recvData(buf, 1024);
if (0 == temp)
{
//加判断,跟前一包的数据的时间对比,大于一定间隔直接发送,小于合并发送
if (info.data.count() > 0
&& info.time.msecsTo(QDateTime::currentDateTime()) > m_nTimeoutMS)
{
m_pDataBuf->appendFrame(info);
info.data.clear();
}
msleep(1);
continue;
}
if (info.data.count() == 0)
{
info.time = QDateTime::currentDateTime();
}
info.data.append(QByteArray(buf, temp));
}
m_bRunning = false;
}