This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aserialio.h
80 lines (58 loc) · 1.53 KB
/
aserialio.h
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
#ifndef ASERIALIO_H
#define ASERIALIO_H
#include <QObject>
#include <QSerialPort>
#include <QQmlListProperty>
class ASerialIO : public QObject
{
Q_OBJECT
Q_PROPERTY(QString port READ getPort WRITE setPort NOTIFY portChanged)
Q_PROPERTY(QStringList portList READ getPortList NOTIFY portListChanged)
Q_PROPERTY(bool open READ isOpen WRITE setOpen NOTIFY openChanged)
QStringList portList;
QSerialPort serialPort;
private slots:
void parseError(QSerialPort::SerialPortError code);
void readyToRead();
public:
ASerialIO();
virtual ~ASerialIO();
QString getPort() const
{
return serialPort.portName();
}
QStringList getPortList() const
{
return portList;
}
bool isOpen() const
{
return serialPort.isOpen();
}
Q_INVOKABLE void refreshPortList();
Q_INVOKABLE void write(int data);
Q_INVOKABLE QString describe(QString port);
signals:
void portChanged(QString port);
void portListChanged(QStringList portList);
void openChanged(bool open);
void error(QString message, QString messageExt = QString());
void incoming(int data);
public slots:
void setPort(QString _port) {
if (_port == getPort())
return;
serialPort.setPortName(_port);
emit portChanged(getPort());
}
void setOpen(bool _opened) {
if (_opened == isOpen())
return;
if (_opened)
serialPort.open(QSerialPort::ReadWrite);
else
serialPort.close();
emit openChanged(isOpen());
}
};
#endif // ASERIALIO_H