This repository was archived by the owner on May 14, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathserialhandler.cpp
More file actions
155 lines (134 loc) · 4.71 KB
/
Copy pathserialhandler.cpp
File metadata and controls
155 lines (134 loc) · 4.71 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
#include "serialhandler.h"
#include <QDebug>
#include <QDialog>
#include <QVBoxLayout>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QSerialPortInfo>
#include <QFile>
SerialHandler::SerialHandler(QObject *parent)
: QObject(parent), serial(new QSerialPort(this)) {
}
void SerialHandler::selectSerialPort() {
QDialog dialog;
dialog.setWindowTitle("Seleccionar Puerto Serie");
// Aplicar tema oscuro
dialog.setStyleSheet(R"(
QDialog {
background-color: #2e2e2e;
color: #ffffff;
}
QLabel, QComboBox, QPushButton {
color: #ffffff;
}
QComboBox {
background-color: #3c3c3c;
border: 1px solid #5e5e5e;
}
QComboBox QAbstractItemView {
background-color: #3c3c3c;
selection-background-color: #5e5e5e;
color: #ffffff;
}
QDialogButtonBox QPushButton {
background-color: #5e5e5e;
border: 1px solid #5e5e5e;
color: #ffffff;
}
QDialogButtonBox QPushButton:hover {
background-color: #757575;
}
)");
QVBoxLayout layout(&dialog);
// ComboBox para listar los puertos serie disponibles
QComboBox comboBox;
const auto ports = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &port : ports) {
comboBox.addItem(port.portName());
}
// Agregar puertos PTY manualmente
for (int i = 0; i < 256; ++i) {
QString ptName = QString("/dev/pts/%1").arg(i);
if (QFile::exists(ptName)) {
comboBox.addItem(ptName);
}
}
layout.addWidget(&comboBox);
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
connect(&buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
layout.addWidget(&buttonBox);
if (dialog.exec() == QDialog::Accepted) {
QString selectedPortName = comboBox.currentText();
if (!selectedPortName.isEmpty()) {
if(connectSerialPort(selectedPortName))
qDebug() << "Error para conectarse a: " << selectedPortName;
else
qDebug() << "Conectado a: " << selectedPortName;
} else {
qDebug() << "No se seleccionó ningún puerto.";
}
}
}
#include <QElapsedTimer>
#include <QThread>
int SerialHandler::connectSerialPort(const QString &portName) {
if (serial->isOpen()) {
serial->close();
qDebug() << "Cerrando conexión en el puerto anterior.";
}
serial->setPortName(portName);
serial->setBaudRate(QSerialPort::Baud115200);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->open(QIODevice::ReadWrite)) {
qDebug() << "Conexión serial abierta en" << portName;
serial->waitForReadyRead(4000);
QByteArray initResponse = serial->readAll();
sendToArduino("SAPEI_INIT");
if (serial->waitForReadyRead(3000)) {
QByteArray connectedResponse = serial->readAll();
if (connectedResponse.contains("CONNECTED")) {
qDebug() << "Conexión verificada: 'CONNECTED' recibido.";
connect(serial, &QSerialPort::readyRead, this, &SerialHandler::handleReadyRead);
return 0;
} else {
qDebug() << "No se recibió respuesta 'CONNECTED'.";
serial->close();
return 1;
}
} else {
qDebug() << "Timeout: No se recibió 'CONNECTED'.";
serial->close();
return 1;
}
} else {
qDebug() << "No se pudo abrir la conexión serial en" << portName;
qDebug() << "Error:" << serial->errorString();
return 1;
}
return 1;
}
void SerialHandler::sendToArduino(const QString &message) {
if (serial->isOpen()) {
QByteArray byteArray = (message + "\n").toUtf8();
serial->write(byteArray);
if (!serial->waitForBytesWritten(1000)) {
qDebug() << "Error al enviar datos al Arduino:" << serial->errorString();
}
} else {
qDebug() << "El puerto serie no está abierto.";
}
}
void SerialHandler::handleReadyRead() {
while (serial->canReadLine()) {
QString line = serial->readLine().trimmed();
emit idReceived(line);
qDebug() << "Datos leídos del puerto serie:" << line;
}
}
bool SerialHandler::isConnected() const {
return serial->isOpen();
}