簡體   English   中英

使用串口發送數據時 QT C++ 應用程序崩潰

[英]QT C++ app crashing when send data with serial port

我只是想通過串口發送數據,但我收到段錯誤錯誤。

當我點擊void productDetail::on_detailSaveBtn_clicked())我得到了這個錯誤

下級停止了,因為它收到了來自操作系統的信號。

Signal name : SIGSEGV
Signal meaning : Segmentation fault

調試在這一行顯示 arron

 { return write(data.constData(), data.size()); }

有人可以幫助我,我該如何解決?

這是我的代碼。

productdetail.h


#ifndef PRODUCTDETAIL_H
#define PRODUCTDETAIL_H

#include <QDialog>
#include <QSerialPort>

namespace Ui {
class productDetail;
}

class productDetail : public QDialog
{
    Q_OBJECT

public:
    explicit productDetail(QWidget *parent = nullptr);
    ~productDetail();

private slots:
    void on_detailSaveBtn_clicked();

private:
    Ui::productDetail *ui;
    void connectSerial();
    QSerialPort *serial1;
};

#endif // PRODUCTDETAIL_H

productDetail.cpp

#include "productdetail.h"
#include "ui_productdetail.h"
#include <QDebug>
#include <QSerialPort>
#include <QMessageBox>
productDetail::productDetail(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::productDetail)
{
    ui->setupUi(this);
}

productDetail::~productDetail()
{
    delete ui;
}


void productDetail::connectSerial(){




     //Set serial port name
     serial1->setPortName("COM3");
     //Open serial port
     serial1->open(QIODevice::WriteOnly);
     //set baud rate
     serial1->setBaudRate(9600);
     //Set the number of data bits
     serial1->setDataBits(QSerialPort::Data8);
      //Set parity
      serial1->setParity(QSerialPort::NoParity);
     //Set stop bit
     serial1->setStopBits(QSerialPort::OneStop);
     //set flow control
     serial1->setFlowControl(QSerialPort::NoFlowControl);



}
void productDetail::on_detailSaveBtn_clicked()
{


serial1->write(ui->productDesp->text().toLatin1());


 }

如果您通過 cgdb、lldb 或任何 IDE 中的任何調試器運行代碼,它會告訴您崩潰的位置。 根據您的進一步澄清,您似乎正在嘗試在尚未構建的 QSerialPort 實例上調用方法。

您需要按照我在 QtSerialPort 中編寫的示例創建串行端口實例。

此外,如果你讓它成為你的類的成員,你真的不需要在堆上構造和分配這個實例。 您也可以在堆棧上分配它以避免進一步的復雜性。

換句話說,只需從此處刪除星號,然后刪除->指針成員引用。

QSerialPort serial1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM