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