forked from iGNSS/Qt_MultiThread_SerialPort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialworker.cpp
65 lines (54 loc) · 1.41 KB
/
serialworker.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
63
64
65
#include "serialworker.h"
#include <QDebug>
#include <QThread>
#include <QTime>
#include <QApplication>
//第三方库
//SerialWorker::SerialWorker(QextSerialPort *ser,QObject *parent)
// : QObject(parent),serial(ser)
//{
//}
SerialWorker::SerialWorker(QSerialPort *ser,QObject *parent)
: QObject(parent),serial(ser)
{
}
void SerialWorker::doDataSendWork(const QByteArray data)
{
//qDebug() << "子线程槽函数发送数据:" << data << "线程ID:" << QThread::currentThreadId();
// 发送数据
serial->write(data);
}
//#ifdef SERIALPORT
//void SerialWorker::doDataSendWork(const QByteArray data)
//{
// // 发送数据
// serial->write(data);
//}
//#endif
//}
void SerialWorker::mySleep(int sec)
{
QTime dieTime = QTime::currentTime().addMSecs(sec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void SerialWorker::doDataReciveWork()
{
if(serial !=NULL)
{
if(serial->bytesAvailable()>0)
{
QByteArray buffertemp = serial->readAll();
buffertemp = buffertemp.toHex();
emit sendResultToGui(buffertemp);
}
else if(serial->bytesAvailable()==0)
{
qDebug()<<"Serial port do not open";
}
else if(serial->bytesAvailable()<0)
{
qDebug()<<"Serial port error";
}
}
}