-
Notifications
You must be signed in to change notification settings - Fork 2
/
serialconnect.cpp
275 lines (233 loc) · 9.5 KB
/
serialconnect.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
#include "serialconnect.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QStringList>
#include <QVariant>
#include <QDebug>
#include <QTimerEvent>
SerialConnect::SerialConnect(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort;
connectSerialPort();
availablePortsList.append("-");
}
void SerialConnect::connectSerialPort() {
connect(serial, SIGNAL(baudRateChanged(qint32,QSerialPort::Directions)), this, SLOT(storeSerialSettings(qint32,QSerialPort::Directions)));
connect(serial, SIGNAL(dataBitsChanged(QSerialPort::DataBits)), this, SLOT(storeSerialSettings(QSerialPort::DataBits)));
connect(serial, SIGNAL(flowControlChanged(QSerialPort::FlowControl)), this, SLOT(storeSerialSettings(QSerialPort::FlowControl)));
connect(serial, SIGNAL(parityChanged(QSerialPort::Parity)), this, SLOT(storeSerialSettings(QSerialPort::Parity)));
connect(serial, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(storeSerialSettings(QSerialPort::StopBits)));
// connect(serial,SIGNAL(readyRead()), this, SLOT(read()));
connect(serial,SIGNAL(readyRead()), this, SLOT(reduceReads())); //?? trying to replace connection above to reduce read calls (only one call every 50ms
}
/*********************************************
*
* Signals are only declared in Header file
*
********************************************/
/*********************************************
*
* Slots
*
********************************************/
void SerialConnect::open() {
qDebug() << "serialConnect:open() slot called";
QString statusTextTemp;
if(serial->isOpen()) {
statusTextTemp = "Serial port ";
statusTextTemp.append(serial->portName());
statusTextTemp.append(" is already open");
}
else {
//settings("/dev/ttyACM0", 9600, 8, 0, 1, 0);
//settings("/dev/ttyACM0", 1200);
if (serial->open(QIODevice::ReadWrite)) {
statusTextTemp = "Serial port opened: ";
statusTextTemp.append(serial->portName());
opened();
}
else {
statusTextTemp = "Error opening serial port: ";
statusTextTemp.append(serial->errorString() );
statusTextTemp.append(" - port: ");
statusTextTemp.append(serial->portName());
}
}
sendStatusText( statusTextTemp );
qDebug() << statusTextTemp;
}
void SerialConnect::close() {
QString statusTextTemp;
if(serial->isOpen()) {
serial->close();
statusTextTemp = "Serial port closed";
}
else {
statusTextTemp = "No serial port open";
}
sendStatusText( statusTextTemp );
qDebug() << statusTextTemp;
}
void SerialConnect::getPortSettings(QString port) {
serial->setPortName(port);
}
void SerialConnect::write(const QString data) {
QByteArray dataArray;
dataArray.append(data);
qint64 bytesWritten = serial->write(dataArray);
// serial->write("\r");
if (bytesWritten == (-1)) {
qDebug() << "Error writing data to serial port: " << serial->errorString();
}
qDebug() << "SerialConnect::write(QString) Text received: " << data;
qDebug() << "SerialConnect::write(QString) Text sent as QByteArray: " << dataArray;
qDebug() << "Number of bytes written: " << bytesWritten;
}
void SerialConnect::write(const QByteArray &data) {
qint64 bytesWritten = serial->write(data);
if (bytesWritten == (-1)) {
qDebug() << "Error writing data to serial port: " << serial->errorString();
}
qDebug() << "SerialConnect::write(QByteArray) Text received: " << data;
qDebug() << "Number of bytes written: " << bytesWritten;
}
void SerialConnect::reduceReads() {
if(timer.isActive()) {
return;
}
else {
timer.start(50, this);
}
}
void SerialConnect::read() {
newSerialDataString = serial->readAll();
serialDataString.append(newSerialDataString);
serialDataChanged();
// qDebug() << "Data in QByteArray = " << newdata;
// qDebug() << "Data in serialDataString = " << serialDataString;
}
void SerialConnect::getBaudSettings(qint32 baud) {
if (!serial->setBaudRate(baud)) {
qDebug() << "When setting baud rate: " << baud << " - error: " << serial->errorString();
}
}
void SerialConnect::getBaudSettings(QString baud) {
if (!serial->setBaudRate(baud.toInt())) {
qDebug() << "When setting baud rate: " << baud.toInt() << " - error: " << serial->errorString();
}
}
void SerialConnect::getBitSettings(qint8 bit) {
if (!serial->setDataBits(static_cast<QSerialPort::DataBits>(bit))) {
qDebug() << "When setting data bits: " << bit << " - error: " << serial->errorString();
}
}
void SerialConnect::getBitSettings(QString bit) {
if (!serial->setDataBits(static_cast<QSerialPort::DataBits>(bit.toInt()))) {
qDebug() << "When setting data bits: " << bit.toInt() << " - error: " << serial->errorString();
}
}
void SerialConnect::getParitySettings(qint8 parity) {
if (!serial->setParity(static_cast<QSerialPort::Parity>(parity))) {
qDebug() << "When setting parity: " << parity << " - error: " << serial->errorString();
}
}
void SerialConnect::getParitySettings(QString parity) {
if (!serial->setParity(static_cast<QSerialPort::Parity>(parity.toInt()))) {
qDebug() << "When setting parity: " << parity.toInt() << " - error: " << serial->errorString();
}
}
void SerialConnect::getStopbitsSettings(qint8 stopbits) {
if (!serial->setStopBits(static_cast<QSerialPort::StopBits>(stopbits))) {
qDebug() << "When setting stop bits: " << stopbits << " - error: " << serial->errorString();
}
}
void SerialConnect::getStopbitsSettings(QString stopbits) {
if (!serial->setStopBits(static_cast<QSerialPort::StopBits>(stopbits.toInt()))) {
qDebug() << "When setting stop bits: " << stopbits.toInt() << " - error: " << serial->errorString();
}
}
void SerialConnect::getFlowcontrolSettings(qint8 flowcontrol) {
if (!serial->setFlowControl(static_cast<QSerialPort::FlowControl>(flowcontrol))) {
qDebug() << "When setting flow control: " << flowcontrol << " - error: " << serial->errorString();
}
}
void SerialConnect::getFlowcontrolSettings(QString flowcontrol) {
if (!serial->setFlowControl(static_cast<QSerialPort::FlowControl>(flowcontrol.toInt()))) {
qDebug() << "When setting flow control: " << flowcontrol.toInt() << " - error: " << serial->errorString();
}
}
void SerialConnect::storeSerialSettings(qint32 baudRate, QSerialPort::Directions dir) {
currentSettings.baudRate = baudRate;
QString statusTextTemp;
statusTextTemp = "Baud rate changed: ";
statusTextTemp.append(QString("%1").arg(currentSettings.baudRate));
sendStatusText( statusTextTemp );
qDebug() << statusTextTemp;
}
void SerialConnect::storeSerialSettings(QSerialPort::DataBits dataBits) {
currentSettings.dataBits = dataBits;
QString statusTextTemp;
statusTextTemp = "Data bits changed: ";
statusTextTemp.append(QString("%1").arg(currentSettings.dataBits));
sendStatusText( statusTextTemp );
qDebug() << statusTextTemp;
}
void SerialConnect::storeSerialSettings(QSerialPort::Parity parity) {
currentSettings.parity = parity;
QString statusTextTemp;
statusTextTemp = "Parity changed: ";
statusTextTemp.append(QString("%1").arg(currentSettings.parity));
sendStatusText( statusTextTemp );
qDebug() << statusTextTemp;
}
void SerialConnect::storeSerialSettings(QSerialPort::StopBits stopbits) {
currentSettings.stopBits = stopbits;
QString statusTextTemp;
statusTextTemp = "Stop bits changed: ";
statusTextTemp.append(QString("%1").arg(currentSettings.stopBits));
sendStatusText( statusTextTemp );
qDebug() << statusTextTemp;
}
void SerialConnect::storeSerialSettings(QSerialPort::FlowControl flowcontrol) {
currentSettings.flowControl = flowcontrol;
QString statusTextTemp;
statusTextTemp = "Flow control changed: ";
statusTextTemp.append(QString("%1").arg(currentSettings.flowControl));
sendStatusText( statusTextTemp );
qDebug() << statusTextTemp;
}
void SerialConnect::findAvailablePorts(bool withSystemPorts) {
qDebug() << "SerialConnect::findAvailablePorts() called withSystemPorts = " << withSystemPorts;
availablePortsList.clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
if(!withSystemPorts) {
if(!info.portName().contains("ttyS")) {
availablePortsList.append(info.portName());
//sendAvailablePort((QVariant) info.portName());
}
}
else {
availablePortsList.append(info.portName());
//sendAvailablePort((QVariant) info.portName());
}
}
availablePortsChanged();
qDebug() << "SerialConnect::availablePortsList = "<< availablePortsList;
// further info functions provided by serail class but not used info.systemLocation() << info.description() << info.manufacturer() << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());
}
void SerialConnect::timerEvent(QTimerEvent *event)
{
if (event->timerId() == timer.timerId()) {
// timer event is from timer set by object
timer.stop();
read();
} /*else {
QWidget::timerEvent(event); //if you want to use widgets in an application, uncomment this line
}*/
}
/******************************************************
*
* Private Functions
*
*****************************************************/