summaryrefslogtreecommitdiffstats
path: root/include/serialport.h
blob: 2089b3257423e262c1f9073e6cbb0f310804f0ee (plain)
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
/*
    License...
*/

#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <QtCore/qiodevice.h>

#ifdef SERIALPORT_SHARED
#  ifdef SERIALPORT_BUILD
#    define SERIALPORT_EXPORT Q_DECL_EXPORT
#  else
#    define SERIALPORT_EXPORT Q_DECL_IMPORT
#  endif
#else
#  define SERIALPORT_EXPORT
#endif

QT_BEGIN_NAMESPACE

class SerialPortInfo;
class SerialPortPrivate;

class SERIALPORT_EXPORT SerialPort : public QIODevice
{
    Q_OBJECT

    Q_PROPERTY(qint32 rate READ rate WRITE setRate)
    Q_PROPERTY(DataBits dataBits READ dataBits WRITE setDataBits)
    Q_PROPERTY(Parity parity READ parity WRITE setParity)
    Q_PROPERTY(StopBits stopBits READ stopBits WRITE setStopBits)
    Q_PROPERTY(FlowControl flowControl READ flowControl WRITE setFlowControl)
    Q_PROPERTY(DataErrorPolicy dataErrorPolicy READ dataErrorPolicy WRITE setDataErrorPolicy)
    Q_PROPERTY(bool dtr READ dtr WRITE setDtr)
    Q_PROPERTY(bool rts READ rts WRITE setRts)
    Q_PROPERTY(PortError error READ error RESET unsetError)
    Q_PROPERTY(bool restoreSettingsOnClose READ restoreSettingsOnClose WRITE setRestoreSettingsOnClose)

    Q_ENUMS( Directions Rate DataBits Parity StopBits FlowControl Lines DataErrorPolicy PortError )

public:

    enum Direction  {
        Input = 1,
        Output = 2,
        AllDirections = Input | Output
    };
    Q_DECLARE_FLAGS(Directions, Direction)

    enum Rate {
        Rate1200 = 1200,
        Rate2400 = 2400,
        Rate4800 = 4800,
        Rate9600 = 9600,
        Rate19200 = 19200,
        Rate38400 = 38400,
        Rate57600 = 57600,
        Rate115200 = 115200,
        UnknownRate = -1
    };

    enum DataBits {
        Data5 = 5,
        Data6 = 6,
        Data7 = 7,
        Data8 = 8,
        UnknownDataBits = -1
    };

    enum Parity {
        NoParity = 0,
        EvenParity = 2,
        OddParity = 3,
        SpaceParity = 4,
        MarkParity = 5,
        UnknownParity = -1
    };

    enum StopBits {
        OneStop = 1,
        OneAndHalfStop = 3,
        TwoStop = 2,
        UnknownStopBits = -1
    };

    enum FlowControl {
        NoFlowControl,
        HardwareControl,
        SoftwareControl,
        UnknownFlowControl = -1
    };

    enum Line {
        Le = 0x01,
        Dtr = 0x02,
        Rts = 0x04,
        St = 0x08,
        Sr = 0x10,
        Cts = 0x20,
        Dcd = 0x40,
        Ri = 0x80,
        Dsr = Le
    };
    Q_DECLARE_FLAGS(Lines, Line)

    enum DataErrorPolicy {
        SkipPolicy,
        PassZeroPolicy,
        IgnorePolicy,
        StopReceivingPolicy,
        UnknownPolicy
    };

    enum PortError {
        NoError,
        NoSuchDeviceError,
        PermissionDeniedError,
        DeviceAlreadyOpenedError,
        DeviceIsNotOpenedError,
        ParityError,
        FramingError,
        BreakConditionError,
        IoError,
        UnsupportedPortOperationError,
        UnknownPortError
    };

    explicit SerialPort(QObject *parent = 0);
    explicit SerialPort(const QString &name, QObject *parent = 0);
    explicit SerialPort(const SerialPortInfo &info, QObject *parent = 0);
    virtual ~SerialPort();

    void setPort(const QString &port);
    void setPort(const SerialPortInfo &info);
    QString portName() const;

    virtual bool open(OpenMode mode);
    virtual void close();

    void setRestoreSettingsOnClose(bool restore);
    bool restoreSettingsOnClose() const;

    bool setRate(qint32 rate, Directions dir = AllDirections);
    qint32 rate(Directions dir = AllDirections) const;

    bool setDataBits(DataBits dataBits);
    DataBits dataBits() const;

    bool setParity(Parity parity);
    Parity parity() const;

    bool setStopBits(StopBits stopBits);
    StopBits stopBits() const;

    bool setFlowControl(FlowControl flow);
    FlowControl flowControl() const;

    bool dtr() const;
    bool rts() const;

    Lines lines() const;

    bool flush();
    virtual bool reset();
    virtual bool atEnd() const;

    bool setDataErrorPolicy(DataErrorPolicy policy = IgnorePolicy);
    DataErrorPolicy dataErrorPolicy() const;

    PortError error() const;
    void unsetError();

    qint64 readBufferSize() const;
    void setReadBufferSize(qint64 size);

    virtual bool isSequential() const;

    virtual qint64 bytesAvailable() const;
    virtual qint64 bytesToWrite() const;
    virtual bool canReadLine() const;

    virtual bool waitForReadyRead(int msecs);
    virtual bool waitForBytesWritten(int msecs);

public Q_SLOTS:
    bool setDtr(bool set);
    bool setRts(bool set);
    bool sendBreak(int duration = 0);
    bool setBreak(bool set = true);
    bool clearBreak(bool clear = true);

protected:
    virtual qint64 readData(char *data, qint64 maxSize);
    virtual qint64 readLineData(char *data, qint64 maxSize);
    virtual qint64 writeData(const char *data, qint64 maxSize);

private:
    SerialPortPrivate * const d_ptr;

    Q_DECLARE_PRIVATE(SerialPort)
    Q_DISABLE_COPY(SerialPort)
};

inline bool SerialPort::clearBreak(bool clear)
{ return setBreak(!clear); }

Q_DECLARE_OPERATORS_FOR_FLAGS(SerialPort::Directions)
Q_DECLARE_OPERATORS_FOR_FLAGS(SerialPort::Lines)

QT_END_NAMESPACE

#endif // SERIALPORT_H