Qt and STM32 Sending and Receive Problem
-
Hello everyone, im build this project for my project, received function from qt can receive data from stm32, but my project has problem with the send data from qt to stm32, my stm32 not receive data from qt
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMenuBar>
#include <QAction>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, ledState(false)
{
ui->setupUi(this);// Inisialisasi port serial serial = new QSerialPort(this); // Membuat menu dan aksi untuk memilih port serial QMenu *serialMenu = menuBar()->addMenu("Port"); QAction *selectPortAction = new QAction("Select Port", this); serialMenu->addAction(selectPortAction); connect(selectPortAction, &QAction::triggered, this, &MainWindow::on_selectPort_triggered); connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
}
MainWindow::~MainWindow()
{
if (serial->isOpen()) {
serial->close();
}
delete ui;
}void MainWindow::on_selectPort_triggered()
{
QList<QSerialPortInfo> availablePorts = QSerialPortInfo::availablePorts();if (availablePorts.isEmpty()) { QMessageBox::warning(this, "No Ports", "Tidak ada port serial yang tersedia."); return; } bool ok; QStringList portNames; for (const QSerialPortInfo &portInfo : availablePorts) { portNames << portInfo.portName(); } QString selectedPort = QInputDialog::getItem(this, "Select Port", "Pilih port serial:", portNames, 0, false, &ok); if (ok && !selectedPort.isEmpty()) { serial->setPortName(selectedPort); 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)) { QMessageBox::information(this, "Port Opened", "Port serial berhasil dibuka."); } else { QMessageBox::critical(this, "Error", "Gagal membuka port serial."); } }
}
void MainWindow::onReadyRead()
{
QByteArray dataReceived = serial->readAll();
QString receivedString = QString::fromLatin1(dataReceived);
ui->textEdit->setPlainText(receivedString);}
void MainWindow::on_pushButton_clicked()
{
QString dataToSend = "Hello STM32"; // Ganti dengan string yang ingin dikirimif (serial->isOpen()) { serial->write(dataToSend.toUtf8()); serial->flush(); QMessageBox::information(this, "Data Sent", "String berhasil dikirim."); } else { QMessageBox::warning(this, "Error", "Port serial belum dibuka."); }
}
this my code
-
Hi and welcome to devnet,
Which version of Qt ?
On which OS ?
You should also connect the errorOccurred signal to ensure there's not something happening that you missed.