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. Open serial port when clicked the button
Forum Updated to NodeBB v4.3 + New Features

Open serial port when clicked the button

Scheduled Pinned Locked Moved Solved General and Desktop
serialportbuttoncrashed
4 Posts 2 Posters 788 Views
  • 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 Offline
    D Offline
    deleted286
    wrote on 17 Feb 2021, 13:26 last edited by
    #1

    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
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 Feb 2021, 13:27 last edited by Christian Ehrlicher
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 1 Reply Last reply 17 Feb 2021, 13:29
      2
      • C Christian Ehrlicher
        17 Feb 2021, 13:27

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

        D Offline
        D Offline
        deleted286
        wrote on 17 Feb 2021, 13:29 last edited by
        #3

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

        C 1 Reply Last reply 17 Feb 2021, 13:30
        0
        • D deleted286
          17 Feb 2021, 13:29

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

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 17 Feb 2021, 13:30 last edited by
          #4

          @suslucoder Then mark the topic as solved.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          3/4

          17 Feb 2021, 13:29

          • Login

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