Results 1 to 4 of 4

Thread: QSerialPort and QtQuick animation freezing on list and open serial ports

  1. #1
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    9

    Default QSerialPort and QtQuick animation freezing on list and open serial ports

    Anyone could help me, please.

    I wrote a C++ method to find all serial ports, open, write and close and use to Q_INVOKABLE to call this method from a QML.

    The problem is a freezing on push LoadingPage.qml if there are many serial ports connected, the animation start and then immediately freezes, when the function find finish, the animation start again. [SerialPort.qml]

    How is it the better way to solve that?
    Thanks

    main.cpp
    Qt Code:
    1. qmlRegisterType<Module::Physical>("MyType", 1, 0, "SerialPort");
    To copy to clipboard, switch view to plain text mode 
    Main.qml
    Qt Code:
    1. ApplicationWindow {
    2. SerialPort {
    3. id: module
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 
    SerialPort.qml
    Qt Code:
    1. Button {
    2. text: qsTr("start")
    3. onClicked: {
    4. stackView.push(Qt.resolvedUrl("LoadingPage.qml"))
    5. module.find()
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    serialport.h
    Qt Code:
    1. Q_INVOKABLE QVector<QString> find();
    To copy to clipboard, switch view to plain text mode 
    serialport.cpp
    Qt Code:
    1. QVector<QString> Physical::find()
    2. {
    3. m_ports.clear();
    4.  
    5. foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    6. bool hasError = false;
    7.  
    8. QSerialPort port;
    9. port.setPort(info);
    10.  
    11. if (port.open(QIODevice::ReadWrite)) {
    12. if (!hasError && !port.setBaudRate(serial::baudRate)) {
    13. emit error(tr("Can't set baud to %1, error %2")
    14. .arg(port.portName())
    15. .arg(port.error()));
    16. hasError |= true;
    17. }
    18. if (!hasError && !port.setDataBits(serial::dataBits)) {
    19. emit error(tr("Can't set data bits to %1, error %2")
    20. .arg(port.portName())
    21. .arg(port.error()));
    22. hasError |= true;
    23. }
    24.  
    25. if (!hasError && !port.setParity(serial::parity)) {
    26. emit error(tr("Can't set parity to %1, error %2")
    27. .arg(port.portName())
    28. .arg(port.error()));
    29. hasError |= true;
    30. }
    31. if (!hasError && !port.setStopBits(serial::stopBits)) {
    32. emit error(tr("Can't set stop bits to %1, error %2")
    33. .arg(port.portName())
    34. .arg(port.error()));
    35. hasError |= true;
    36. }
    37. if (!hasError && !port.setFlowControl(serial::flowCtrl)) {
    38. emit error(tr("Can't set flow control to %1, error %2")
    39. .arg(port.portName())
    40. .arg(port.error()));
    41. hasError |= true;
    42. }
    43. if (!hasError) {
    44. m_ports.append(port.portName());
    45. }
    46.  
    47. QByteArray data;
    48. data.resize(1);
    49. data[0] = ID_READ;
    50.  
    51. port.write(data);
    52. port.close();
    53. }
    54. }
    55.  
    56. return m_ports;
    57. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    9

    Default Re: QSerialPort and QtQuick animation freezing on list and open serial ports

    From StackOverflow, answered by Kuba Ober, source: http://stackoverflow.com/questions/2...n-serial-ports

    Your code runs in the GUI thread, and since it blocks the GUI thread, the user interaction is stopped as well.

    You need to perform the scan in a separate thread. The Qt Concurrent framework is perfect for this, since you're performing a self-contained action that can be done in any thread. Your find() method must be turned into a stand-alone function or a static method (since that's what it really is).

    You'd then run it as follows:
    Qt Code:
    1. class Physical {
    2. QFuture<QStringList> m_future;
    3. QFutureWatcher<QStringList> m_futureWatcher;
    4. // A string list is a simpler type to type :)
    5. static QStringList doFindPorts() {
    6. ...
    7. }
    8. Q_SLOT void findPortsFinished() {
    9. QStringList ports(m_future);
    10. // use the list of ports
    11. }
    12.  
    13. public:
    14. Physical() {
    15. connect(&m_futureWatcher, SIGNAL(finished()), SLOT(findPortsFinished()));
    16. m_futureWatcher.set(m_future);
    17. ...
    18. }
    19. Q_SLOT void findPorts() {
    20. if (m_future.isRunning()) return;
    21. m_future = QtConcurrent::run(doFindPorts);
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSerialPort and QtQuick animation freezing on list and open serial ports

    That or processing the serial ports one at a time.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSerialPort and QtQuick animation freezing on list and open serial ports

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    ricardodovalle (7th April 2014)

Similar Threads

  1. QSerialPort :: read - Serial Comms noob.
    By llaregyb in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2014, 13:18
  2. List serial ports in Qt application on windows and Mac
    By arunkumaraymuo1 in forum Qt Programming
    Replies: 5
    Last Post: 27th August 2012, 11:30
  3. qextserialport cant open ports in windows 7
    By manaila in forum Newbie
    Replies: 4
    Last Post: 16th April 2012, 16:40
  4. Replies: 5
    Last Post: 11th August 2011, 15:16
  5. Replies: 1
    Last Post: 16th June 2009, 10:09

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.