forked from ganzziani/xscopes-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialportconnection.cpp
190 lines (170 loc) · 5.32 KB
/
serialportconnection.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
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
#include "serialportconnection.h"
#include <QThread>
#include "extserialport/qextserialport.h"
SerialPortConnection::SerialPortConnection(QObject *parent) :
QObject(parent)
{
serial=NULL;
sendData=false;
wsk=NULL;
finish=true;
samplingValue=0;
index = 0;
m_stateOfConnection = 1;
end_of_frame[0] = 10;
end_of_frame[1] = 13;
end_of_frame[2] = 55;
start_of_frame_fast[0] = 13;
start_of_frame_fast[1] = 10;
start_of_frame_fast[2] = 70;
start_of_frame_slow[0] = 13;
start_of_frame_slow[1] = 10;
start_of_frame_slow[2] = 83;
thread()->setPriority(QThread::HighestPriority);
connect(&timer,SIGNAL(timeout()),this,SLOT(onReadyRead()));
}
bool SerialPortConnection::connectToPort(QString name){
if(serial==NULL || (serial!=NULL && !serial->isOpen())){
PortSettings settings = {BAUD115200, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10};
serial = new QextSerialPort(name, settings, QextSerialPort::Polling);
serial->setPortName(name);
if (serial->open(QIODevice::ReadWrite)) {
qDebug()<<"SUCCESS";
timer.start(10);
finish=false;
return true;
}else{
emit connectionStatus("Port is busy or unreachable.");
clearPort();
return false;
}
}
return false;
}
void SerialPortConnection::clearPort(){
wsk = NULL;
if(serial){
delete serial;
serial = NULL;
}
}
void SerialPortConnection::close(){
if(serial && serial->isOpen()){
wsk=NULL;
serial->flush();
serial->close();
disconnect(serial);
delete serial;
serial=NULL;
timer.stop();
}
}
void SerialPortConnection::write(QString string){
if(serial && serial->isOpen()){
serial->write(string.toLatin1());
//while(serial->waitForBytesWritten(1000) );
}
}
void SerialPortConnection::writeByteArray(QByteArray string){
if(serial && serial->isOpen()){
serial->write(string);
//while(serial->waitForBytesWritten(1000) );
}
}
bool SerialPortConnection::bytesAvailable(){
if(serial && serial->isOpen())
return serial->bytesAvailable();
return false;
}
void SerialPortConnection::setSamplingValue(int value){
if(samplingValue < 11 && value >= 11){
index = 0;
if(serial && serial->isOpen()){
serial->flush();
}
m_stateOfConnection = 0;
}
if(samplingValue >= 11 && value < 11){
m_stateOfConnection = 0;
if(serial && serial->isOpen())
serial->flush();
}
this->samplingValue = value;
}
void SerialPortConnection::onReadyRead(){
int max_length = 768;
if(!sendData) return;
if(!serial || !serial->isOpen()) return;
if(!serial->bytesAvailable()) return;
if(wsk == NULL) return;
char tmp[769];
if(samplingValue >= 11){
int max = 3*(serial->bytesAvailable()/3);
if(max == 0) return;
if(max > 768) max = 768;
int size = serial->read(tmp,max);
for(int i = 0; i < size; i += 3){
if(checkIfStartOfFrame(tmp + i, false)){
index = 0;
m_stateOfConnection = 1;
continue;
}
if(checkIfEndOfFrame(tmp + i)){
index = 0;
continue;
}
if(m_stateOfConnection == 0) continue;
wsk[index ] = tmp[i];
wsk[index + 256] = tmp[i+1];
wsk[index + 512] = tmp[i+2];
index ++;
emit newData(3);
}
}else{
if(m_stateOfConnection == 0){
bool end = false;
while(!end){
if(serial->bytesAvailable() < 3){
return;
}
char tmp_start_of_frame[3];
serial->read(tmp_start_of_frame,3);
if(checkIfStartOfFrame(tmp_start_of_frame,true)){
m_stateOfConnection = 1;
end = true;
}
}
}else if(m_stateOfConnection == 1){
if(serial->bytesAvailable() < 768){
return;
}
int size = serial->read(tmp, max_length);
for(int i = 0; i < size; i++){
wsk[i] = tmp[i];
}
if(size == 768) {
wsk[768] = 0;
wsk[769] = 0;
}
emit newData(770);
m_stateOfConnection = 2;
}else if(m_stateOfConnection == 2){
if(serial->bytesAvailable() < 3){
return;
}
char tmp_end_of_frame[3];
serial->read(tmp_end_of_frame,3);
if(checkIfEndOfFrame(tmp_end_of_frame))
m_stateOfConnection = 0;
}
}
}
bool SerialPortConnection::checkIfEndOfFrame(char tab[]){
return ((int)tab[0] == end_of_frame[0] && (int)tab[1] == end_of_frame[1] && (int)tab[2] == end_of_frame[2]);
}
bool SerialPortConnection::checkIfStartOfFrame(char tab[], bool mode){
if(mode)
return ((int)tab[0] == start_of_frame_fast[0] && (int)tab[1] == start_of_frame_fast[1] && (int)tab[2] == start_of_frame_fast[2]);
else
return ((int)tab[0] == start_of_frame_slow[0] && (int)tab[1] == start_of_frame_slow[1] && (int)tab[2] == start_of_frame_slow[2]);
}