[英]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.