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. [SOLVED] QSerialPort Crash when i click button or close application
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QSerialPort Crash when i click button or close application

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.6k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    matijalazic
    wrote on last edited by
    #1

    Hello,
    So I have been working in Qt for quite a long time now. But until last week I was only working with developing simple console applications. I also like to have fun with electronic devices and stuff. So i made an electronic device which connects to PC over Arduino UNO. I need to read data with C++ GUI program. I have looked at a lot of examples and did exactly the same but my program keeps crashing when I click the button. Button is supposed to connect to Serial port COM3. But it crashes. I have been looking for a solution for the last 3 hours so I wouldnt waste your time with questions already answered (I did howsoever find something about crashing after program closes). I really appreciate all your responses.

    Screenshot:
    !http://shrani.si/f/3o/Xv/f2Y2jOc/trest.png(picture)!

    MainWindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    #include <QtCore/QCoreApplication>
    #include <QtCore/QDebug>

    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    void set_label_2_text(QString tekst);
    void set_label_text(QString tekst);
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void readData();
    void on_pushButton_clicked();

    private:
    bool conn;
    QSerialPort *serial;
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    MainWindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QtCore/QCoreApplication>
    #include <QtCore/QDebug>

    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>

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

    conn = false;
    
    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    

    }

    void MainWindow::set_label_text(QString tekst)
    {
    QFont f( "Arial", 12);
    ui->label->setFont(f);
    ui->label->setText(tekst);
    }
    void MainWindow::set_label_2_text(QString tekst)
    {
    ui->label_2->setText(tekst);
    }

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

    void MainWindow::readData()
    {
    if(conn == true) {
    QByteArray data = serial->readAll();
    ui->label_2->setText((QString) data);
    }
    }

    void MainWindow::on_pushButton_clicked()
    {
    serial->setPortName("COM3");
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    if(serial->open(QIODevice::ReadWrite))
    conn = true;
    /*foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    qDebug() << "Name : " << info.portName();
    qDebug() << "Description : " << info.description();
    qDebug() << "Manufacturer: " << info.manufacturer();

            if(info.description() == "USB-SERIAL CH340") {
                set_label_text("CONNECTED! on " + info.portName());
    
                serial->setPort(info);
                serial->open(QIODevice::ReadWrite);
                connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
            }
        }*/
    

    }
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      What object is your "serial" pointing to?

      1 Reply Last reply
      0
      • tomasz3dkT Offline
        tomasz3dkT Offline
        tomasz3dk
        wrote on last edited by
        #3

        You didn't instantiate serialport, add:
        @serial = new QSerialPort(this);@

        into MainWindow constructor.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          matijalazic
          wrote on last edited by
          #4

          THANK YOU!! That was the issue. I expected it would be just a little mistake which are easiest to miss... Really thank you it works flawlessly now. Im gonna hide in my shame corner for missing such obvious thing :D

          1 Reply Last reply
          0

          • Login

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