0
votes

Self taught rookie coder here, so pardon the mistakes. I am trying to make a program send/read serial data and having issues with the read part of it. I am able to select the comm port from a pull down, and transmit what I need. When i started to code the receive side using numerous examples online, it fails to compile and I cant seem to figure out why. If I exactly copy the code from the QT examples, it may work, but it wont do what I want it to (i.e. use the combo box pull down tab for selection)

#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....

The line that says "connect(serial, &QSerialPort::readyRead, this,&MainWindow::readSerial);" Is the culprit giving me the error: mainwindow.cpp:52: error: no matching function for call to 'MainWindow::connect(QSerialPort&, void (QIODevice::)(), MainWindow const, void (MainWindow::*)())' connect(serial, &QSerialPort::readyRead, this, &MainWindow::readSerial);

I tried to read the QObject and QSerialPort library help info, but the SIGNAL and SLOT stuff is too confusing for this amatuer. I also took parts of examples online and pasted to try and fix it,...no dice.

1

1 Answers

0
votes

You must pass the sender's pointer.

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)

Change:

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

to

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