-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserialmanager.cpp
More file actions
340 lines (269 loc) · 9.31 KB
/
serialmanager.cpp
File metadata and controls
340 lines (269 loc) · 9.31 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "serialmanager.h"
#include <QStringList>
// --- [CONSTRUCTOR] -----------------------------------------------------------------------
SerialManager::SerialManager(QObject *parent) : QThread(parent)
{
this->m_ready = false;
this->m_portHandler = -1;
this->m_portSetuped = false;
}
// --- [DISTRUCTOR] ------------------------------------------------------------------------
SerialManager::~SerialManager()
{
if (isOpen()) {
this->ClosePort();
}
delete this->m_pOptions;
}
// --- [SetupSerialPort] -------------------------------------------------------------------
void SerialManager::SetupSerialPort(PortOptions *opt)
{
this->m_pOptions = opt;
this->m_portSetuped = true;
this->wait();
}
// --- [StartListen] -----------------------------------------------------------------------
void SerialManager::StartListen(ReadType type)
{
this->m_ready = true;
this->m_isStarted = true;
this->m_readType = type;
start();
}
// --- [StopListen] ------------------------------------------------------------------------
void SerialManager::StopListen()
{
m_ready = false;
this->m_isStarted = false;
this->wait();
}
// --- [OpenPort] --------------------------------------------------------------------------
bool SerialManager::OpenPort()
{
bool retVal = true;
qDebug() << "Opening Port: [" << m_pOptions->portName.toLatin1().data() << "]";
if(-1 == (m_portHandler = open(m_pOptions->portName.toLatin1().data(), O_RDWR | O_NOCTTY | O_NDELAY))) {
retVal = false;
} else {
fcntl(m_portHandler, F_SETFL, 0);
termios options;
bzero(&options, sizeof(options));
options.c_cflag |= CREAD | CLOCAL;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 0;
tcflush(m_portHandler, TCIFLUSH);
if (tcsetattr(m_portHandler, TCSANOW, & options) < 0) {
retVal = false;
} else {
fcntl(m_portHandler, F_SETFL, FNDELAY);
retVal =true;
}
}
if (!retVal) {
qDebug() << "Error on opening port: [" << m_pOptions->portName.toLatin1().data() << "]";
} else {
qDebug() << "Port [" << m_pOptions->portName.toLatin1().data() << "] opened successfully.";
}
return retVal;
}
// --- [ClosePort] -------------------------------------------------------------------------
bool SerialManager::ClosePort()
{
if (m_portHandler >= 0) {
close(m_portHandler);
m_portHandler = -1;
}
return true;
}
// --- [isOpen] ----------------------------------------------------------------------------
bool SerialManager::isOpen()
{
return (m_portHandler != -1);
}
// --- [SetPortConfig] ---------------------------------------------------------------------
bool SerialManager::SetPortConfig()
{
int baudRate = m_pOptions->baudRate;
int bits = m_pOptions->charSize;
int parity = m_pOptions->parity;
int stopBits = m_pOptions->stopBits;
bool enableFlowControl = m_pOptions->enableFlowControl;
if(!isOpen())
return false;
int BR;
//=== Set BaudRate:
switch(baudRate){
case 50: BR = B50; break;
case 75: BR = B75; break;
case 110: BR = B110; break;
case 134: BR = B134; break;
case 200: BR = B200; break;
case 300: BR = B300; break;
case 600: BR = B600; break;
case 1200: BR = B1200; break;
case 2400: BR = B2400; break;
case 4800: BR = B4800; break;
case 9600: BR = B9600; break;
case 19200: BR = B19200; break;
case 38400: BR = B38400; break;
case 57600: BR = B57600; break;
case 115200: BR = B115200; break;
default: return false;
}
m_pOptions->baudRate = baudRate;
//=== Get current configuration of port.
termios options;
if(tcgetattr(m_portHandler, &options) < 0) return false;
//=== Set BaudRate for read and write.
if((cfsetispeed(&options, BR) < 0) || (cfsetospeed(&options,BR) < 0))
return false;
//=== Set Character size.
options.c_cflag &= ~CSIZE;
switch(bits){
case 5: options.c_cflag |= CS5; break;
case 6: options.c_cflag |= CS6; break;
case 7: options.c_cflag |= CS7; break;
case 8: options.c_cflag |= CS8; break;
default: return false;
}
//=== Set Parity.
//=== 0: NONE, 1: ODD, 2: EVEN
switch(parity){
case 2:
options.c_cflag |= PARENB ;
options.c_cflag &= ~PARODD ;
options.c_iflag |= INPCK ;
break ;
case 1:
options.c_cflag |= ( PARENB | PARODD );
options.c_iflag |= INPCK;
break ;
case 0:
options.c_cflag &= ~(PARENB);
options.c_iflag |= IGNPAR;
break ;
default: return false;
}
//=== Set Stop Bit
switch(stopBits){
case 1: options.c_cflag &= ~(CSTOPB); break;
case 2: options.c_cflag |= CSTOPB; break;
default: return false;
}
//=== Set Flow Control Enabled or not.
if (enableFlowControl)
{
// RTS/CTS ON:
options.c_cflag |= CRTSCTS ;
}
else
{
// none
options.c_cflag &= ~(CRTSCTS) ;
}
//=== Write New settings to port
if(tcsetattr(this->m_portHandler, TCSANOW, &options) < 0 )
return false;
qDebug("Settings Saved successfully");
return true;
}
// --- [SetPortTimeout] --------------------------------------------------------------------
bool SerialManager::SetPortTimeout(int readInterval, int readTotal)
{
// Port must be open!
if (!isOpen())
return false;
// Save variables which are used in other methods:
m_pOptions->totalTimeout_ms = readTotal;
m_pOptions->interBytesTimeout_ms = readInterval;
// VMIN & VTIME
termios options;
if ( tcgetattr( m_portHandler, &options ) < 0 )
return false;
// We set VMIN=0 and VTIME=ReadIntervalTimeout (in thenth of seconds)
options.c_cc[ VMIN ] = 0;
options.c_cc[ VTIME ] = (readTotal / 100 > 1 ? readTotal / 100 : 1);
//max(1,ReadTotalTimeoutConstant / 100);
// Write the new settings to the port.
if ( tcsetattr( m_portHandler,TCSANOW,&options ) < 0 )
return false;
return true;
}
// --- [ReadPort] --------------------------------------------------------------------------
QString SerialManager::ReadPort()
{
if(!isOpen())
return "";
int numRead = 0;
if((0 > (numRead = read(m_portHandler, buffer, MAX_BUFFER_SIZE))))
return "";
buffer[numRead] = '\0';
QString str = buffer;
return str;
}
// --- [ReadPortBinary] --------------------------------------------------------------------
QByteArray* SerialManager::ReadPortBinary()
{
if(!isOpen())
return NULL;
int numRead = 0;
if((0 > (numRead = read(m_portHandler, buffer, MAX_BUFFER_SIZE))))
return NULL;
QByteArray* ba = new QByteArray(buffer, numRead);
return ba;
}
// --- [run] -------------------------------------------------------------------------------
void SerialManager::run()
{
if (!this->m_portSetuped) {
qDebug("Port Not Configged yet.");
} else { //Port is ready for start listening.
if( this->OpenPort() && this->SetPortConfig() && this->SetPortTimeout() ) {
QString str = this->ReadPort();
qRegisterMetaType< QString >("QString");
emit SerialPortIsReady();
if (m_readType == ASCII) {
while (this->m_ready) {
QStringList strLines = str.split("\r");
for (int i = 0; i < strLines.count() - 1; ++i) {
emit LineDetected(strLines[i]);
}
str = strLines[strLines.count() - 1];
str += ReadPort();
msleep(100);
}
} else if (this->m_ready == BINARY) {
while (m_ready) {
QByteArray *ba = this->ReadPortBinary();
if (ba != NULL && ba->length() > 0) {
emit BinaryDataReceived(ba);
}
msleep(100);
}
}
}
}
}
// --- [WriteToPort] -----------------------------------------------------------------------
void SerialManager::WriteToPort(QByteArray cmd)
{
static int cntr = 0;
if(!this->isOpen())
return;
int n = write(m_portHandler, cmd.data(), cmd.length());
if (n < 0)
qDebug("Write failed");
else
qDebug() << "[WRITE:" << cntr++ << "]: " << cmd;
msleep(100);
}
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------
// --- [] ----------------------------------------------------------------------------------