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
QtWS25 Last Chance

Open serial port when clicked the button

Scheduled Pinned Locked Moved Solved General and Desktop
serialportbuttoncrashed
4 Posts 2 Posters 734 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 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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      2
      • Christian EhrlicherC Christian Ehrlicher

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

        D Offline
        D Offline
        deleted286
        wrote on last edited by
        #3

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

        Christian EhrlicherC 1 Reply Last reply
        0
        • D deleted286

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

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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

          • Login

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