Results 1 to 16 of 16

Thread: Serial Port Communication for USB Devices

  1. #1
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Serial Port Communication for USB Devices

    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)

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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.

  3. #3
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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();
    }

  4. #4
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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()!

  5. #5
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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());
    }

  6. #6
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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.

  7. #7
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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.

  8. #8
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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.

  9. #9
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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....

  10. #10
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    i will be in touch with you, you have strong knowledge
    That's because I'm the developer of the library QSerialDevice.

  11. #11
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Serial Port Communication for USB Devices

    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.

  12. #12
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    How Can i detect Available Serial ports of my system....................

  13. #13
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    >How Can i detect Available Serial ports of my system....................
    In QSerialDevice see examples in /examples and /test directory

  14. #14
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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...............

  15. #15
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    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.

  16. #16
    Join Date
    Sep 2011
    Location
    India
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Communication for USB Devices

    thank u very much for ur suggestion...........
    Last edited by un9tsandeep; 4th October 2011 at 11:19.

Similar Threads

  1. serial port communication
    By robotics in forum Qt Programming
    Replies: 19
    Last Post: 28th September 2011, 15:11
  2. gui application for serial port communication
    By jerkymotion in forum Newbie
    Replies: 11
    Last Post: 9th March 2011, 21:43
  3. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 02:38
  4. serial port and USB communication
    By shamik in forum Qt Programming
    Replies: 5
    Last Post: 4th December 2006, 10:40
  5. Serial Port Communication
    By soldstatic in forum Qt Programming
    Replies: 6
    Last Post: 22nd June 2006, 16:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.