-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortListener.cpp
81 lines (52 loc) · 1.79 KB
/
PortListener.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
#include "PortListener.h"
#include <QtDebug>
PortListener::PortListener()
{
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
foreach (QextPortInfo info, ports)
{
qDebug() << "port name:" << info.portName;
qDebug() << "friendly name:" << info.friendName;
qDebug() << "physical name:" << info.physName;
qDebug() << "enumerator name:" << info.enumName;
qDebug() << "vendor ID:" << info.vendorID;
qDebug() << "product ID:" << info.productID;
}
exit(0);
this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_2);
if (port->open(QIODevice::ReadWrite) == true)
{
connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(port, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
if (!(port->lineStatus() & LS_DSR))
{
qDebug() << "warning: device is not turned on";
}
qDebug() << "listening for data on" << port->portName();
}
else
{
qDebug() << "device failed to open:" << port->errorString();
}
}
void PortListener::onReadyRead()
{
QByteArray bytes;
int a = port->bytesAvailable();
bytes.resize(a);
port->read(bytes.data(), bytes.size());
qDebug() << "bytes read:" << bytes.size();
qDebug() << "bytes:" << bytes;
}
void PortListener::onDsrChanged(bool status)
{
if (status)
qDebug() << "device was turned on";
else
qDebug() << "device was turned off";
}