PDA

View Full Version : Serial Port Communication for USB Devices



un9tsandeep
24th September 2011, 12:57
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)

pkj
24th September 2011, 20:02
I have used QExtSerialPort and there is no problem with the class. Through usb emulator it creates a lock on the serial port sometimes. Never have I faced a problem incomplete data! And the lock is an issue of emulator. Not much you can do other than reboot.

un9tsandeep
26th September 2011, 07:46
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();
}

kuzulis
26th September 2011, 09:53
2 un9tsandeep

You're doing wrong!
Read the documentation carefully and see examples.

1. Configure the port after to the opening and not vice versa.
You should have at least bothered to check the return values ​​setBaudRate(), setDataBits(), etc.
And to understand what you're doing something wrong.

2. If you open the port mode: AbstractSerial::Unbuffered then you just have to be sure that you do.
Аnd not "stupid" to do copy-paste from the examples, not understanding.

3. Use signal readyRead()!

un9tsandeep
26th September 2011, 12:49
thank you friend

you have given good information

as you have told me i have made correction in my code and i m getting output but still their is some problem in my code so due to that i m not getting what i want.

after your suggestion improvement in output is their but not what i need.

i have set port mode: AbstractSerial::Unbuffered and i have set port->setCharIntervalTimeout(5000); also

but still some data is missing please if u can give me some more idea about "port mode: AbstractSerial::Unbuffered"

do i m going correct or not

can you give me some code by which i can understand it if you have. qt is new for me and i m nt good in it thats why it is difficult for me.

changes i made shown below

cpp file

void MainWindow::pushButton_Clicked()

{
port->setDeviceName(ui->comboBox->currentText());

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

port->setBaudRate(AbstractSerial::BaudRate115200);
port->setDataBits(AbstractSerial::DataBits8);
port->setParity(AbstractSerial::ParityNone);
port->setStopBits(AbstractSerial::StopBits1);
port->setFlowControl(AbstractSerial::FlowControlOff);
port->setCharIntervalTimeout(5000);//For unix
QMessageBox::information( this, "Information", port->deviceName() +" open Successful" );

}

}
void MainWindow::slotRead()
{
QString s=port->readAll();
ui->textEdit->insertPlainText(s.toAscii().toHex());
}

kuzulis
26th September 2011, 13:29
2 un9tsandeep,

Try use

/test/guiapp
and/or
/test/guiapp2

and tell the result: that it is not working, how do you expect the data, etc.

un9tsandeep
26th September 2011, 13:46
i have used those examples and after analyzing them i have made this code.

actually i have a wireless receiver device by which i m getting the output which have FTDI Chip which males a virtual serial port for data transfer, it receives data from a clicker device when we press any key of it.

the output is in Hex

i am getting the output but not full

right now i am getting

7b04faa2 and it should be end with 7d

or

7b05faa201aaaaaa03 and it should be end with 7d

in windows it is working fine (in Visual Studio) but in Ubuntu ( in QT) it gives some less data.

thats why i am asking this Question.

Can you please give me example code rather then
/test/guiapp
and/or
/test/guiapp2

it really helps me.

previously i m working in Visual Studio but now I want to go for QT as i want my programs work in Linux also.

kuzulis
26th September 2011, 14:23
I just checked (using the /test/guiapp) with an adapter PL2303.
Bound cross-cable two-port /dev/ttyS0 and /dev/ttyUSB0, launched two instances of /test/guiapp, configure ports 1 115 200 8 N Off.
From one /test/guiapp send "7b05faa201aaaaaa037d", another /test/guiapp takes "7b05faa201aaaaaa037d".
I have everything working correctly.

Try it yourself to find the problem, Go debugger, etc.

PS: I used a library of branches master and ArchLinux.

un9tsandeep
26th September 2011, 15:02
dear friend.

thanks to you for giving support.

i have solved the problem.

the problem was in readAll();
i was taking Qstring every time in my code and it removes spetial cherecters from my Output. now i am using byteArray instid of it and the problem solved.

i will be in touch with you, you have strong knowledge. agin thanks for your post....

kuzulis
26th September 2011, 15:22
i will be in touch with you, you have strong knowledge
That's because I'm the developer of the library QSerialDevice. :)

un9tsandeep
27th September 2011, 06:37
ohhhhhh........

its my pleasure to interact with you, i have no idea about it. and you are really a nice person.........

QserialDevice is a grate library really helpfull and good then QextSerialPort.

un9tsandeep
30th September 2011, 15:04
How Can i detect Available Serial ports of my system....................

kuzulis
30th September 2011, 18:38
>How Can i detect Available Serial ports of my system....................
In QSerialDevice see examples in /examples and /test directory

un9tsandeep
4th October 2011, 09:04
i have checked the example and test program but i doesn't find in those that available ports are used...............

and do you have any idea by which i can install ODBC and MYSQL drivers in QT for connecting Databases.

in qt which i have installed only SQlite and SQlite2 drivers are available....

how can i add more drivers, i have search web for it but problem not solved......

if any one have idea on this...............

kuzulis
4th October 2011, 09:11
i have checked the example and test program but i doesn't find in those that available ports are used...............
So you watched not attentively. That's your problem.



and do you have any idea by which i can install ODBC and MYSQL drivers in QT for connecting Databases.

in qt which i have installed only SQlite and SQlite2 drivers are available....

how can i add more drivers, i have search web for it but problem not solved......

if any one have idea on this..............

This does not apply to the topic of the thread. Start a new thread.

un9tsandeep
4th October 2011, 11:21
thank u very much for ur suggestion...........