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



  • 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()));
            }
        }*/
    

    }
    @



  • What object is your "serial" pointing to?



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

    into MainWindow constructor.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.