Results 1 to 6 of 6

Thread: QSerial problem:receives random data

  1. #1
    Join Date
    May 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QSerial problem:receives random data

    My code can receive data serially from PIC16F877A.
    Sometimes, random and unnecessary data are received. If i use tinyBootloader's serial receiver, it picks up required data. Immediately after closing tinyBootloader, if i run my code, then my program also receives that data. Is there something wrong with my code's port opening and closing. Please help.
    Here's that code...
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "qextserialport.h"
    4. #include <QTimer>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. QString com;
    12. com = ui->comboBox->currentText();
    13. port = new QextSerialPort(com);
    14. port->setBaudRate(BAUD9600);
    15. port->setFlowControl(FLOW_OFF);
    16. port->setParity(PAR_NONE);
    17. port->setDataBits(DATA_8);
    18. port->setStopBits(STOP_1);
    19. ui->dataWindow1->setDisabled(1);
    20. ui->dataWindow2->setDisabled(1);
    21. if(port->isOpen())
    22. {
    23. port->flush();
    24. port->close();
    25. }
    26. QTimer *timer = new QTimer(this);
    27. connect(timer, SIGNAL(timeout()), this, SLOT(serialupdate()));
    28. timer->start(100);
    29. }
    30.  
    31. MainWindow::~MainWindow()
    32. {
    33. port->close();
    34. delete ui;
    35. delete port;
    36. }
    37.  
    38.  
    39. void MainWindow::serialupdate()
    40. {
    41. port->flush();
    42. char buff[1024];
    43. if(port->bytesAvailable())
    44. {
    45. int i = port->read(buff, 1);
    46. buff[i] = '\0';
    47. if(i != -1)
    48. {
    49. QString str(buff);
    50. QByteArray data = str.toAscii().toHex();
    51. ui->dataWindow1->append("0x"+data);
    52. ui->dataWindow2->append(str);
    53. }
    54. else
    55. qDebug("cannot open");
    56. }
    57. }
    58.  
    59. void MainWindow::on_pushButton_clicked()
    60. {
    61. ui->dataWindow1->setDisabled(0);
    62. ui->dataWindow2->setDisabled(0);
    63. ui->comboBox->setDisabled(1);
    64. ui->comboBox_2->setDisabled(1);
    65. port->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
    66. }
    67.  
    68. void MainWindow::on_pushButton_2_clicked()
    69. {
    70. ui->dataWindow1->setDisabled(1);
    71. ui->dataWindow2->setDisabled(1);
    72. ui->comboBox->setDisabled(0);
    73. ui->comboBox_2->setDisabled(0);
    74. port->close();
    75. }
    76.  
    77.  
    78.  
    79. void MainWindow::on_pushButton_3_clicked()
    80. {
    81. ui->dataWindow1->clear();
    82. ui->dataWindow2->clear();
    83. }
    84.  
    85. void MainWindow::on_comboBox_2_currentIndexChanged(int index)
    86. {
    87. if(index == 0)
    88. port->setBaudRate(BAUD4800);
    89. if(index == 1)
    90. port->setBaudRate(BAUD9600);
    91. if(index == 2)
    92. port->setBaudRate(BAUD14400);
    93. if(index == 3)
    94. port->setBaudRate(BAUD19200);
    95. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QSerial problem:receives random data

    First of all in MainWindow::serialupdate You are operating on no opened port.

  3. #3
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerial problem:receives random data

    Try another library, for example QSerialDevice ( https://gitorious.org/qserialdevice ).

    There is a ready-made GUI test sample to work with the serial device.
    An example is in the directory: /test/guiapp.
    Build this example and test communication with your PIC16F877A.

    PS: Why did you check the data every 100 ms? Is not it easier to hang a slot on the signal readyRead()?

  4. #4
    Join Date
    May 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSerial problem:receives random data

    Hmmm.. I have opened the port. It happens only after pushButton_clicked(). Isn't it ok?

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSerial problem:receives random data

    Quote Originally Posted by omegaKnot View Post
    Hmmm.. I have opened the port. It happens only after pushButton_clicked(). Isn't it ok?
    But MainWindow::serialupdate is called every 100 ms. Before opening port too.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSerial problem:receives random data

    Line 13 attempts to construct a QextSerialPort with a port of "" because at line 12 the combo box will possibly have no meaningful value. You haven't given the user a chance to enter anything in that widget yet. I expect that the port is not open after this but, even if it were, you will close it at line 24.

    Once you get past opening the port you should reconsider using a polling mechanism and use the readyRead() signal to trigger handling when data is available.
    Last edited by ChrisW67; 27th May 2011 at 09:38.

Similar Threads

  1. Replies: 11
    Last Post: 1st December 2010, 11:28
  2. Replies: 4
    Last Post: 30th August 2010, 14:37
  3. Replies: 1
    Last Post: 7th April 2010, 16:26
  4. Widget that receives the mouseReleaseEvent
    By qtUser500 in forum Newbie
    Replies: 10
    Last Post: 16th July 2009, 15:33
  5. random
    By raphaelf in forum General Programming
    Replies: 9
    Last Post: 6th June 2007, 12:33

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.