-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreceivedata.cpp
executable file
·222 lines (177 loc) · 5.33 KB
/
receivedata.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
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
214
215
216
217
218
219
220
221
#include "receivedata.h"
#define CODEC_NEED_DATA 1
#define CODEC_OK 4
#define CODEC_SYNC 2
#define CODEC_PAYLOAD 3
#define PREAMBLE_SIZE 4 //in bytes
#define PAYLOAD_SIZE 800 //in bytes
#define PREAMBLE_1 0xAA
#define PREAMBLE_2 0xAA
#define PREAMBLE_3 0xAA
#define PREAMBLE_4 0xAA
ReceiveData::ReceiveData(QObject *parent) : QObject(parent)
{
serialPort = new QSerialPort(this);
inBuffer = new Buffer(inBufferArray, 65536);
this->decoderState = CODEC_SYNC;
timeoutTimer = new QTimer(this);
timeoutTimer->setInterval(380);
connect(timeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()));
}
ReceiveData::~ReceiveData()
{
}
bool ReceiveData::serialPortConnect()
{
serialPort->setPortName(SerialPortSettingModel::getInstance()->getPortName());
qDebug() << serialPort->portName();
serialPort->setBaudRate(SerialPortSettingModel::getInstance()->getBaudRate());
qDebug() << serialPort->baudRate();
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::OneStop);
serialPort->setFlowControl(QSerialPort::NoFlowControl);
if(serialPort->open(QIODevice::ReadWrite))
{
connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialReceived()));
qDebug() << "Opened";
return true;
}
else
{
return false;
qDebug() << "Could not open";
}
}
void ReceiveData::serialPortDisconnect()
{
serialPort->close();
disconnect(serialPort, SIGNAL(readyRead()), this, SLOT(serialReceived()));
}
void ReceiveData::sendData(QString dataStr)
{
serialPort->write(dataStr.toLatin1(), dataStr.toLatin1().count());
}
void ReceiveData::reset()
{
serialPort->readAll();
char b;
while(inBuffer->getLength())
{
inBuffer->getByte(&b);
}
}
void ReceiveData::serialReceived()
{
int result;
QByteArray serialData = serialPort->readAll();
inBuffer->putN(serialData.data(), 0, serialData.length());
do
{
result = this->decode();
}while(result != CODEC_NEED_DATA);
}
int ReceiveData::decode()
{
switch (this->decoderState) {
case CODEC_SYNC:
if(inBuffer->getLength() < PREAMBLE_SIZE)
{
// qDebug() << "N";
return CODEC_NEED_DATA;
}
if(processPreamble())
{
timeoutTimer->start();
// qDebug() << count << "P";
this->decoderState = CODEC_PAYLOAD;
if(inBuffer->getLength() < PAYLOAD_SIZE)
{
// qDebug() << "N";
return CODEC_NEED_DATA;
}
else
{
this->decoderState = CODEC_SYNC;
timeoutTimer->stop();
processData();
}
}
break;
case CODEC_PAYLOAD:
if(inBuffer->getLength() < PAYLOAD_SIZE)
{
// qDebug() << "N";
return CODEC_NEED_DATA;
}
else
{
//FIXME: tem um bug na recepção dos dados, quando o carrinho é resetado e a interface é mantida sem clicar em
//nada, de alguma forma os dados são plotados, eu verifiquei e os dados chegam corretamente na porta serial
//de alguma forma estes dados não são passados para o mainwidget a classe que se registra no evento de dados
//recebidos. O valor plotado fica zero e da um pico em 1.25e-9
//Bug descoberto
//Ele se deve ao fato de qdo é pressionado reset no cara que envia o pacote, ele vai reenviar o cabeçalho
//como o cabeçalho é 0xAAAAAAAA isso dá um valor muito grande, quando o gráfico ajusta a escala não
//da então para ver os dados reais. A solução é implementar um timeout, se os dados não chegarem reseta
//o codec para CODEC SYNC e descarta os dados
// qDebug() << count << "D";
this->decoderState = CODEC_SYNC;
processData();
count++;
}
break;
default:
break;
}
return CODEC_OK;
}
bool ReceiveData::processPreamble()
{
char temp_data = 0;
bool bresult = false;
inBuffer->getByte(&temp_data);
if((unsigned char)temp_data == PREAMBLE_1)
{
inBuffer->getByte(&temp_data);
if((unsigned char)temp_data == PREAMBLE_2)
{
inBuffer->getByte(&temp_data);
if((unsigned char)temp_data == PREAMBLE_3)
{
inBuffer->getByte(&temp_data);
if((unsigned char)temp_data == PREAMBLE_3)
{
bresult = true;
}
}
}
}
return bresult;
}
bool ReceiveData::processData()
{
//static int i = 0;
inBuffer->getN((char *)payloadDecoded, 0, PAYLOAD_SIZE);
memcpy(dataconverted, payloadDecoded, PAYLOAD_SIZE);
//qDebug() << "received - " << i;
//i++;
// for(int i = 0; i < 200; i++)
// {
// qDebug() << payloadDecoded[i];
// }
emit dataDecoded(dataconverted);
return true;
}
void ReceiveData::timeout()
{
this->decoderState = CODEC_SYNC;
serialPort->readAll();
char n;
while(inBuffer->getLength() > 0)
{
inBuffer->getByte(&n);
}
//qDebug() << "Timeout";
timeoutTimer->stop();
}