Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt and STM32 Sending and Receive Problem
Forum Updated to NodeBB v4.3 + New Features

Qt and STM32 Sending and Receive Problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 175 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mateusarjunaci
    wrote on last edited by
    #1

    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 dikirim

    if (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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved