Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Open serial port when clicked the button

    General and Desktop
    serialport button crashed
    2
    4
    236
    Loading More Posts
    • 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.
    • D
      deleted286 last edited by

      Why my app crashed when i clicked the push button?
      I want to open a port when i clicked the button. Program is crash every time.

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QtSerialPort/QSerialPort>
      #include <QDebug>
      #include <QSerialPortInfo>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          
          
      }
      MainWindow::~MainWindow()
      {
          delete ui;
          serial->close();
      }
      
      void MainWindow::on_ports_activated(const QString &arg1)
      {
      ui->ports->clear();
      const auto infos = QSerialPortInfo::availablePorts();
      for(const QSerialPortInfo &info : infos )
      {
          QString s = info.portName();
          ui->ports->addItem(s);
      }
      if (!ui->ports->count())
                  ui->ports->addItem(tr("no com"));
      
      }
      
      void MainWindow::on_pushButton_clicked()
      {
          serial->setPortName("/dev/ttyS1");
          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)) {
                qDebug()<<"Serial Port Open ";
                serial->clear();
          }
          else
          {
                qDebug()<<"Serial  Port Not Open !!!!";
          }
      }
      
      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by Christian Ehrlicher

        C++ basics - where do you initialize your member serial ?

        Qt has to stay free or it will die.

        D 1 Reply Last reply Reply Quote 2
        • D
          deleted286 @Christian Ehrlicher last edited by

          @Christian-Ehrlicher Oh yes i forgot it. I initialize it now. Thank you

          Christian Ehrlicher 1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion @deleted286 last edited by

            @suslucoder Then mark the topic as solved.

            Qt has to stay free or it will die.

            1 Reply Last reply Reply Quote 1
            • First post
              Last post