omegaKnot
25th May 2011, 10:22
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...
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString com;
com = ui->comboBox->currentText();
port = new QextSerialPort(com);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
ui->dataWindow1->setDisabled(1);
ui->dataWindow2->setDisabled(1);
if(port->isOpen())
{
port->flush();
port->close();
}
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(serialupdate()));
timer->start(100);
}
MainWindow::~MainWindow()
{
port->close();
delete ui;
delete port;
}
void MainWindow::serialupdate()
{
port->flush();
char buff[1024];
if(port->bytesAvailable())
{
int i = port->read(buff, 1);
buff[i] = '\0';
if(i != -1)
{
QString str(buff);
QByteArray data = str.toAscii().toHex();
ui->dataWindow1->append("0x"+data);
ui->dataWindow2->append(str);
}
else
qDebug("cannot open");
}
}
void MainWindow::on_pushButton_clicked()
{
ui->dataWindow1->setDisabled(0);
ui->dataWindow2->setDisabled(0);
ui->comboBox->setDisabled(1);
ui->comboBox_2->setDisabled(1);
port->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
}
void MainWindow::on_pushButton_2_clicked()
{
ui->dataWindow1->setDisabled(1);
ui->dataWindow2->setDisabled(1);
ui->comboBox->setDisabled(0);
ui->comboBox_2->setDisabled(0);
port->close();
}
void MainWindow::on_pushButton_3_clicked()
{
ui->dataWindow1->clear();
ui->dataWindow2->clear();
}
void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{
if(index == 0)
port->setBaudRate(BAUD4800);
if(index == 1)
port->setBaudRate(BAUD9600);
if(index == 2)
port->setBaudRate(BAUD14400);
if(index == 3)
port->setBaudRate(BAUD19200);
}
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...
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString com;
com = ui->comboBox->currentText();
port = new QextSerialPort(com);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
ui->dataWindow1->setDisabled(1);
ui->dataWindow2->setDisabled(1);
if(port->isOpen())
{
port->flush();
port->close();
}
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(serialupdate()));
timer->start(100);
}
MainWindow::~MainWindow()
{
port->close();
delete ui;
delete port;
}
void MainWindow::serialupdate()
{
port->flush();
char buff[1024];
if(port->bytesAvailable())
{
int i = port->read(buff, 1);
buff[i] = '\0';
if(i != -1)
{
QString str(buff);
QByteArray data = str.toAscii().toHex();
ui->dataWindow1->append("0x"+data);
ui->dataWindow2->append(str);
}
else
qDebug("cannot open");
}
}
void MainWindow::on_pushButton_clicked()
{
ui->dataWindow1->setDisabled(0);
ui->dataWindow2->setDisabled(0);
ui->comboBox->setDisabled(1);
ui->comboBox_2->setDisabled(1);
port->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
}
void MainWindow::on_pushButton_2_clicked()
{
ui->dataWindow1->setDisabled(1);
ui->dataWindow2->setDisabled(1);
ui->comboBox->setDisabled(0);
ui->comboBox_2->setDisabled(0);
port->close();
}
void MainWindow::on_pushButton_3_clicked()
{
ui->dataWindow1->clear();
ui->dataWindow2->clear();
}
void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{
if(index == 0)
port->setBaudRate(BAUD4800);
if(index == 1)
port->setBaudRate(BAUD9600);
if(index == 2)
port->setBaudRate(BAUD14400);
if(index == 3)
port->setBaudRate(BAUD19200);
}