-
Notifications
You must be signed in to change notification settings - Fork 6
/
SerialCommunication.h
110 lines (81 loc) · 2.99 KB
/
SerialCommunication.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
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
#ifndef SERIALCOMMUNICATION_H
#define SERIALCOMMUNICATION_H
class SerialConfiguration;
class SerialLink;
#include <QThread>
#include <QMutex>
#include <QSerialPort>
#include <common/mavlink.h>
/** @brief Polling interval in ms */
#define SERIAL_POLL_INTERVAL 4
// We use QSerialPort::SerialPortError in a signal so we must declare it as a meta type
Q_DECLARE_METATYPE(QSerialPort::SerialPortError)
class SerialConfiguration:public QObject
{
Q_OBJECT
friend class LinkManager;
public:
SerialConfiguration(const QString &name);
int baud() { return _baud; }
int dataBits() { return _dataBits; }
int flowControl() { return _flowControl; }
int stopBits() { return _stopBits; }
int parity() { return _parity; }
const QString portName() { return _portName; }
const QString description() { return _description; }
void setPortName (const QString& portName);
void setBaud (int baud);
void setLink(SerialLink* serial) {_link = serial;}
private:
int _baud;
int _dataBits;
int _flowControl;
int _stopBits;
int _parity;
QString _portName;
QString _description;
protected:
//link using this cofiguration
SerialLink * _link;
};
class SerialLink : public QThread
{
Q_OBJECT
friend class SerialCofiguration;
friend class LinkManager;
public:
bool isConnected() const;
//mavlink channel this link using, mavlink_parse_char uses it ,too.
uint8_t getMavlinkChannel(void) const { Q_ASSERT(_mavlinkChannelSet); return _mavlinkChannel; }
SerialConfiguration* getSerialConfiguration();
void run();
static const int poll_interval = SERIAL_POLL_INTERVAL;
public slots:
void readBytes();
void writeBytes(const char *data,qint64 length);
signals:
// New data arrived, for control links,not for image transmission
void bytesReceived(SerialLink* link, QByteArray data);
void connected();
void disconnected();
protected:
QSerialPort* _port;
quint64 _bytesRead;
int _timeout;
QMutex _dataMutex; // Mutex for reading data from _port
QMutex _writeMutex; // Mutex for accessing the _transmitBuffer
private:
SerialLink(SerialConfiguration *config);
~SerialLink();
void _setMavlinkChannel(uint8_t channel) { Q_ASSERT(!_mavlinkChannelSet); _mavlinkChannelSet = true; _mavlinkChannel = channel; }
virtual bool _connect(void);
virtual bool _disconnect(void);
bool _hardwareConnect();
volatile bool _stopp;
QMutex _stoppMutex; // Mutex for accessing _stopp
QByteArray _transmitBuffer; // An internal buffer for receiving data from member functions and actually transmitting them via the serial port.
SerialConfiguration* _config;
bool _mavlinkChannelSet = false; ///< true: _mavlinkChannel has been set
uint8_t _mavlinkChannel; ///< mavlink channel to use for this link, as used by mavlink_parse_char
};
#endif // SERIALCOMMUNICATION_H