Results 1 to 7 of 7

Thread: QSerialPort closed when access from another class

  1. #1
    Join Date
    Feb 2016
    Location
    S. Stefano di Sessanio
    Posts
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default QSerialPort closed when access from another class

    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.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,460
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort closed when access from another class

    And where is the serialport->open() ?

  3. #3
    Join Date
    Feb 2016
    Location
    S. Stefano di Sessanio
    Posts
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QSerialPort closed when access from another class

    And where is the serialport->open() ?
    Sorry: I opened it in the mainwindow.pp just under the connect code:
    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. if (serialport->open(QIODevice::ReadWrite))
    8. {
    9. showStatusMessage(tr("STATO SISTEMA: OK"));
    10. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,460
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort closed when access from another class

    Can You show real code ? In the first post you showed some strange pieces of code that do not make sense.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,136
    Thanks
    235
    Thanked 660 Times in 650 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QSerialPort closed when access from another class

    If the original code fragment posted is part of the real code, then the line "MainWindow m;" is creating a new second instance of MainWindow on the stack and is trying to use the QSerialPort that was previously created and opened in the first MainWindow instance.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Feb 2016
    Location
    S. Stefano di Sessanio
    Posts
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QSerialPort closed when access from another class

    Quote Originally Posted by d_stranz View Post
    If the original code fragment posted is part of the real code, then the line "MainWindow m;" is creating a new second instance of MainWindow on the stack and is trying to use the QSerialPort that was previously created and opened in the first MainWindow instance.
    I see.. so which should be the best way to call a method/function defined in another class without calling a second instance of that class (like I did before)?
    Thank you!

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,460
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort closed when access from another class

    You can pass the pointer of a QSerialPort object or use the signals and slots mechanism.

Similar Threads

  1. Replies: 1
    Last Post: 16th May 2017, 12:48
  2. Replies: 4
    Last Post: 12th November 2015, 13:00
  3. Replies: 4
    Last Post: 2nd April 2013, 09:13
  4. Replies: 4
    Last Post: 29th May 2010, 12:56
  5. Replies: 5
    Last Post: 14th July 2006, 22:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.