-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserialmanager.h
More file actions
90 lines (78 loc) · 1.91 KB
/
serialmanager.h
File metadata and controls
90 lines (78 loc) · 1.91 KB
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
#ifndef SERIALMANAGER_H
#define SERIALMANAGER_H
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <QThread>
#include <QDebug>
#include <QMetaType>
#define MAX_BUFFER_SIZE 1000
struct PortOptions
{
public:
QString portName;
int baudRate;
int parity;
int charSize;
int stopBits;
bool enableFlowControl;
int totalTimeout_ms;
int interBytesTimeout_ms;
PortOptions(){
this->baudRate = 115200;
this->parity = 0;
this->charSize = 8;
this->stopBits = 1;
this->portName = "";
this->enableFlowControl = false;
this->totalTimeout_ms = 0;
this->interBytesTimeout_ms = 0;
}
PortOptions(QString pname){
this->baudRate = 115200;
this->parity = 0;
this->charSize = 8;
this->stopBits = 1;
this->portName = pname;
this->enableFlowControl = false;
this->totalTimeout_ms = 0;
this->interBytesTimeout_ms = 0;
}
};
class SerialManager : public QThread
{
Q_OBJECT
public:
enum ReadType { ASCII, BINARY };
SerialManager(QObject *parent = 0);
~SerialManager();
void SetupSerialPort(PortOptions *opt);
void StartListen(ReadType type);
void StopListen();
bool isStarted() { return m_isStarted; }
void WriteToPort(QByteArray cmd);
protected:
void run();
signals:
void LineDetected(QString line);
void BinaryDataReceived(QByteArray *data);
void SerialPortIsReady();
private:
bool OpenPort();
bool ClosePort();
bool isOpen();
bool SetPortConfig();
bool SetPortTimeout(int readInterval = 0, int readTotal = 0);
bool m_isStarted;
QString ReadPort();
QByteArray* ReadPortBinary();
//Variables:
bool m_ready;
bool m_portSetuped;
PortOptions *m_pOptions;
int m_portHandler;
ReadType m_readType;
char buffer[MAX_BUFFER_SIZE];
};
#endif // SERIALMANAGER_H