PDA

View Full Version : serial port communication



robotics
20th May 2011, 05:08
hello everybody
I am using qt in window. I am trying to communicate with my serial port device. I have already downloaded the "QExtSerialPort" and Sucessfullly build it.
I want to receive the data sent my my hardware devices in my Gui application..for simple I want just to recieve data from serial port ...........please can anyone share example with me......ur help shall be highly appreciated

squidge
20th May 2011, 07:39
QExtSerialPort comes with lots of examples already...

robotics
20th May 2011, 08:15
I have managed to open the serial port . Since I am sending characters continiously from micrcontroller and I am recieving garbage data in my gui....
please help me::
my code is as follow::
I should recieve data when I press the button .........but I am recieving garbage value in my lineEdit..:(

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include "qdebug.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

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

void MainWindow::on_pushButton_clicked()
{
QextSerialPort * port = new QextSerialPort();
port->setPortName("COM4");
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_HARDWARE);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->open( QIODevice::ReadOnly );
char *buff;
if(port->isReadable())
{
qint64 i=port->read(buff,1);
buff[i]='\0';
if(i!=-1)
{
QString str(buff);
ui->lineEdit->setText(str);
port->close();
}
}

else
qDebug("port not open");
}

high_flyer
20th May 2011, 09:50
your 'buff' is not initialized!!
And since you always only read one char, why do you use a pointer and not just a char?

squidge
20th May 2011, 12:55
You should also be using signals and slots.

I assume your micro is sending just text? Because thats all a lineedit can display.

robotics
20th May 2011, 16:19
guys thanks for your reply
actually my microcontroller sends character continiously everytime(i.e. sends character 'a'continiously)
I used it just to check whether my gui is recieving or not ....
but still I am recieving garbage value in lineedit
I modified according to your suggestion
I used Qtimer that operates on overy 0.5 secs
My CODE

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include "qdebug.h"
#include "qtimer.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timeupda te()));
timer->start(500);
// QextSerialPort * serialport=new QextSerialPort;
// connect(serialport,SIGNAL(readyRead()),this,SLOT(t imeupdate()));
}

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


void MainWindow::timeupdate()
{
QextSerialPort * port = new QextSerialPort();
port->setPortName("COM4");
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_HARDWARE);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
//port->setTimeout(0);
port->open( QIODevice::ReadOnly );
char buff[4];
if(port->isReadable())
{
qint64 i=port->read(buff,1);
buff[i]='\0';
if(i!=-1)
{
QString str(buff);
ui->lineEdit->setText(str);
port->close();
}
}

else
qDebug("port not open");
}

and In mainwindow.h

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

private slots:

void timeupdate();
};

Lesiok
20th May 2011, 16:52
Why are You opening and closing port again and again ?????
Open port in MainWindow constructor and close it in destructor.

serjts
20th May 2011, 18:14
I remeber making a working program and I started with the examples.. check those first

squidge
20th May 2011, 20:29
Even worse, every 500ms you are creating a new QextSerialPort but never deleting it :o

Are you sure all the parameters are correct? You are using hardware flow control and 9600 baud?

Check the output from your micro with a DSO to confirm its sending what you think its sending and dont forget the common fault of forgetting the level translator between the micro and PC.

robotics
21st May 2011, 07:25
Why are You opening and closing port again and again ?????
Open port in MainWindow constructor and close it in destructor.
I tried as You suggested but my program does not run....instead it gets hang
please help me to modify the above code........It's really frustrating me...

Are you sure all the parameters are correct? You are using hardware flow control and 9600 baud?

Check the output from your micro with a DSO to confirm its sending what you think its sending and dont forget the common fault of forgetting the level translator between the micro and PC.
yes I have checked that my microcontroller is sending the data...
please help me...

squidge
21st May 2011, 10:27
So everything works correctly if you use a terminal program such as Hyperterm?

robotics
21st May 2011, 12:15
So everything works correctly if you use a terminal program such as Hyperterm?yes everything works fine in program such as hyperterminal.......they are recieving the continious data which i sent from the microcontroller.......
Please modify the above code for me ......I am really getting frustrated....

Why are You opening and closing port again and again ?????
Open port in MainWindow constructor and close it in destructor.
how to declare the port setting in constructor ................please suggest
I tried but program did not ran...maybe I am bit weak in C++

Added after 1 6 minutes:

i did modified my code but still I am recieving the garbage value
my code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include "qtimer.h"



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
port=new QextSerialPort("COM4");
//port->setPortName("COM4");
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->open( QIODevice::ReadOnly);
//port->flush();


QTimer *timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timeupda te()));
timer->start(1000);
}

MainWindow::~MainWindow()
{
delete ui;
port->close();
}
void MainWindow::timeupdate()
{
port->flush();
char buff[1024];
if(port->isReadable())
{
qint64 i=port->read(buff,4);

buff[i]='\0';
if(i!=-1)
{
QString str(buff);
ui->lineEdit->setText(str);

}

} else
qDebug("cannot open the port");
}

and

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <qextserialport.h>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();


void run();
public slots:
void timeupdate();


private:
Ui::MainWindow *ui;

QextSerialPort *port;
};

#endif // MAINWINDOW_H


please help me to correct it ....since i am recieving garbage value at lineedit as my controller is sending character continiously i.e. character "a"

robotics
21st May 2011, 14:37
please help ...
somebody somewhere might have the solution to this ......
please,....
your help shall be highly appreciated

squidge
21st May 2011, 16:55
If your not going to be using the signals of QextSerialPort then you should use QextSerialPort::Polling in your constructor.

kuzulis
21st May 2011, 17:58
robotics,

