serial port communication
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
Re: serial port communication
QExtSerialPort comes with lots of examples already...
Re: serial port communication
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..:(
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include "qdebug.h"
MainWindow
::MainWindow(QWidget *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);
char *buff;
if(port->isReadable())
{
qint64 i=port->read(buff,1);
buff[i]='\0';
if(i!=-1)
{
ui->lineEdit->setText(str);
port->close();
}
}
else
qDebug("port not open");
}
Re: serial port communication
your 'buff' is not initialized!!
And since you always only read one char, why do you use a pointer and not just a char?
Re: serial port communication
You should also be using signals and slots.
I assume your micro is sending just text? Because thats all a lineedit can display.
Re: serial port communication
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
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include "qdebug.h"
#include "qtimer.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timeupdate()));
timer->start(500);
// QextSerialPort * serialport=new QextSerialPort;
// connect(serialport,SIGNAL(readyRead()),this,SLOT(timeupdate()));
}
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);
char buff[4];
if(port->isReadable())
{
qint64 i=port->read(buff,1);
buff[i]='\0';
if(i!=-1)
{
ui->lineEdit->setText(str);
port->close();
}
}
else
qDebug("port not open");
}
and In mainwindow.h
Code:
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void timeupdate();
};
Re: serial port communication
Why are You opening and closing port again and again ?????
Open port in MainWindow constructor and close it in destructor.
Re: serial port communication
I remeber making a working program and I started with the examples.. check those first
Re: serial port communication
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.
Re: serial port communication
Quote:
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...
Quote:
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...
Re: serial port communication
So everything works correctly if you use a terminal program such as Hyperterm?
Re: serial port communication
Quote:
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....
Quote:
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
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qextserialport.h"
#include "qtimer.h"
MainWindow
::MainWindow(QWidget *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->flush();
connect(timer,SIGNAL(timeout()),this,SLOT(timeupdate()));
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)
{
ui->lineEdit->setText(str);
}
} else
qDebug("cannot open the port");
}
and
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <qextserialport.h>
namespace Ui {
class MainWindow;
}
{
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"
Re: serial port communication
please help ...
somebody somewhere might have the solution to this ......
please,....
your help shall be highly appreciated
Re: serial port communication
If your not going to be using the signals of QextSerialPort then you should use QextSerialPort::Polling in your constructor.
Re: serial port communication
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. :)
Re: serial port communication
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
Re: serial port communication
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)
Re: serial port communication
Quote:
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.
Re: serial port communication
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
Code:
# -------------------------------------------------
# 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:-
Code:
#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;
Q_OBJECT
signals:
public:
~MainWindow();
//void setTitle(const QString);
void start(bool enable);
public slots:
void pushButton_Clicked();
void pushButton2_Clicked();
void pushButton3_Clicked();
void printTrace();
protected:
private:
AbstractSerial *port;
Ui::MainWindow *ui;
int responseTimeout;
};
#endif // MAINWINDOW_H
mainwindow.cpp file:-
Code:
#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
) :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->setInterval(20); //polling interval
connect(timer, SIGNAL(timeout()), this, SLOT(printTrace()));
}
MainWindow::~MainWindow()
{
delete ui;
port->close();
start(false);
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
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()
{
data.append(ui->lineEdit->text());
if (data.size() > 0)
{
port->flush();
port->write(data);
this->printTrace();
}
}
void MainWindow::printTrace()
{
port->flush();
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();
}
Re: serial port communication
Why do you use both QextSerialPort and QSerialDevice?
They both are meant for the same thing...