PDA

View Full Version : QSerial problem:receives random data



omegaKnot
25th May 2011, 11: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);
}

Lesiok
25th May 2011, 13:42
First of all in MainWindow::serialupdate You are operating on no opened port.

kuzulis
25th May 2011, 14:01
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()?

omegaKnot
26th May 2011, 15:39
Hmmm.. I have opened the port. It happens only after pushButton_clicked(). Isn't it ok?

Lesiok
27th May 2011, 09:48
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.

ChrisW67
27th May 2011, 10:32
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.