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?
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QSerialPort *serialport; // Is this the right place for the declaration?
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
serialport = new QSerialPort(this);
connect(serialport, SIGNAL(readyRead()), this, SLOT(main_readData()));
...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
serialport = new QSerialPort(this);
connect(serialport, SIGNAL(readyRead()), this, SLOT(main_readData()));
...
To copy to clipboard, switch view to plain text mode
Now, I need to write data through the serialport from another class:
serial.cpp:
ba.resize(50);
...
MainWindow m;
const qint64 bytesWritten = m.serialport->write(ba);
QByteArray ba;
ba.resize(50);
...
MainWindow m;
const qint64 bytesWritten = m.serialport->write(ba);
To copy to clipboard, switch view to plain text mode
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.
Bookmarks