-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththreadsend.cpp
More file actions
171 lines (154 loc) · 3.66 KB
/
threadsend.cpp
File metadata and controls
171 lines (154 loc) · 3.66 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
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
#include "threadsend.h"
ThreadSend::ThreadSend(QObject *parent) :
QThread(parent)
{
serial=new QSerialPort();
setParent(0);
moveToThread(this);
}
void ThreadSend::run()
{
exec();
}
void ThreadSend::test(QString portName,QByteArray ssid,QByteArray pwd, QByteArray encryption)
{
ThreadSend::TryConnect(portName);
QByteArray m_readData;
serial->write("w");
m_readData=ThreadSend::wait_for_response(15000);
if (m_readData[0]!='0')
{
emit write(11);
CloseConnec();
}
else
{
emit write(3);
serial->write(ssid);
serial->write("\r");
serial->write(pwd);
serial->write("\r");
serial->write(encryption);
//wait for the device to answer that it has read everything (CONFIG_ERROR_NO_ERROR) + timeout
m_readData=wait_for_response(5000);
if (m_readData[0]!='0')
{
emit write(12);
CloseConnec();
}
else
{
emit write(4);
m_readData=wait_for_response(120000);
if (m_readData[0]!='0')
{
emit write(13);
}
else
{
emit write(5);
m_readData = wait_for_response(10000);
if (m_readData[0] != '0')
{
emit write(15);
CloseConnec();
}
else
{
emit write(6);
CloseConnec();
}
/*
else if (m_readData[0]=='4' || m_readData[0]=='b' || m_readData[0]=='8' || m_readData[0]=='9'|| m_readData[0]=='a')
{
//Boxmessage on the GUI
emit write(10);
//int reponse = QMessageBox::warning(this, "Could not connect", "COULD NOT CONNECT :Do you want to save the configuration anyway?",QMessageBox::Yes|QMessageBox::No);
//if (reponse==QMessageBox::Yes)
//{
//}
//else serial->write("n");
}*/
}
}
}
// closeSerialPort();
}
void ThreadSend::writeConf(bool b)
{
QByteArray m_readData;
if (b)
{
serial->write("s");
m_readData=wait_for_response(5000);
emit write(5);
m_readData = wait_for_response(10000);
if (m_readData[0] != '0')
{
emit write(15);
CloseConnec();
}
else
{
emit write(6);
CloseConnec();
}
}
else
{
serial->write("n");
m_readData=wait_for_response(5000);
if (m_readData[0]=='0')
{
emit write(14);
//CloseConnec();
}
else
{
CloseConnec();
emit write(16);
}
}
}
void ThreadSend::closeSerialPort()
{
serial->close();
}
QByteArray ThreadSend::wait_for_response(int msec)
{
bool b=true;
QByteArray tmp;
serial->waitForReadyRead(msec);
while (b)
{
tmp=serial->read(1);
if (tmp=="#")
{
b=false;
}
}
return serial->readAll();
}
void ThreadSend::TryConnect(QString str)
{
serial->setPortName(str);
//if there is no open serial communication
if(serial->isOpen())
{
closeSerialPort();
}
emit write(1);
if (serial->open(QIODevice::ReadWrite)) {
if (serial->setBaudRate(BAUDRATE))
{
emit write(2);
}
} else {
emit write(10);
}
}
void ThreadSend::CloseConnec()
{
closeSerialPort();
exit();
}