简体   繁体   English

使用串口发送数据时 QT C++ 应用程序崩溃

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

I am simply trying to send data via serial port, but I am getting segment fault error.我只是想通过串口发送数据,但我收到段错误错误。

When I click void productDetail::on_detailSaveBtn_clicked()) I got thıs error当我点击void productDetail::on_detailSaveBtn_clicked())我得到了这个错误

The inferior stopped because it received a signal from the operating system.下级停止了,因为它收到了来自操作系统的信号。

Signal name : SIGSEGV
Signal meaning : Segmentation fault

Debug shows arron on this line调试在这一行显示 arron

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

Can someone help me please how can I solve it?有人可以帮助我,我该如何解决?

Here is my code.这是我的代码。

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 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());


 }

If you run your code through cgdb, lldb, or any debugger in any IDEs, it will tell you where it is crashing.如果您通过 cgdb、lldb 或任何 IDE 中的任何调试器运行代码,它会告诉您崩溃的位置。 Based on your further clarification, it seems that you are trying to call a method on an instance of QSerialPort which has not been constructed yet.根据您的进一步澄清,您似乎正在尝试在尚未构建的 QSerialPort 实例上调用方法。

You need to create the serial port instance as per the examples I authored in QtSerialPort.您需要按照我在 QtSerialPort 中编写的示例创建串行端口实例。

Also, if you make it a member of your class, you do not really need to construct and allocate this instance on the heap.此外,如果你让它成为你的类的成员,你真的不需要在堆上构造和分配这个实例。 You could just as well allocate this on the stack avoiding further complexities.您也可以在堆栈上分配它以避免进一步的复杂性。

In other words, just remove the star from here and then the -> pointer member references.换句话说,只需从此处删除星号,然后删除->指针成员引用。

QSerialPort serial1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM