Cazzaniga
4th July 2018, 15:01
Hi,
In a Qt 5 application I need to access the QSerialPort from a class/form different from the one in which the serialport is declared. In other words, In my MainWindow I have:
mainwindow.h:
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QSerialPort *serialport; // Is this the right place for the declaration?
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
serialport = new QSerialPort(this);
connect(serialport, SIGNAL(readyRead()), this, SLOT(main_readData()));
...
Now, I need to write data through the serialport from another class:
serial.cpp:
QByteArray ba;
ba.resize(50);
...
MainWindow m;
const qint64 bytesWritten = m.serialport->write(ba);
The problem is: no errors during the build, but when the "write" function is called, the following error appears:
"Permission error while locking the device", and the output console says: "QIODevice::write (QSerialPort): device not open".
It seems like the serialport is close when it is called from a class or form that is not the MainWindow.
How can I have the serialport accessible from everywhere?
How can I fix it? Anyone could give any advice?
Thank you.
In a Qt 5 application I need to access the QSerialPort from a class/form different from the one in which the serialport is declared. In other words, In my MainWindow I have:
mainwindow.h:
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QSerialPort *serialport; // Is this the right place for the declaration?
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
serialport = new QSerialPort(this);
connect(serialport, SIGNAL(readyRead()), this, SLOT(main_readData()));
...
Now, I need to write data through the serialport from another class:
serial.cpp:
QByteArray ba;
ba.resize(50);
...
MainWindow m;
const qint64 bytesWritten = m.serialport->write(ba);
The problem is: no errors during the build, but when the "write" function is called, the following error appears:
"Permission error while locking the device", and the output console says: "QIODevice::write (QSerialPort): device not open".
It seems like the serialport is close when it is called from a class or form that is not the MainWindow.
How can I have the serialport accessible from everywhere?
How can I fix it? Anyone could give any advice?
Thank you.