Results 1 to 5 of 5

Thread: STM32 Nucleo F446RE - Serial Port Problem

  1. #1
    Join Date
    Nov 2019
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default STM32 Nucleo F446RE - Serial Port Problem

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,199
    Thanks
    237
    Thanked 668 Times in 658 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: STM32 Nucleo F446RE - Serial Port Problem

    I found a bug in Qt
    Probably not. Most people are quick to blame Qt when the problem is in their own code.

    Do you need to flush the port after you write to it? Are you sure that HyperTerminal is using -exactly- the same port setup as your code? You are opening your port as read/write, but you say you are lighting an LED. That sounds like a write-only operation. What are you reading?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2019
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: STM32 Nucleo F446RE - Serial Port Problem

    Reads from the Stm in the logs of the written program "active" "" off "" incorrect character ". I have the same port, I open it correctly once. Manual reset of the device (unplug and reconnect) or software (rebinding) allows status change.
    A bug found on the Polish forum, their suggestion, that's why I came here..
    Last edited by dernis; 20th November 2019 at 10:21.

  4. #4
    Join Date
    Nov 2019
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: STM32 Nucleo F446RE - Serial Port Problem

    Ok ... I found my "bug". I added:

    Qt Code:
    1. this->device->waitForBytesWritten();
    To copy to clipboard, switch view to plain text mode 

    and everything works.


  5. #5
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: STM32 Nucleo F446RE - Serial Port Problem

    Ok ... I found my "bug". I added:
    Nonono. If You use Windows && Qt 5.13.1/5.12.5 then it is a bug in QSP (just update Qt to latest version 5.13.2/5.12.6 or to use a previous version 5.13.0/5.12.4).

    waitForBytesWritten()
    Don't do that.

Similar Threads

  1. Problem sending data over to serial port
    By hovuquocan1997 in forum Qt Programming
    Replies: 6
    Last Post: 28th July 2015, 18:49
  2. Replies: 0
    Last Post: 6th March 2014, 08:38
  3. qext-serial-port write problem.
    By rex in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2013, 08:18
  4. QT to Android serial port API problem?
    By eddis in forum Newbie
    Replies: 5
    Last Post: 5th January 2012, 01:09
  5. Serial Port Reading problem
    By sfabel in forum Qt Programming
    Replies: 12
    Last Post: 18th February 2010, 15:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.