0

I want to write to a serial port . By using hercules, I have opened the port "COM6" and when I run the program, I can see the text "ok" in hercules window. But when I put the line "serial->write("ok");" inside a while loop, even my mainwindow isn't poped up. I would appreciate if someone could help me fix this problem

my code:

widget.h:

 #ifndef WIDGET_H
 #define WIDGET_H

 #include <QWidget>
 #include <QtSerialPort/QSerialPort>

 namespace Ui {
 class Widget;
   }

class Widget : public QWidget
 {
   Q_OBJECT

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

 QSerialPort *serial;

private slots:
   void on_pushButton_clicked();


private:
    Ui::Widget *ui;
};

 #endif // WIDGET_H

widget.cpp:

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QSerialPortInfo>

Widget::Widget(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::Widget)
  {
        ui->setupUi(this);

    serial = new QSerialPort(this);

    serial->setPortName("COM1");

    for(const auto &serialPortInfo : QSerialPortInfo::availablePorts())
    {
            qDebug() << "find serial port: " << serialPortInfo.portName() ;
    }

    serial->open(QIODevice::ReadWrite);

    if(serial->isOpen())
    {
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);

        while(1)
          {
             serial->write("ok");
             
          }
    }

    else
    {
      qDebug() << "can't open the port";
    }

}

Widget::~Widget()
{
   delete ui;
  serial->close();
 }

void Widget::on_pushButton_clicked()
{
    qDebug() << "salam";
}

main.cpp:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   Widget w;
    w.show();

  return a.exec();
}
Nanor
  • 37
  • 1
  • 6
  • Why are you surprised that your Widget never shows up when you have `while(1)` in its constructor? If you want to write stuff to the serial port in an endless loop _AND_ have a responsive UI, then you'll need to kick off a thread to do the loop. – paddy Jan 25 '21 at 07:18

2 Answers2

0

Only when this constructor is done, the UI can show up, but it blocked by the endless while loop.You need to move the while loop to another place, a manual trigger function or a thread will be OK.

potter lv
  • 11
  • 1
  • Please describe how a "manual trigger function" that executes an infinite loop "will be OK" – paddy Jan 25 '21 at 07:36
  • I deleted the while loop inside the constructor and only one time I wrote "ok" inside the constructor, then using signals and slots, I connected the bytesWritten(qint64) signal to a slot and inside that slot I wrote my while loop. But still it crashes – Nanor Jan 25 '21 at 07:41
  • @paddy For example, you can use a button press event or a timer, to delay the trigger. – potter lv Jan 25 '21 at 08:25
  • If it's an infinite loop then that will block the UI thread, because of how the Qt event pump works. The only useful option is to make the operation non-blocking or put it in another thread. – paddy Jan 25 '21 at 09:33
0

Move your sending function to your on_pushButton_clicked slot to send your message when you click on your button.

void Widget::on_pushButton_clicked()
{
    qDebug() << "salam";
    serial->write("ok");
}

Also if you want to send something in a loop you can define a QTimer and send you message when QTimer triggered.

You can use this answer to implement QTimer.

Farshid616
  • 1,404
  • 1
  • 14
  • 26