Send a QSerialPort variable to another file

2

I am currently programming an app in C ++ with IDE Qt but I need to send a QSerialPort variable to another .cpp file in order to have the connection in any screen of the application but it does not accept the variable and I would like to know if only the variables of type Int, float, String, etc ... can be pulled or made with any type of variable

I leave the code I use: Principal.h

#ifndef PRINCIPAL_H
#define PRINCIPAL_H

#include <QWidget>
#include <QSerialPort>

namespace Ui {
class Principal;
}

class Principal : public QWidget
{
    Q_OBJECT

public:
    explicit Principal(QWidget *parent = 0);
    ~Principal();

    QSerialPort Puerto;
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_PruebaCon_clicked();
private:
    Ui::Principal *ui;
};

#endif // PRINCIPAL_H

Principal.cpp

#include "principal.h"
#include "ui_principal.h"
#include <puertoserial.h>
#include <conectados.h>
#include <serialport.h>
#include <QDebug>

Principal::Principal(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Principal)
{
    ui->setupUi(this);
    Puerto.setPortName("/dev/ttyUSB0");
    if(Puerto.open(QIODevice::ReadWrite)){
        Puerto.setBaudRate(QSerialPort::Baud115200);
        Puerto.setParity(QSerialPort::NoParity);
        Puerto.setStopBits(QSerialPort::OneStop);
        Puerto.setFlowControl(QSerialPort::NoFlowControl);
        Puerto.setDataBits(QSerialPort::Data8);
        qDebug() << "Conectado a puerto " << Puerto.portName() << " Conexión Original";
    }else{
        qDebug() << Puerto.errorString();
    }
}

Principal::~Principal()
{
    delete ui;
}

void Principal::on_pushButton_clicked()
{
    PuertoSerial *U = new PuertoSerial();
    U->show();
}

void Principal::on_pushButton_2_clicked()
{
    ConectaDos *D = new ConectaDos();
    D->show();
}

void Principal::on_PruebaCon_clicked()
{
    if(Puerto.isOpen()){
        ui->Estado->setText("Conectado a puerto: " + Puerto.portName());
    }else{
        ui->Estado->setText("Error! " + Puerto.errorString());
    }
}

connected.h

#ifndef CONECTADOS_H
#define CONECTADOS_H

#include <QWidget>
#include <principal.h>
#include <serialport.h>
#include <QSerialPort>

extern QSerialPort Puerto;
namespace Ui {
class ConectaDos;
}

class ConectaDos : public QWidget
{
    Q_OBJECT

public:
    explicit ConectaDos(QWidget *parent = 0);
    ~ConectaDos();

    SerialPort *ConnectDos;
private slots:
    void on_EnviarDatos_clicked();

    void on_CambiaVentana_clicked();

    void on_ProbConDos_clicked();
private:
    Ui::ConectaDos *ui;
};

#endif // CONECTADOS_H

conecta.cpp

#include "conectados.h"
#include "ui_conectados.h"
#include <QDebug>
#include <puertoserial.h>
#include <principal.h>

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

ConectaDos::~ConectaDos()
{
    delete ui;
}

void ConectaDos::on_EnviarDatos_clicked()
{

}

void ConectaDos::on_CambiaVentana_clicked()
{
    PuertoSerial *Uno = new PuertoSerial();
    Uno->show();
    this->close();
}

void ConectaDos::on_ProbConDos_clicked()
{
    QSerialPort Puerto;
    if(Puerto.isOpen()){
        ui->EstadoCD->setText("Puerto abierto " + Puerto.portName() + "Segunda Conexión");
    }else{
        ui->EstadoCD->setText("Error! " + Puerto.errorString());
    }
}
    
asked by Aarón Gutiérrez 21.06.2017 в 00:52
source

1 answer

1

If you can.

  

I need to send a QSerialPort variable to another .cpp file

This does not make any sense, the variables are not sent between files. Surely you are talking about Translation Units .

  

I would like to know if only variables of type Int, float, String, etc ... can be pulled or can be done with any type of variable.

Assuming you refer to if any instance of any type of data can be used as external data, the answer is: Yes.

Link.

The external variables are linked in the linking phase, that is: when the binary objects (usually extension .obj) of the Translation Units have already been generated. For an external variable to be linked, you have to declare it as extern in all the sites you want to use it and define it in just one site by omitting the% declarer extern , so:

Archivo1.cpp
extern MiClase mi_clase;
Archivo2.cpp
extern MiClase mi_clase;
Archivo3.cpp
extern MiClase mi_clase;

With these statements we are telling the compiler " eh, in another Translation Unit an object of type MiClase called mi_clase " is defined, that is: we have declared the existence of mi_clase . In another file:

Archivo0.cpp
MiClase mi_clase;

Here we are telling the compiler " hey, create an object of type MiClase called mi_clase in this Translation Unit ". Notice that mi_clase of Archivo0.cpp has no idea of being referenced from three other files. If you forget the definition of mi_clase the generation of the executable file would fail in time of linked with an error of " Unresolved external symbol ".

Your problem.

Your problem is not clear to me because you forgot to add to your question the error that your code generates, but I notice that you have a variable declared as external:

conectados.h
extern QSerialPort Puerto;

You are telling the compiler " eh, in another Translation Unit an object of type QSerialPort called Puerto " is defined, but as I see in the code that you have published, you are lying to the compiler:

Principal.h
class Principal : public QWidget
{
    // ...

    QSerialPort Puerto;

    // ...
};

Here you tell the compiler " hey, I'm going to describe to you how is the type object Principal , among other things this object has a member of type QSerialPort called Puerto " . So the object QSerialPort Principal::Puerto can not be the definition of extern QSerialPort Puerto .

conectados.cpp
void ConectaDos::on_ProbConDos_clicked()
{
    QSerialPort Puerto;

    // ...
}

Here you tell the compiler " hey do you remember that I told you that the type ConectaDos had a function called on_ProbConDos_clicked() ? I'll tell you what this function does, the first thing it does is create a object of type QSerialPort called Puerto ". So the QSerialPort Puerto object only visible within the ConectaDos::on_ProbConDos_clicked() function can not be the extern QSerialPort Puerto definition.

And therefore, the code surely fails you to link ... unless you have more code that you have not shown us.

    
answered by 21.06.2017 / 09:34
source