Results 1 to 4 of 4

Thread: How to make QT automatically connect to correct COM Port

  1. #1

    Default How to make QT automatically connect to correct COM Port

    I am trying to make QT automatically connect to my ST32 MCU by serial. MCU is keep sending data for each second. So I thought I can read all the available COM Ports and when the line is not null, I will detect that COM Port is MCU. But All COM Ports are null. Here is related piece of code I am using in QT:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtSerialPort>
    4. #include <QDebug>
    5. QSerialPort *serial;
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QMainWindow(parent)
    8. , ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. serial = new QSerialPort(this);
    12. foreach(QSerialPortInfo port, QSerialPortInfo::availablePorts())
    13. {
    14. serial->setPortName(port.portName());
    15. serial->setBaudRate(QSerialPort::Baud115200);
    16. serial->setDataBits(QSerialPort::Data8);
    17. serial->setParity(QSerialPort::NoParity);
    18. serial->setStopBits(QSerialPort::OneStop);
    19. serial->setFlowControl(QSerialPort::NoFlowControl);
    20. serial->open(QIODevice::ReadOnly);
    21.  
    22. if(!(serial->readLine().isNull()))
    23. {
    24. qDebug()<<port.portName()<<"is opened";
    25. }
    26.  
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    I believe 1 second is not enough for QT to detect that. Maybe I should use timeout in QT. Any idea how can I solve this problem?

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

    Default Re: How to make QT automatically connect to correct COM Port

    First of all, one should check if open() was successful. If not, check the current error code. Secondly, you should do clearError() before open(). You operate all the time on the same object.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,630
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to make QT automatically connect to correct COM Port

    QSerialDevice is almost certainly going to require a running Qt event loop, which is not available inside your foreach() loop. Even if it was, the likelihood of data being in the buffer for readLine() to return inside this tight loop is not that high.

    You need to defer all this reading activity until the main event loop is running. You can then implement a timeout in parallel with attempting a readLine(). You should also consider the number of bytes returned by readLine(), which may be zero or an indicator of an error.

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

    Default Re: How to make QT automatically connect to correct COM Port

    Or use QSerialPort::waitForReadyRead. Something like this :
    Qt Code:
    1. foreach(QSerialPortInfo port, QSerialPortInfo::availablePorts())
    2. {
    3. serial->setPortName(port.portName());
    4. serial->setBaudRate(QSerialPort::Baud115200);
    5. serial->setDataBits(QSerialPort::Data8);
    6. serial->setParity(QSerialPort::NoParity);
    7. serial->setStopBits(QSerialPort::OneStop);
    8. serial->setFlowControl(QSerialPort::NoFlowControl);
    9. serial->open(QIODevice::ReadOnly);
    10.  
    11. if(serial->waitForReadyRead(1500))
    12. {
    13. qDebug()<<port.portName()<<"is opened";
    14. }
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to Make Qt Creator use the correct GDB on OSX
    By mark100net in forum Qt Tools
    Replies: 0
    Last Post: 4th December 2011, 08:21
  2. Replies: 1
    Last Post: 23rd April 2011, 09:42
  3. Replies: 0
    Last Post: 9th August 2010, 16:03
  4. Replies: 2
    Last Post: 22nd November 2009, 05:47
  5. How can i make widget automatically adjust screen size
    By kapoorsudhish in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2009, 15:21

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.