-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_port.cpp
334 lines (286 loc) · 9.36 KB
/
serial_port.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
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
/*
qCommTest - Serial Communication Test Tool
Version : 1.0
Date : 20.11.2017
Author : Eray Ozturk | github.com/diffstorm
*/
#include "serial_port.h"
serial_port::serial_port(QObject *parent) : QObject(parent)
{
m_serialPort = new QSerialPort(this);
connect(m_serialPort, &QSerialPort::bytesWritten, this, &serial_port::onBytesWritten);
connect(m_serialPort, &QSerialPort::readyRead, this, &serial_port::onReadyRead);
connect(m_serialPort, &QSerialPort::errorOccurred, this, &serial_port::onError);
connect(&m_timer_rx, &QTimer::timeout, this, &serial_port::onTimeoutRX);
connect(&m_timer_tx, &QTimer::timeout, this, &serial_port::onTimeoutTX);
m_timer_tx.setSingleShot(true);
m_timer_tx.stop();
m_timer_rx.setSingleShot(true);
m_timer_rx.stop();
m_dataReceivedAt = 0;
m_dataSentAt = 0;
}
serial_port::~serial_port()
{
Close();
delete m_serialPort;
}
void serial_port::Log(const QString &log)
{
emit logMessage("COM Port : " + log);
}
qint64 serial_port::getReceivedTime()
{
return m_dataReceivedAt;
}
qint64 serial_port::getSentTime()
{
return m_dataSentAt;
}
QList<QString> serial_port::Scan()
{
QList<QString> ports;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ports.append(info.portName());
/*
QString s = QObject::tr("Port:") + info.portName() + "\n"
+ QObject::tr("Location:") + info.systemLocation() + "\n"
+ QObject::tr("Description:") + info.description() + "\n"
+ QObject::tr("Manufacturer:") + info.manufacturer() + "\n"
//+ QObject::tr("Serial number:") + info.serialNumber() + "\n"
+ QObject::tr("Vendor Identifier:") + (info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) + "\n"
+ QObject::tr("Product Identifier:") + (info.hasProductIdentifier() ? QString::number(info.productIdentifier(), 16) : QString()) + "\n"
//+ QObject::tr("Busy:") + (info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) + "\n"
;
qDebug() << s;
*/
}
return ports;
}
bool serial_port::Open(QString name)
{
bool ret = false;
if(m_serialPort->isOpen())
{
Close();
}
Log(QString("%1").arg(name));
m_serialPort->setPortName(name);
m_serialPort->setReadBufferSize(4096);
if(m_serialPort->open(QIODevice::ReadWrite))
{
Log("Open");
ret = true;
}
else
{
Log("Open error");
}
return ret;
}
bool serial_port::isOpen()
{
return m_serialPort->isOpen();
}
void serial_port::Configure(qint32 rate, QSerialPort::DataBits bits, QSerialPort::FlowControl flow, QSerialPort::Parity parity, QSerialPort::StopBits stopBits)
{
if(!m_serialPort->setBaudRate(rate))
{
Log("Baud rate error :" + m_serialPort->errorString());
}
if(!m_serialPort->setDataBits(bits))
{
Log("Data bits error :" + m_serialPort->errorString());
}
if(!m_serialPort->setFlowControl(flow))
{
Log("Flow control error :" + m_serialPort->errorString());
}
if(!m_serialPort->setParity(parity))
{
Log("Parity error :" + m_serialPort->errorString());
}
if(!m_serialPort->setStopBits(stopBits))
{
Log("Stop bits error :" + m_serialPort->errorString());
}
if(!m_serialPort->setDataTerminalReady(false))
{
Log("DTR error :" + m_serialPort->errorString());
}
// TODO : calculate the actual frame size
setBaudTimeout(rate, 12); // Assume that a frame is 12 bits long as worst case
}
void serial_port::setBaudTimeout(qint64 baud_rate, qint8 frame_size)
{
m_baudtimeout = (4 * 1000) / (baud_rate / frame_size);
qDebug() << "Baud timeout is calculated as" << (1 + m_baudtimeout) << "ms";
}
qint64 serial_port::getBaudTimeout(qint64 data_size)
{
return (1 + (m_baudtimeout * data_size));
}
void serial_port::waitForBytesWritten()
{
qint64 bytesToWrite = m_serialPort->bytesToWrite();
if(bytesToWrite)
{
qDebug() << "Busy! Waiting" << getBaudTimeout(bytesToWrite) << "ms to write" << bytesToWrite << "bytes";
m_serialPort->waitForBytesWritten(getBaudTimeout(bytesToWrite));
}
}
void serial_port::Close()
{
if(m_serialPort->isOpen())
{
waitForBytesWritten();
m_serialPort->close();
Log("Closed");
}
m_timer_rx.stop();
m_timer_tx.stop();
}
bool serial_port::Write(const QByteArray &writeData)
{
bool ret = true;
waitForBytesWritten();
if(m_serialPort->isWritable())
{
m_writeData.clear();
m_writeData = writeData;
m_dataSentAt = QDateTime::currentMSecsSinceEpoch();
qDebug() << "Bytes to write :" << writeData.size();
qint64 bytesWritten = m_serialPort->write(writeData);
if(bytesWritten == -1)
{
ret = false;
qDebug() << "Failed to write the data to port" << m_serialPort->portName() << "error:" << m_serialPort->errorString();
}
else if(bytesWritten != m_writeData.size())
{
ret = false;
qDebug() << "Failed to write all the data to port" << m_serialPort->portName() << "error:" << m_serialPort->errorString();
}
else if(bytesWritten == m_writeData.size())
{
qDebug() << "Buffer write successful to port" << m_serialPort->portName();
}
qDebug() << "Remaining bytes to write :" << m_serialPort->bytesToWrite();
qDebug() << "Write timeout is set to" << getBaudTimeout(m_writeData.size()) << "ms to write" << m_writeData.size() << "bytes";
m_timer_tx.start(getBaudTimeout(m_writeData.size()));
}
else
{
ret = false;
qDebug() << "Serial port is not writable";
}
return ret;
}
QByteArray serial_port::Read()
{
QByteArray readData;
if(m_serialPort->isReadable())
{
qint64 bytesAvailable = m_serialPort->bytesAvailable();
if(bytesAvailable > 0)
{
qDebug() << "Busy! Waiting" << getBaudTimeout(bytesAvailable) << "ms to read" << bytesAvailable << "bytes";
m_serialPort->waitForReadyRead(getBaudTimeout(bytesAvailable));
}
}
else
{
qDebug() << "Serial port is not readable";
}
readData = m_readData;
m_readData.clear();
Log(QString("Read %1 bytes").arg(readData.size()));
return readData;
}
void serial_port::onReadyRead()
{
if(m_serialPort->isReadable())
{
qint64 bytesAvailable = m_serialPort->bytesAvailable();
qDebug() << bytesAvailable << "bytes are available to read";
if(bytesAvailable > 0)
{
m_readData.append(m_serialPort->readAll());
m_dataReceivedAt = QDateTime::currentMSecsSinceEpoch();
}
qDebug() << "Read timeout is set to" << getBaudTimeout(bytesAvailable) << "ms to read" << bytesAvailable << "bytes";
m_timer_rx.start(getBaudTimeout(bytesAvailable));
}
else
{
qDebug() << "Serial port is not readable";
}
}
void serial_port::onBytesWritten(qint64 bytes)
{
m_bytesWritten += bytes;
if(m_bytesWritten == m_writeData.size())
{
m_timer_tx.stop();
m_bytesWritten = 0;
qDebug() << "Data successfully sent to port" << m_serialPort->portName();
Log(QString("Written %1 bytes").arg(m_writeData.size()));
//qDebug() << QString("TX:" + m_writeData.toHex());
}
else
{
qint64 bytesToWrite = m_writeData.size() - m_bytesWritten;
qDebug() << "Written" << m_bytesWritten << "/" << m_writeData.size();
qDebug() << "Write timeout is set to" << getBaudTimeout(bytesToWrite) << "ms to write" << bytesToWrite << "bytes";
m_timer_tx.start(getBaudTimeout(bytesToWrite));
}
}
void serial_port::onTimeoutTX()
{
if(m_bytesWritten != m_writeData.size())
{
Log("Write operation timed out");
qDebug() << "Write operation timed out for port" << m_serialPort->portName();
}
else
{
qDebug() << "Data sent succeeded but timeout occurred";
}
if(m_serialPort->error() != QSerialPort::NoError)
{
qDebug() << "error:" << m_serialPort->errorString();
}
}
void serial_port::onTimeoutRX()
{
if(m_readData.isEmpty())
{
qDebug() << "No data was currently available for reading from port" << m_serialPort->portName();
}
else
{
qDebug() << "Data successfully received from port" << m_serialPort->portName();
qDebug() << m_readData.length() << "bytes received.";
//qDebug() << QString("RX:" + m_readData.toHex());
emit dataReceived();
}
}
void serial_port::onError(QSerialPort::SerialPortError serialPortError)
{
switch(serialPortError)
{
case QSerialPort::NoError:
// ignore
break;
case QSerialPort::WriteError:
qDebug() << "An I/O error occurred while writing data to port" << m_serialPort->portName() << "error:" << m_serialPort->errorString();
break;
case QSerialPort::ReadError:
qDebug() << "An I/O error occurred while reading data from port" << m_serialPort->portName() << "error:" << m_serialPort->errorString();
break;
default:
qCritical() << "Serial Port error:" << m_serialPort->errorString();
break;
}
}