0
votes

I successfully read from a serial port with QSerialPort when I setup the instance's parameters in my main window constructor. I want my GUI to enable changing port however, but I can't seem to start reading when changing my com port after starting my application (I do so with a spinBox that sets up the COM port name of my QSerialPort instance). Here is my code (mainwindow.cpp), my problem is that if I do not directly use serial->setPortName(); in my constructor, and I put it in my slot linked to my spinBox signal, I can't read data received from my com port with qDebug anymore.

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

QSerialPort *serial;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);  // Here by default. Takes a pointer to mainwindow as argument
    serial = new QSerialPort(this);

    qDebug() << "nb ports: " << QSerialPortInfo::availablePorts().length();
    foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
    {
    qDebug() << "name" << serialPortInfo.portName();
    }
    qDebug() << "is " << serial->open(QSerialPort::ReadOnly);
    qDebug() << "err " << serial->error();


    // Create the signal and slot for
    connect(ui->com_spinBox, SIGNAL(valueChanged(const QString&)),
                    this, SLOT(setComPort(const QString&)));

    // Create the signal and slot for receiving data from device
    connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
}

MainWindow::~MainWindow()
{
    delete ui;
    serial->close(); // instance is closed when mainwindow destroyed
}

// My 2 custom slots below!!!

void MainWindow::serialReceived()
{
    QByteArray ba;
    ba = serial->readAll();
    ui->label->setText(serial->readAll());
   qDebug()<<ba;
}

void MainWindow::setComPort(const QString& com)
{
    serial->close();
    serial = new QSerialPort(this); // this (mainwindow) is parent

    qDebug() << serial->portName();
    QString comPort = "COM" + com;
    qDebug() << comPort;
    serial->setPortName(comPort);
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->open(QSerialPort::ReadOnly);
}

Here I tried moving all my serial instance setup from constructor to my slot, and close and re-open the instance to see if it helps, but I still cannot read anything with serialReceived() slot when tuning to the right COM port.. It does work if I put everything in the constructor and setPortName() with the right port number at the start of the program.

Thanks!

1
remove this line: serial = new QSerialPort(this); // this (mainwindow) is parent - eyllanesc
Be note the method serialReceived contains an error, you are trying to read the same data twice: ba = serial->readAll(); ui->label->setText(serial->readAll()); - Vladimir Bershov

1 Answers

0
votes

Try to delete and create again your QSerialPort before setPortName()

serial->deletLater();
serial = new QSerialPort(this);
serial->setPortName("COM1");