2017-03-22 67 views
0

自學新手編碼器在這裏,所以請原諒錯誤。我正在嘗試使程序發送/讀取串行數據,並在讀取部分存在問題。我可以從下拉列表中選擇通信端口,並傳輸我需要的內容。當我開始在網上使用大量示例編碼接收端時,它無法編譯,我似乎無法弄清楚爲什麼。如果我完全代碼從QT的例子,它可能作品的拷貝,但它不會做什麼,我希望它(即使用組合框下拉選項卡中選擇)編譯代碼時「沒有匹配調用函數」。 [QTSerialPort開放閱讀]

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include <QDebug> 
#include <QApplication> 
#include <QWidget> 
#include <QSerialPort> 
#include <QSerialPortInfo> 


QString commPort; 
QSerialPort serial; 
QByteArray charBuffer; 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 

    foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) //populates the combo box with all availble comm ports. 
     { 
     ui->comboBox->addItem(serialPortInfo.portName()); 
     commPort=serialPortInfo.portName(); 
     } 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
    serial.close(); 
} 

void MainWindow::on_comboBox_activated(const QString &commPort) //selects the chosen comm port. 
{ 
    ui->report->setText(commPort); 
} 

void MainWindow::openSerialPort() 
{ 

    serial.setPortName(commPort); 

    if(serial.open(QIODevice::ReadWrite)) 
    { 
     qDebug("Serial Opened"); 
     ui->report->setStyleSheet("QLabel{background-color:'green';}"); 
     serial.setBaudRate(QSerialPort::Baud9600); 
     serial.setDataBits(QSerialPort::Data8); 
     serial.setParity(QSerialPort::NoParity); 
     serial.setStopBits(QSerialPort::OneStop); 
     serial.setFlowControl(QSerialPort::NoFlowControl); 
     connect(serial, &QSerialPort::readyRead, this,&MainWindow::readSerial); 
//^^That is the part that fails to compile 

    } 
    else 
    { 
     ui->report->setStyleSheet("QLabel{background-color:'red';}"); 
     qDebug ("Serial NOT Opened"); 
     qDebug() << serial.error(); 
     qDebug() << serial.errorString(); 
    } 

} 

void MainWindow::on_connectButton_pressed() 
{ 
    openSerialPort(); 
} 

void MainWindow::readSerial() 
{ 
qDebug()<<("serial works"); 
} 

*more code follows, but not needed.... 

的一行「連接(串行,& QSerialPort :: readyRead,this,& MainWindow :: readSerial);「 是罪魁禍首給我的錯誤: mainwindow.cpp:52:錯誤:調用「主窗口沒有匹配功能::連接(QSerialPort &,無效(的QIODevice :: )(),主窗口常量,無效( MainWindow :: *)())' connect(serial,& QSerialPort :: readyRead,this,& MainWindow :: readSerial);

我試圖讀取QObject和QSerialPort庫的幫助信息,但SIGNAL和SLOT的東西太混亂了,我也在網上拿了部分例子,貼上去修復它,......沒有骰子。

回答

0

您必須通過發件人的指針。

connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)

connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type)

connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)

connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)

connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)

變化:

connect(serial, &QSerialPort::readyRead, this,&MainWindow::readSerial); 

connect(&serial, &QSerialPort::readyRead, this,&MainWindow::readSerial); 
+0

太謝謝你了。那樣做了! – Xarddrax

+0

如果我的答案有幫助,請將其標記爲正確。 – eyllanesc