-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialread.cpp
226 lines (191 loc) · 6.77 KB
/
serialread.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
/***************************************************************************
serialread.cpp - description
-------------------
begin : april 2020
copyright : (C) 2020 by Jaime Robles
email : jaime@robles.es
***************************************************************************/
/*****************************************************************************
* This file is part of KMeter. *
* *
* KMeter 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. *
* *
* KMeter 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 KMeter. If not, see <https://www.gnu.org/licenses/>. *
* *
*****************************************************************************/
#include "serialread.h"
// http://ea7tb.com/index.php/2020/04/26/lp-100a-puerto-serie/
SerialRead::SerialRead()
{
serialPort = new QSerialPort;
bauds = 115200;
bits = 8;
stopBit = 1;
parity = false;
timer = new QTimer(this);
createActions();
//connect(timer, SIGNAL(timeout()), this, SLOT(slotTimeOut()) );
}
SerialRead::~SerialRead()
{}
void SerialRead::createActions()
{
connect(timer, SIGNAL(timeout()), this, SLOT(slotTXTimeOut()));
connect(serialPort, SIGNAL(readyRead()), this, SLOT(slotRXReadyRead()));
connect(serialPort, SIGNAL(errorOccurred(QSerialPort::SerialPortError)), this, SLOT(slotRXHandleError(QSerialPort::SerialPortError)));
//serialPort = new QSerialPort();
}
void SerialRead::setSerialPort(const QString &_p)
{
qDebug() << "SerialRead::setSerialPort: " << _p << endl;
serialPort->clearError();
serialPortString = _p.toLocal8Bit().constData();
//serialPort->toLocal8Bit().constData()
serialPort->setPortName(_p.toLocal8Bit().constData());
serialPort->setBaudRate(bauds);
serialPort->setStopBits(QSerialPort::OneStop);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->clearError();
bool opened = serialPort->open(QIODevice::ReadWrite);
if (opened)
{
qDebug() << "SerialRead::sendData: OPENED" << endl;
emit serialPortOpened(true);
}
else
{
emit serialPortOpened(false);
qDebug() << "SerialRead::sendData: NOT OPENED: " << QString::number(serialPort->error()) << endl;
}
//bool opened = serialPort->open(QIODevice::WriteOnly);
}
void SerialRead::slotTXTimeOut()
{
qDebug() << "SerialRead::slotTimeOut " << endl;
timer->stop();
}
void SerialRead::sendM()
{
qDebug() << "SerialRead::sendM" << endl;
sendData("M");
}
void SerialRead::sendP()
{
//qDebug() << "SerialRead::sendP" << endl;
sendData("P");
}
void SerialRead::sendA()
{
qDebug() << "SerialRead::sendA" << endl;
sendData("A");
}
void SerialRead::sendF()
{
qDebug() << "SerialRead::sendA" << endl;
sendData("F");
}
void SerialRead::sendData(const QString &_d)
{
//qDebug() << "SerialRead::sendData: " << _d << endl;
QString dataToSend;
if (_d == "P")
{
dataToSend = "P";
}
else if (_d == "M")
{
dataToSend = "M";
}
else if (_d == "A")
{
dataToSend = "A";
}
else if (_d == "F")
{
dataToSend = "F";
}
else
{
return;
}
QByteArray writeData;
writeData.clear();
writeData.append(dataToSend.toLocal8Bit());
if ((!writeData.isEmpty()) && (serialPort->isOpen()))
{
//qDebug() << "SerialRead::sendData: Writing... : " << endl;
serialPort->write(writeData);
timer->start(5000);
}
else
{
if (!serialPort->isOpen())
{
qDebug() << "SerialRead::sendData: Serial port is closed" << endl;
emit serialPortOpened(false);
}
}
}
void SerialRead::slotRXTimeOut()
{
qDebug() << "SerialRead::slotRXTimeOut" << endl;
}
void SerialRead::slotRXReadyRead()
{
//qDebug() << "SerialRead::slotRXReadyRead" << endl;
m_readData.append(serialPort->readAll());
parseAnswer(m_readData);
//qDebug() << "SerialRead::slotRXReadyRead" << m_readData << endl;
//qDebug() << "SerialRead::slotRXReadyRead - length: " << QString::number(m_readData.length()) << endl;
//";0000.00,046.9,087.5,3,EA4K ,2,1,-2.3,1.00"
//if (!timer.isActive())
// timer.start(5000)
}
void SerialRead::slotRXHandleError(QSerialPort::SerialPortError error)
{
qDebug() << "SerialRead::slotRXHandleError" << endl;
if (error == QSerialPort::ReadError) {
qDebug() << QObject::tr("An I/O error occurred while reading "
"the data from port %1, error: %2")
.arg(serialPort->portName())
.arg(serialPort->errorString())
<< endl;
//it errorSignal;
}
}
void SerialRead::parseAnswer(const QByteArray &_d)
{ //https://ea7tb.com/index.php/2020/04/26/lp-100a-puerto-serie/
//qDebug() << "SerialRead::parseAnswer: " << QString::number(_d.length()) << endl;
if ((_d.startsWith(';')) && (_d.length() == 43) )
{
//qDebug() << "SerialRead::parseAnswer: COMPLETE: " << _d << endl;
QString _data = _d.right(_d.length()-1);
//qDebug() << "SerialRead::parseAnswer: DATA: " << _data << endl;
//DATA: "0000.00,046.9,087.6,3,EA4K ,2,1,-2.3,1.00"
QStringList fields;
fields.clear();
fields << _data.split(',');
emit meterData(fields);
m_readData.clear();
//qDebug() << "SerialRead::parseAnswer: meterData emitted " << endl;
// m_readData.clear();
}
else if (_d.startsWith(';'))
{
//qDebug() << "SerialRead::parseAnswer: Receiving...: " << _d << endl;
}
else
{
//qDebug() << "SerialRead::parseAnswer: Non valid data: " << _d << endl;
}
}