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