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