Qt5使用connect连接信号和槽,编译构建无错,实际运行不会调用槽函数!!!!
如题:使用串口发送文件,使用GUI配置文件写入下位机信息,组帧下发给下位机解析,解析成功后,下位机会在程序进入接受数据循环前发送响应给上位机,上位机接受响应下发文件,在mainwindow.h中自定义信号getinfo()和槽函数downloadfile()
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSerialPort> #include <QSerialPortInfo> #include <QList> #include <QFileDialog> #include <QPushButton> #include <QLineEdit> #include <QDebug> #include <QProgressBar> #include <QDir> #include <QFileInfo> #include <QFile> #include <QComboBox> #include <QByteArray> #include <QMessageBox> #include <QDebug> #include <QTime> #include <QTimer> #include <QTextCodec> #include <QThread> #include <QTextDocument> quint32 swapInt32(quint32 value); quint32 QStringTo4ByteHex(QString source); unsigned char * SerportMsg(quint32 Addr,quint32 FileSize); namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); QString filename; QTimer timer; void QDelay(quint32 msec); private slots: void on_btn_openConsole_clicked(); void on_btn_clearRecv_clicked(); void on_btn_clearSend_clicked(); void readData(); void on_fileBtn_clicked(); void on_sendfileBtn_clicked(); private: Ui::MainWindow *ui; QSerialPort *serial; QString buf; signals: void getinfo();//自定义信号 public slots: void downloadFile(); }; #endif // MAINWINDOW_H
MainWindow.cpp中读取下位机发送的数据,分析响应,若是握手信号,接受框显示信息,若为文件数据帧响应则将发送getinfo()信号
void MainWindow::readData() { //QString buf; buf = serial->read(1); ui->textEdit_recv->append(buf); //响应数据对比处理 if(buf == "s") //显示串口打开成功 { ui->textEdit_recv->append("connect uart success!"); buf.clear(); } //文件发送 if(buf == "S") { ui->textEdit_recv->append("ready to send signal!"); emit getinfo(); } }
void MainWindow::on_sendfileBtn_clicked() { QString filename = ui->File_LineEdit->text(); QFile file(filename); quint32 FileSize = file.size(); qDebug("FileSize is 0x%x",FileSize); //发数据命令:Addr+file_size QString addr = ui->AddrLineEdit->text(); if(addr.length() > 8) { QMessageBox::critical(this,"critical","address range override,please rewrite!!!", QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes); return; } QByteArray tmp = addr.toLatin1(); const char * lpo = tmp.data(); quint32 ADDR; sscanf(lpo,"%x",&ADDR); qDebug("addr is %s",addr); qDebug("ADDR is 0x%x",ADDR); if(filename.isEmpty()|addr.isEmpty()) { QMessageBox::critical(this,"critical", "please select bin file and configure the address", QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes); } //定义下发文件数据帧 unsigned char *frame = SerportMsg(ADDR,FileSize); QByteArray bootInfo((char*)frame,12); serial->write(bootInfo); delete [] frame; connect(ui->sendfileBtn,SIGNAL(getinfo),this,SLOT(MainWindow::downloadFile())); }
构建无错误,运行后出现下发数据帧,但不会执行槽函数downloadFile()!!! --- 2019/10/24