You need it QExtSerialPort? or consider other options (other libraries, such as QSerialDevice https://gitorious.org/qserialdevice)?
There are examples of GUI applications /test/guiapp. :)

robotics
22nd May 2011, 04:37
yessssssssss
I have sucessfully recieved data in my gui application .......
my GUI now perfectly recieves the data in my GUI application
thanks you guys for helping me
If you want the code or help you can contact me::
suraj@robotics.org.np

un9tsandeep
24th September 2011, 11:51
can anyone give me idea for QextSerialPort and QserialDevice. i am new in QT programming i have make a GUI for Communicate with Serial Port using QextSerialPort and QserialDevice both but my data is not coming complete some of data is getting lost.

it is in the case of when i comunicate with USB Devices(Vertual Com port) using FTDI Chip, MicroChip AND TI USB com Emulators(/dev/ttyUSB0,/dev/ttyACM0,,,,etc). not in case of COM Port Available in Computer(/dev/ttyS0,/devttyS1....etc)

kuzulis
24th September 2011, 13:47
not coming complete some of data is getting lost
Seems to me you're stretching the truth somewhere. ;)

Show your code (for example, QSerialDevice) and library version.
But better - bring a small ready sample project, which demonstrates the loss of data.

un9tsandeep
26th September 2011, 06:47
can you give me example of your application so i can see what mistake i am doing if possible please attach your example program
i am also giving my code below.......

in this i have use QserialDevice

pro file


# -------------------------------------------------
# Project created by QtCreator 2011-08-29T12:45:21
# -------------------------------------------------
QT += core gui

TARGET = SERIAL
TEMPLATE = app

unix:include(qserialdevice/src/unix/ttylocker.pri)
include(qserialdevice/src/qserialdeviceenumerator/qserialdeviceenumerator.pri)
include(qserialdevice/src/qserialdevice/qserialdevice.pri)

SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui

win32 {
LIBS += -lsetupapi -luuid -ladvapi32
}
unix:!macx {
LIBS += -ludev
}


mainwindow.h file:-


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QtGui>
#include <QDialog>
#include <QtCore/QObject>
#include <abstractserial.h>
#include <serialdeviceenumerator.h>

namespace Ui {
class MainWindow;
}
class SerialDeviceEnumerator;
class AbstractSerial;
class MainWindow : public QMainWindow {
Q_OBJECT
signals:
void sendSerialData(const QByteArray &data);
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
//void setTitle(const QString);
void start(bool enable);
public slots:
void pushButton_Clicked();
void pushButton2_Clicked();
void pushButton3_Clicked();
void printTrace();

protected:
void changeEvent(QEvent *e);

private:
AbstractSerial *port;
QTimer *timer;
Ui::MainWindow *ui;
int responseTimeout;
};

#endif // MAINWINDOW_H




mainwindow.cpp file:-



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QtGui>
#include <QDialog>
#include <QMessageBox>
#include <QtCore/QCoreApplication>
#include <abstractserial.h>
#include <serialdeviceenumerator.h>



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),responseTimeout(200)//, m_queryLen(MinQueryLen)
{
ui->setupUi(this);
port = new AbstractSerial(this);

connect ( ui->pushButton, SIGNAL( clicked() ), this, SLOT( pushButton_Clicked() ) );
connect ( ui->pushButton_2, SIGNAL( clicked() ), this, SLOT( pushButton2_Clicked() ) );
connect ( ui->pushButton_3, SIGNAL( clicked() ), this, SLOT( pushButton3_Clicked() ) );
this->ui->comboBox->addItem("/dev/ttyS0",QVariant::Char);
this->ui->comboBox->addItem("/dev/ttyS1",QVariant::Char);
this->ui->comboBox->addItem("/dev/ttyUSB0",QVariant::Char);
this->ui->comboBox->addItem("/dev/ttyACM0",QVariant::Char);
this->ui->comboBox->setCurrentIndex(0);

timer = new QTimer(this);
timer->setInterval(20); //polling interval
connect(timer, SIGNAL(timeout()), this, SLOT(printTrace()));
}

MainWindow::~MainWindow()
{
delete ui;
port->close();
start(false);
}

void MainWindow::changeEvent(QEvent *e)
{

QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::pushButton_Clicked()

{ //QMessageBox::information( this, "Information", ui->comboBox->currentText() +" Selected" );
port->setDeviceName(ui->comboBox->currentText());
port->setBaudRate(AbstractSerial::BaudRate115200);
port->setDataBits(AbstractSerial::DataBits8);
port->setParity(AbstractSerial::ParityNone);
port->setStopBits(AbstractSerial::StopBits1);
port->setFlowControl(AbstractSerial::FlowControlOff);

if ( !port->open(AbstractSerial::ReadWrite | AbstractSerial::Unbuffered) )
{
QMessageBox::information( this, "Information", port->deviceName() +" open fail." );
return;
}
else
{
QMessageBox::information( this, "Information", port->deviceName() +" open Successful" );

}

}
void MainWindow::pushButton2_Clicked()
{
QByteArray data;
data.append(ui->lineEdit->text());

if (data.size() > 0)
{
port->flush();
port->write(data);
this->printTrace();
}

}
void MainWindow::printTrace()
{


port->flush();
QByteArray data1;
QString s;

if ((port->bytesAvailable() > 0) || port->waitForReadyRead(responseTimeout))
{
s = port->readAll();
ui->textEdit->insertPlainText(s);


}

start(true);

}
void MainWindow::pushButton3_Clicked()
{
port->close();
start(false);
}
void MainWindow::start(bool enable)
{
if (enable)
timer->start();
else
timer->stop();
}

high_flyer
28th September 2011, 15:11
Why do you use both QextSerialPort and QSerialDevice?
They both are meant for the same thing...