-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqmyconsole.cpp
407 lines (390 loc) · 11.9 KB
/
qmyconsole.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <stdio.h>
#include "qmyconsole.h"
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QClipboard>
#include <QApplication>
#include <QByteArray>
#include <QMutexLocker>
#include <QMessageBox>
#include <QDebug>
#include <QSettings>
#include <QTime>
QT_USE_NAMESPACE
QMyconsole *QMyconsole::MsgHandler=NULL;
QMyconsole::QMyconsole(QObject *parent) :
QObject(parent)
{
playState=false;
sIniFilePath ="config.ini";
mediaPlayer.setMedia (QUrl::fromLocalFile("Sound/Nyan Cat.mp3"));
openFlag=true;
messageRequ="";
waitTimeOut=1000;
m_period=0;
sIniSetting=new QSettings(sIniFilePath,QSettings::IniFormat);
sIniSetting->setIniCodec ("UTF-8");
if(!sIniSetting->value ("Init/LanuchTime",0).toInt ())
{
sIniSetting->setValue ("Init/LanuchTime",1);
sIniSetting->setValue ("Widget/Lefft",true);
sIniSetting->setValue ("Widget/Bottom",true);
sIniSetting->setValue ("Widget/Right",true);
sIniSetting->setValue ("Widget/Neko",true);
}
enablePeriod=false;
QMyconsole::MsgHandler=this;
board=QApplication::clipboard();
board->setText(NULL,QClipboard::Clipboard);
port = new QSerialPort();
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{port->setPortName (serialPortInfo.portName ());
combusy=serialPortInfo.isBusy ();}
port->open (QIODevice::ReadWrite);
}
void QMyconsole::toClipboard (QString text)
{
board->clear ();
QByteArray ba=text.toLatin1 ();
const char *c_str2=ba.data ();
board->setText(c_str2,QClipboard::Clipboard);
}
void QMyconsole::clearclipboard ()
{
board->clear ();
}
void QMyconsole::playFile ()
{
playState=!playState;
if(playState)mediaPlayer.play ();
else mediaPlayer.pause();
}
void QMyconsole::nNumbe ()
{
QString text;
text= QString("Number of serial ports:%1\n").
arg(QSerialPortInfo::availablePorts().count());
text.append (board->text ().toLatin1 ());
toClipboard (text);
}
void QMyconsole::closeWidget (int which)
{
switch (which) {
case 0:
sIniSetting->setValue ("Widget/Lefft",false);
break;
case 1:
sIniSetting->setValue ("Widget/Bottom",false);
break;
case 2:
sIniSetting->setValue ("Widget/Right",false);
break;
case 3:
sIniSetting->setValue ("Widget/Neko",false);
break;
default:
break;
}
}
bool QMyconsole::enableWidget (int which)
{
switch(which){
case 0:
return sIniSetting->value ("Widget/Lefft",false).toBool ();
case 1:
return sIniSetting->value ("Widget/Bottom",false).toBool ();
case 2:
return sIniSetting->value ("Widget/Right",false).toBool ();
case 3:
return sIniSetting->value ("Widget/Neko",false).toBool ();
default:
return false;
}
}
void QMyconsole::setBaud_com (QString comname)
{
QByteArray ba=comname.toLatin1 ();
qint32 c_str2=ba.toInt ();
port->setBaudRate (c_str2,QSerialPort:: AllDirections);
}
void QMyconsole::setCom (QString comname)
{
port->close ();
port->setPortName (comname);
if(!port->open (QIODevice::ReadWrite)){
toClipboard (QString("Can't open %1, error code %2\n\n")
.arg(port->portName ()).arg(port->error()));
}
else{
toClipboard (QString("The %1 has opened.\n\n")
.arg(port->portName ()));
}
}
void QMyconsole::setParity_com (int value)
{
if(value!=0)value=value+1;
switch (value) {
case 0:
port->setParity (QSerialPort::NoParity);
break;
case 2:
port->setParity (QSerialPort::EvenParity);
break;
case 3:
port->setParity (QSerialPort::OddParity);
break;
case 4:
port->setParity (QSerialPort::SpaceParity);
break;
case 5:
port->setParity (QSerialPort::MarkParity);
break;
default:
break;
}
}
void QMyconsole::setDatabit_com (int datebits)
{
switch (datebits) {
case 0:
port->setDataBits (QSerialPort::Data5);
break;
case 1:
port->setDataBits (QSerialPort::Data6);
break;
case 2:
port->setDataBits (QSerialPort::Data7);
break;
case 3:
port->setDataBits (QSerialPort::Data8);
break;
default:
break;
}
}
void QMyconsole::setStopbit_com (int stopbit)
{
switch (stopbit) {
case 0:
port->setStopBits (QSerialPort::OneStop);
break;
case 1:
port->setStopBits (QSerialPort::OneAndHalfStop);
break;
case 2:
port->setStopBits (QSerialPort::TwoStop);
break;
default:
break;
}
}
void QMyconsole::setFlowControl_com (int fc)
{
switch (fc) {
case 0:
port->setFlowControl (QSerialPort::NoFlowControl);
break;
case 1:
port->setFlowControl (QSerialPort::HardwareControl);
break;
case 2:
port->setFlowControl (QSerialPort::SoftwareControl);
break;
default:
break;
}
}
void QMyconsole::setWaitOutTime (QString time)
{
waitTimeOut=time.toInt ();
}
void QMyconsole::setPeriod (QString time)
{
m_period=time.toInt ();
}
void QMyconsole::enPeriod (bool states)
{
enablePeriod=states;
}
bool QMyconsole::busy_com ()
{
return combusy;
}
bool QMyconsole::isOpen ()
{
return openFlag;
}
bool QMyconsole::comStateChange ()
{
if(openFlag)
{
port->close ();
openFlag=false;
toClipboard (QString("The %1 has closed.\n\n")
.arg(port->portName ()));
return true;
}
else {
if(!port->open (QIODevice::ReadWrite)){
toClipboard (QString("Can't open %1, error code %2\n\n")
.arg(port->portName ()).arg(port->error()));
if(port->error ()==QSerialPort::OpenError)
{
toClipboard (QString("The %1 has opened.\n\n")
.arg(port->portName ()));
return true;
}
openFlag=false;
return false;
}
else{
toClipboard (QString("The %1 has opened.\n\n")
.arg(port->portName ()));
openFlag=true;
return true;
}
}
}
QString QMyconsole::nowComAvailab (int nNum)
{
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{ nNum--;
if(nNum);
else{
return serialPortInfo.portName();
}
}
return NULL;
}
void QMyconsole::nowSpinfo ()
{
QString text,txtinfo,txtavailab;
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
txtavailab= QString("Port:%1\nLocation:%2\nDescription:%3\nManufacturer:%4\nVendor Identifier:%5\nProduct Identifier:%6\nBusy:%7\n").
arg(serialPortInfo.portName()).arg(serialPortInfo.systemLocation())
.arg(serialPortInfo.description()).arg(serialPortInfo.manufacturer())
.arg((QString)(serialPortInfo.hasVendorIdentifier() ? QByteArray::number(serialPortInfo.vendorIdentifier(), 16) : QByteArray()))
.arg((QString)(serialPortInfo.hasProductIdentifier() ? QByteArray::number(serialPortInfo.productIdentifier(), 16) : QByteArray()))
.arg((serialPortInfo.isBusy() ? QObject::tr("Yes") : QObject::tr("No")));
QSerialPort *m_port = new QSerialPort(serialPortInfo);
if (m_port->open(QIODevice::ReadWrite)) {
txtinfo= QString("Baud rate:%1\nData bits:%2\nStop bits:%3\nParity:%4\nFlow control:%5\nRead buffer size:%6\n\n").
arg( m_port->baudRate()).arg(m_port->dataBits()).arg(m_port->stopBits())
.arg(m_port->parity()).arg(m_port->flowControl()).arg(m_port->readBufferSize());
m_port->close();
} else{
txtinfo= QString("Unable to open port, error code%1\n").
arg(m_port->error());
}
txtavailab.append (txtinfo);
text.append (txtavailab);
delete m_port;
}
toClipboard (text);
}
bool QMyconsole::emitMessageRequ (QString message)
{
messageRequ=message;
int currentWaitTimeOut=waitTimeOut;
QByteArray messageDate=message.toLocal8Bit ();
port->write (messageDate);
QTime currentTime=QTime::currentTime();
if (port->waitForBytesWritten(waitTimeOut)) {
if(port->waitForReadyRead (currentWaitTimeOut)){
QByteArray responseDate=port->readAll ();
while(port->waitForReadyRead (10))
responseDate+=port->readAll ();
QString response(responseDate);
toClipboard (QString("response:%1\n\n").
arg (response));
return true;
}
else{
toClipboard (QString("Wait write request timeout %1\n\n")
.arg(currentTime.msecsTo (QTime::currentTime())));
//超时
return false;
}
}
else{
toClipboard (QString("Could not touch the serial port,Wait write request timeout %1\n\n")
.arg(currentTime.msecsTo (QTime::currentTime())+100));
//超时
return false;
}
}
QString QMyconsole::QtMsgToQString (QtMsgType type, const char *msg)
{
switch (type) {
case QtDebugMsg:
return QLatin1String("Debug: ")+QLatin1String(msg);
case QtWarningMsg:
return QLatin1String("Warning: ")+QLatin1String(msg);
case QtCriticalMsg:
return QLatin1String("Critical: ")+QLatin1String(msg);
case QtFatalMsg:
return QLatin1String("Fatal: ")+QLatin1String(msg);
default:
return QLatin1String("Unrecognized message type: ")+QLatin1String(msg);
}
}
void QMyconsole::AppendMsgWrapper (QtMsgType type, const char *msg)
{
static QMutex mutex;
QMutexLocker locker(&mutex);
if (QMyconsole::MsgHandler != NULL)
return QMyconsole::MsgHandler->postMsgEvent(type, msg);
else
fprintf(stderr, "%s", QMyconsole::QtMsgToQString(type, msg).toLatin1().data());
}
void QMyconsole::customEvent(QEvent *event)
{
if (static_cast<QMyconsole::EventType>(event->type()) == QMyconsole::MessageEventType)
//msgTextEdit.append(dynamic_cast<MessageEvent *>(event)->msg);
QMyconsole::toClipboard (dynamic_cast<MessageEvent *>(event)->msg);
}
void QMyconsole::postMsgEvent(QtMsgType type, const char *msg)
{
QString qmsg = QMyconsole::QtMsgToQString(type, msg);
switch (type) {
case QtDebugMsg:
break;
case QtWarningMsg:
qmsg.prepend(QLatin1String("<FONT color=\"#FF0000\">"));
qmsg.append(QLatin1String("</FONT>"));
break;
case QtCriticalMsg:
/*if (QMessageBox::critical(this, QLatin1String("Critical Error"), qmsg,
QMessageBox::Ignore,
QMessageBox::Abort,
QMessageBox::NoButton) == QMessageBox::Abort)
abort(); // core dump*/
qmsg.prepend(QLatin1String("<B><FONT color=\"#FF0000\">"));
qmsg.append(QLatin1String("</FONT></B>"));
break;
case QtFatalMsg:
//QMessageBox::critical(this, QLatin1String("Fatal Error"), qmsg, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
abort(); // deliberately core dump
}
//it's impossible to change GUI directly from thread other than the main thread
//so post message encapsulated by MessageEvent to the main thread's event queue
QCoreApplication::postEvent(this, new MessageEvent(qmsg));
}
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
void QMyconsole::AppendMsgWrapper(QtMsgType type, const QMessageLogContext & /*context*/, const QString &msg)
{
AppendMsgWrapper(type, msg.toLatin1().data());
}
#endif
QMyconsole::~QMyconsole ()
{
port->close();
delete port;
}
MessageEvent::MessageEvent(QString &msg):
QEvent(static_cast<QEvent::Type>(QMyconsole::MessageEventType))
{
this->msg=msg;
}