Skip to content

Instantly share code, notes, and snippets.

@esmarr58
Created September 17, 2025 15:04
Show Gist options
  • Select an option

  • Save esmarr58/4c062aec0734145b2a8a2292baaff675 to your computer and use it in GitHub Desktop.

Select an option

Save esmarr58/4c062aec0734145b2a8a2292baaff675 to your computer and use it in GitHub Desktop.
void MainWindow::on_pushButton_clicked()
{
if (!serialActual || !serialActual->isOpen()) {
QMessageBox::warning(this, "Error", "No hay ningún puerto abierto.");
return;
}
// Tomar el texto escrito por el usuario (ej: {"sensor":"dht22","pin":4})
QString jsonStr = ui->lineEdit->text().trimmed();
if (jsonStr.isEmpty()) {
QMessageBox::information(this, "Aviso", "Por favor escribe un mensaje JSON.");
return;
}
// Convertir a bytes y agregar un salto de línea
QByteArray datos = jsonStr.toUtf8() + "\n";
// Enviar al puerto serial
qint64 enviados = serialActual->write(datos);
if (enviados == -1)
QMessageBox::warning(this, "Error", "No se pudo enviar el mensaje.");
else
qDebug() << "JSON enviado:" << jsonStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment