/
threadcomport.cpp
157 lines (139 loc) · 2.9 KB
/
threadcomport.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
threadcomport v.0.0.1
author Golubkin Egor (Gorin), Russia
///////////////////////////////////////////////
Multiplatform asynchronous Serial Port Extension
based on Wayne Roth's QExtSerialPort
*/
#include <threadcomport.h>
#include <qextserialport.h>
#include <QtDebug>
#include <QObject>
#include <QByteArray>
/*
==============
<CONSTRUCTORS>
==============
*/
Qthreadcomport::Qthreadcomport (const PortSettings *settings):
QextSerialPort(*settings)
{
thread = NULL;
}
ReceiveThread::ReceiveThread()
{
lastcount = 0;
}
QMainComThread::QMainComThread(QString name, const PortSettings *settings)
{
comsettings = new PortSettings();
*comsettings = *settings;
comname = name;
comport = NULL;
}
/*
==============
<DESTRSTRUCTORS>
==============
*/
Qthreadcomport::~Qthreadcomport()
{
stopThread();
}
QMainComThread::~QMainComThread()
{
terminate();
wait();
delete comport;
comport = NULL;
delete comsettings;
comsettings = NULL;
}
/*
==============
<METHODS>
==============
*/
/*TMainComThread*/
Qthreadcomport* QMainComThread::getPort()
{
return comport;
}
void QMainComThread::run()
{
Qthreadcomport *port = new Qthreadcomport(comsettings);
port->setPortName(comname);
comport = port;
exec();
}
/*QReceiveThread*/
void ReceiveThread::run()
{
int count;
forever
{
msleep(1);
mutex.lock();
count = comport->bytesAvailable();
mutex.unlock();
if (0 < count)
{
emit newDataInPortThread(count);
QTime timedb;
qDebug()<<"thread count= "<<count<<"time= "<<"\t"<<timedb.currentTime().second()<<" "<<timedb.currentTime().msec();
}
}
}
void ReceiveThread::setPort(Qthreadcomport *port)
{
comport = port;
}
/*Qthreadcomport*/
void Qthreadcomport::close()
{
stopThread();
QextSerialPort::close();
}
bool Qthreadcomport::open(QIODevice::OpenMode mode)
{
thread = new ReceiveThread();
thread->setPort(this);
connect(thread,SIGNAL(newDataInPortThread(int)),this,SLOT(newDataInPortSlot(int)));
bool bopen = QextSerialPort::open(mode);
thread->start();
return bopen;
}
qint64 Qthreadcomport::readData(char *data, qint64 maxSize)
{
qint64 count = QextSerialPort::readData(data, maxSize);
return count;
}
void Qthreadcomport::stopThread()
{
if (thread)
{
thread->terminate();
thread->wait();
delete thread;
thread = NULL;
}
}
/*
==============
<SLOTS>
==============
*/
void Qthreadcomport::newDataInPortSlot(int count)
{
if ((bytesAvailable() == 0) || (!(isOpen()))) return;
const int size = 4096;
char data[size];
int readcount = readData(data, count);
QTime timedb;
timedb = timedb.currentTime();
qDebug()<<"readData"<<"time= "<<"\t\t\t"<<timedb.second()<<" "<<timedb.msec();
data[readcount] = '\0';
unsigned char *dataPtr;
dataPtr = (unsigned char *)&data;
emit newDataInPortSignal(timedb, dataPtr,readcount);
}