Hello.

I have a problem with NucleoF446RE namely the Qt program will only work once. After connecting, I can only light the diode once, then I have to disconnect and connect the device to do anything over and over again. What could be the problem ?? It looks like I have a clogged port.

With HyperTerminal Nukleo works fine...

Ps.

Sorry for English, I'm from Poland and I'm looking for help here because I found a bug in Qt

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QList>
#include <QSerialPortInfo>
#include <QDateTime>
#include <QtSerialPort>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->device = new QSerialPort(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButtonSearch_clicked()
{
ui->comboBoxDevices->clear();

this->addToLogs("Szukam urz?dze?...");

QList<QSerialPortInfo> devices;
devices = QSerialPortInfo::availablePorts();


for(int i = 0; i < devices.count(); i++)
{
this->addToLogs("Znalaz?em urz?dzenie: " + devices.at(i).portName() + " " + devices.at(i).description());
ui->comboBoxDevices->addItem(devices.at(i).portName() + "\t" + devices.at(i).description());
}
}

void MainWindow::addToLogs(QString message)
{
QString currentDateTime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss");
ui->textEditLogs->append(currentDateTime + "\t" + message);

}

void MainWindow::sendMessageToDevice(QString message)

{

if(this->device->isOpen() && this->device->isWritable())
{
this->addToLogs("Wysy?am informacje do urz?dzenia " + message);
this->device->write(message.toStdString().c_str());

}
else
{
this->addToLogs("Nie mog? wys?a? wiadomo?ci. Port nie jest otwarty!");
}
return;
}

void MainWindow::on_pushButtonConnect_clicked()
{
if(ui->comboBoxDevices->count() == 0)
{
this->addToLogs("Nie wykryto ?adnych urz?dze?!");
return;
}

QString comboBoxQString = ui->comboBoxDevices->currentText();
QStringList portList = comboBoxQString.split("\t");
QString portName = portList.first();

this->device->setPortName(portName);

// OTWÓRZ I SKONFIGURUJ PORT:
if(!device->isOpen())
{
if(device->open(QSerialPort::ReadWrite))
{
this->device->setBaudRate(QSerialPort::Baud9600);
this->device->setDataBits(QSerialPort::Data8);
this->device->setParity(QSerialPort::NoParity);
this->device->setStopBits(QSerialPort::OneStop);
this->device->setFlowControl(QSerialPort::NoFlowControl);


// CONNECT:
connect(this->device, SIGNAL(readyRead()),
this, SLOT(readFromPort()));

this->addToLogs("Otwarto port szeregowy.");
}
else
{
this->addToLogs("Otwarcie porty szeregowego si? nie powiod?o!");
}
}
else
{
this->addToLogs("Port ju? jest otwarty!");
return;
}
}

void MainWindow::readFromPort()
{
while(this->device->canReadLine())
{
QString line = this->device->readLine();
//qDebug() << line;

QString terminator = "\r";
int pos = line.lastIndexOf(terminator);
//qDebug() << line.left(pos);

this->addToLogs(line.left(pos));
}
}

void MainWindow::on_pushButtonCloseConnection_clicked()
{
if(this->device->isOpen())
{
this->device->close();
this->addToLogs("Zamkni?to po??czenie.");
}
else
{
this->addToLogs("Port nie jest otwarty!");
return;
}
}

void MainWindow::on_pushButtonLedOn_clicked()

{

this->sendMessageToDevice("1");

}

void MainWindow::on_pushButtonLedOff_clicked()
{

this->sendMessageToDevice("0");
}

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSerialPort>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_pushButtonSearch_clicked();

void on_pushButtonConnect_clicked();

void readFromPort();

void on_pushButtonCloseConnection_clicked();

void on_pushButtonLedOn_clicked();

void on_pushButtonLedOff_clicked();

private:
Ui::MainWindow *ui;

void addToLogs(QString message);

QSerialPort *device;
void sendMessageToDevice(QString message);

};

#endif // MAINWINDOW_H