Results 1 to 2 of 2

Thread: Serial port timeout - Qextserialport

  1. #1
    Join Date
    Mar 2013
    Location
    Poland
    Posts
    15
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Unhappy Serial port timeout - Qextserialport

    hello.
    I want to write a program that will open the port and read data (in continuous mode, loop).

    According to the algorithm:

    Start ->
    1.Try open port
    a) if opened then read data
    b) if not opened it wait 30 seconds and try to raise open port (jump to 1)

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. port = new QextSerialPort("/dev/ttyUSB0");
    11. port->setBaudRate(BAUD9600);
    12. port->setFlowControl(FLOW_OFF);
    13. port->setParity(PAR_NONE);
    14. port->setDataBits(DATA_8);
    15. port->setStopBits(STOP_1);
    16.  
    17. port->setTimeout(500);
    18. reading();
    19.  
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26.  
    27.  
    28.  
    29. void MainWindow::reading()
    30. {
    31.  
    32.  
    33. qDebug() << port->open(QIODevice::ReadWrite);
    34.  
    35. if (port->isOpen()==false)
    36. {
    37. port->flush();
    38.  
    39. port->startTimer(30000);
    40. qDebug() << "its it reading";
    41. //port->waitForReadyRead(1000);
    42. reading();
    43. }
    44. else
    45. {
    46. connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
    47. }
    48. }
    49.  
    50. void MainWindow::onDataAvailable()
    51. {
    52.  
    53. if (port->isOpen())
    54. {
    55. QByteArray mdata = port->readAll();
    56. qDebug() << "\n";
    57.  
    58. qDebug() << mdata.toHex(); // hex data
    59. qDebug() << mdata; // ascii data
    60.  
    61.  
    62.  
    63. ui->lineEdit->clear();
    64. ui->lineEdit->setText(mdata);
    65. }
    66. else
    67. {
    68.  
    69. reading();
    70. qDebug() << "error 2 port is not open";
    71. }
    72. // port->close();
    73. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Serial port timeout - Qextserialport

    First of all in method MainWindow::reading() if port->isOpen == false You have an infinite loop and stack overflow - You are calling reading in reading.
    Second this all happens in the MainWindow constructor. By me it should be something like this :
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. port = new QextSerialPort("/dev/ttyUSB0");
    11. port->setBaudRate(BAUD9600);
    12. port->setFlowControl(FLOW_OFF);
    13. port->setParity(PAR_NONE);
    14. port->setDataBits(DATA_8);
    15. port->setStopBits(STOP_1);
    16.  
    17. port->setTimeout(500);
    18. QTimer::singleShot(0,this,SLOT(reading()));
    19.  
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26.  
    27.  
    28.  
    29. void MainWindow::reading()
    30. {
    31.  
    32.  
    33. qDebug() << port->open(QIODevice::ReadWrite);
    34.  
    35. if (port->isOpen()==false)
    36. {
    37. port->flush();
    38.  
    39. qDebug() << "its it reading";
    40. QTimer::singleShot(30000,this,SLOT(reading()));
    41. }
    42. else
    43. {
    44. connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
    45. }
    46. }
    47.  
    48. void MainWindow::onDataAvailable()
    49. {
    50.  
    51. if (port->isOpen())
    52. {
    53. QByteArray mdata = port->readAll();
    54. qDebug() << "\n";
    55.  
    56. qDebug() << mdata.toHex(); // hex data
    57. qDebug() << mdata; // ascii data
    58.  
    59.  
    60.  
    61. ui->lineEdit->clear();
    62. ui->lineEdit->setText(mdata);
    63. }
    64. else
    65. {
    66.  
    67. reading();
    68. qDebug() << "error 2 port is not open";
    69. }
    70. // port->close();
    71. }
    72.  
    73. To copy to clipboard, switch view to plain text mode
    To copy to clipboard, switch view to plain text mode 
    Of course raeding() should be declared as SLOT.

  3. The following user says thank you to Lesiok for this useful post:

    h-n-s (9th April 2013)

Similar Threads

  1. Replies: 2
    Last Post: 3rd July 2012, 16:42
  2. qextserialport timeout
    By wally123 in forum Qt Programming
    Replies: 0
    Last Post: 8th March 2012, 13:43
  3. Replies: 5
    Last Post: 11th August 2011, 14:16
  4. Replies: 1
    Last Post: 1st July 2009, 00:36
  5. qextserialport waitForReadyRead and timeOut
    By adamatic in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2009, 09:52

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.