-
Notifications
You must be signed in to change notification settings - Fork 0
/
seriallogging.cpp
137 lines (109 loc) · 3.97 KB
/
seriallogging.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "mainwindow.h"
#include "seriallogging.h"
SerialLogging::SerialLogging(MainWindow *mainWindow, QSerialPortInfo serialPortDevice, int baudRate, QSerialPort::DataBits dataBits, QSerialPort::Parity parity, QSerialPort::StopBits stopBits)
{
serialPort = new QSerialPort(serialPortDevice);
// Open
int ret = serialPort->open(QIODevice::ReadWrite);
if (!ret) {
qDebug() << "Serial port failed to open";
delete serialPort;
Q_ASSERT(0);
} else {
// Open file
fileName = mainWindow->getFileName();
if (fileName != "") {
file.setFileName(fileName);
ret = file.open(QIODevice::WriteOnly);
if (!ret) {
qDebug() << "File failed to open";
delete serialPort;
Q_ASSERT(0);
}
}
// Set start time
startTime = QDateTime::currentDateTimeUtc();
// Configure serial port
serialPort->setBaudRate(baudRate);
serialPort->setDataBits(dataBits);
serialPort->setParity(parity);
serialPort->setStopBits(stopBits);
connect(this, SIGNAL(serialPortOpened()), mainWindow, SLOT(serialPortConnected()));
connect(this, SIGNAL(newDataReady(QByteArray, quint64)), mainWindow, SLOT(receivedData(QByteArray, quint64)));
connect(this, SIGNAL(newTimeStamp(qint64)), mainWindow, SLOT(updateTimer(qint64)));
emit serialPortOpened();
}
// Connect signals
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readData()));
connect(mainWindow, SIGNAL(baudRateChanged(int)), this, SLOT(on_baudRateUpdated(int)));
connect(mainWindow, SIGNAL(dataBitsChanged(QSerialPort::DataBits)), this, SLOT(on_dataBitsUpdated(QSerialPort::DataBits)));
connect(mainWindow, SIGNAL(parityChanged(QSerialPort::Parity)), this, SLOT(on_parityUpdated(QSerialPort::Parity)));
connect(mainWindow, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(on_stopBitsUpdated(QSerialPort::StopBits)));
}
SerialLogging::~SerialLogging()
{
// Close out the file
file.close();
// 'delete' closes the serial port if it is still open
delete serialPort;
}
void SerialLogging::saveToDisk(QByteArray data)
{
// ToDo: Handle the case where lenth(data) > 255
quint32 ellapsedTicks = startTime.msecsTo(QDateTime::currentDateTimeUtc())/10;
QByteArray packet;
packet.append(SYNC0);
packet.append(SYNC1);
packet.append(reinterpret_cast<const char*>(&ellapsedTicks), sizeof(ellapsedTicks));
packet.append(data.size());
packet.append(data);
packet.append(END_MARKER);
// If the file is open, write the data
if (file.isOpen()) {
file.write(packet, packet.size());
}
}
void SerialLogging::on_baudRateUpdated(int baudRate)
{
serialPort->setBaudRate(baudRate);
}
void SerialLogging::on_dataBitsUpdated(QSerialPort::DataBits dataBits)
{
serialPort->setDataBits(dataBits);
}
void SerialLogging::on_parityUpdated(QSerialPort::Parity parity)
{
serialPort->setParity(parity);
}
void SerialLogging::on_stopBitsUpdated(QSerialPort::StopBits stopBits)
{
serialPort->setStopBits(stopBits);
}
void SerialLogging::readData()
{
// Get timestamp immediately
quint64 timeStamp = startTime.msecsTo(QDateTime::currentDateTimeUtc());
// Read data from serial port
QByteArray data = serialPort->readAll();
// Save the data to file
saveToDisk(data);
// Send the data to any connected listeners
emit newDataReady(data, timeStamp);
// Send out the elapsed time to any connected listeners
emit newTimeStamp(timeStamp);
}