PDA

View Full Version : serial programming help



eva2002
18th January 2010, 03:38
Hi all,

anyone use before qextserialport? I need some help in getting it to work properly. I have a arm development board which does not have any keyboard attached so I need to hyper terminal my laptop to the board for keyboard entry.

here are my problems:
1) at start it proceed fine but after a while it started to print garbage and also lose some input. eg. abcd@@.- after that I typed g once and it will show abcd@@.-g@@.-

2) I seems to get seg fault after I close the form to load another form or end prog (if I start the receive function by button clicked event).

3) regardless of whether I click on button to start receiving or not, when I end the program, it will bring me to the login in screen of linux. eg ARM login: _

Can someone help me? below is my attached coded. Thanks


form1.h


#ifndef FORM1_H
#define FORM1_H

#include <QWidget>
//#include <qextserialport.h>
#include "serial.h"
#include "form2.h"
#include <QLineEdit>

namespace Ui {
class form1;
}

class form1 : public QWidget {
Q_OBJECT
public:
form1(QWidget *parent = 0);
~form1();

protected:
void changeEvent(QEvent *e);

private slots:
void on_btnLoadf2_clicked();
void on_btn_rec_clicked();
void on_btn_exit_clicked();
void closeEvent(QCloseEvent *e);

private:
Ui::form1 *ui;
Form2 *f2;
serial *port;
};

#endif // FORM1_H


form1.cpp


#include "form1.h"
#include "ui_form1.h"
#include <QString>
#include "form2.h"
#include <iostream>

form1::form1(QWidget *parent) :
QWidget(parent),
ui(new Ui::form1)
{
ui->setupUi(this);
port = new serial;
port->init();
}

form1::~form1()
{
delete ui;
}

void form1::closeEvent(QCloseEvent *e)
{
port->close();
}

void form1::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void form1::on_btnLoadf2_clicked()
{
QString str = "to be pass over to f2";
f2 = new Form2;
f2->show();
this->close();
this->destroy();
}

void form1::on_btn_exit_clicked()
{
this->close();
}

void form1::on_btn_rec_clicked()
{
port->receive((void *) ui->line_serial);
}


serial.h


#ifndef SERIAL_H
#define SERIAL_H

#include <qextserialport.h>

class serial
{
public:
serial();
~serial();
void init();
void receive(void *line);
void close();

private:
bool stop;
QextSerialPort *com;
};

#endif // SERIAL_H


serial.cpp


#include "serial.h"
#include <QApplication>
#include <QLineEdit>

serial::serial()
{
}

serial::~serial()
{
}

void serial::init()
{
//set up serial port
com = new QextSerialPort("/dev/ttyS0");
com->setBaudRate(BAUD115200);
com->setParity(PAR_NONE);
com->setDataBits(DATA_8);
com->setStopBits(STOP_1);
com->setFlowControl(FLOW_OFF);
com->open(QIODevice::ReadOnly);
stop = false;
}

void serial::close()
{
stop = true;
delete com;
com = NULL;
}

void serial::receive(void *obj)
{
char data[256];
int nbytes;

QLineEdit *line = (QLineEdit *) obj;
QString* str = new QString;

while (stop == false) {
QApplication::processEvents();
nbytes = com->bytesAvailable();

if (nbytes > 0) {
com->read(data,nbytes);
*str = data;
line->setText(line->text().append(*str));
}
}
}

wysota
22nd January 2010, 10:42
I don't think QExtSerialPort has anything to do with this, it uses a native implementation for handling the port. It would be more probably that you terminal sends some special characters which you don't know how to read or that you otherwise corrupt the port.

By the way, what's the reason for passing a void pointer around your code?

Lesiok
22nd January 2010, 11:45
2) I seems to get seg fault after I close the form to load another form or end prog (if I start the receive function by button clicked event).


Problem is in this two lines :

QApplication::processEvents();
nbytes = com->bytesAvailable();

I think that serial::close() is called in some place activated by QApplication::processEvents(). After this com == NULL. Yours code must looks like :

QApplication::processEvents();
if( com != NULL )
{
nbytes = com->bytesAvailable();
.....
}