Results 1 to 20 of 20

Thread: serial port communication

  1. #1
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial port communication

    QExtSerialPort comes with lots of examples already...

  3. The following user says thank you to squidge for this useful post:

    robotics (21st May 2011)

  4. #3
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default 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..
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "qextserialport.h"
    4. #include "qdebug.h"
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::on_pushButton_clicked()
    18. {
    19. QextSerialPort * port = new QextSerialPort();
    20. port->setPortName("COM4");
    21. port->setBaudRate(BAUD9600);
    22. port->setFlowControl(FLOW_HARDWARE);
    23. port->setParity(PAR_NONE);
    24. port->setDataBits(DATA_8);
    25. port->setStopBits(STOP_1);
    26. port->open( QIODevice::ReadOnly );
    27. char *buff;
    28. if(port->isReadable())
    29. {
    30. qint64 i=port->read(buff,1);
    31. buff[i]='\0';
    32. if(i!=-1)
    33. {
    34. QString str(buff);
    35. ui->lineEdit->setText(str);
    36. port->close();
    37. }
    38. }
    39.  
    40. else
    41. qDebug("port not open");
    42. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. The following user says thank you to high_flyer for this useful post:

    robotics (21st May 2011)

  7. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

  8. #6
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "qextserialport.h"
    4. #include "qdebug.h"
    5. #include "qtimer.h"
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. QTimer *timer=new QTimer(this);
    12. connect(timer,SIGNAL(timeout()),this,SLOT(timeupdate()));
    13. timer->start(500);
    14. // QextSerialPort * serialport=new QextSerialPort;
    15. // connect(serialport,SIGNAL(readyRead()),this,SLOT(timeupdate()));
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23.  
    24. void MainWindow::timeupdate()
    25. {
    26. QextSerialPort * port = new QextSerialPort();
    27. port->setPortName("COM4");
    28. port->setBaudRate(BAUD9600);
    29. port->setFlowControl(FLOW_HARDWARE);
    30. port->setParity(PAR_NONE);
    31. port->setDataBits(DATA_8);
    32. port->setStopBits(STOP_1);
    33. //port->setTimeout(0);
    34. port->open( QIODevice::ReadOnly );
    35. char buff[4];
    36. if(port->isReadable())
    37. {
    38. qint64 i=port->read(buff,1);
    39. buff[i]='\0';
    40. if(i!=-1)
    41. {
    42. QString str(buff);
    43. ui->lineEdit->setText(str);
    44. port->close();
    45. }
    46. }
    47.  
    48. else
    49. qDebug("port not open");
    50. }
    To copy to clipboard, switch view to plain text mode 
    and In mainwindow.h
    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. namespace Ui {
    4. class MainWindow;
    5. }
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14.  
    15. private:
    16. Ui::MainWindow *ui;
    17.  
    18. private slots:
    19.  
    20. void timeupdate();
    21. };
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: serial port communication

    Why are You opening and closing port again and again ?????
    Open port in MainWindow constructor and close it in destructor.

  10. The following user says thank you to Lesiok for this useful post:

    robotics (21st May 2011)

  11. #8
    Join Date
    May 2011
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded

    Default Re: serial port communication

    I remeber making a working program and I started with the examples.. check those first

  12. The following user says thank you to serjts for this useful post:

    robotics (21st May 2011)

  13. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial port communication

    Even worse, every 500ms you are creating a new QextSerialPort but never deleting it

    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.

  14. The following user says thank you to squidge for this useful post:

    robotics (21st May 2011)

  15. #10
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial port communication

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

  16. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial port communication

    So everything works correctly if you use a terminal program such as Hyperterm?

  17. The following user says thank you to squidge for this useful post:

    robotics (21st May 2011)

  18. #12
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial port communication

    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
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "qextserialport.h"
    4. #include "qtimer.h"
    5.  
    6.  
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. port=new QextSerialPort("COM4");
    14. //port->setPortName("COM4");
    15. port->setBaudRate(BAUD9600);
    16. port->setFlowControl(FLOW_OFF);
    17. port->setParity(PAR_NONE);
    18. port->setDataBits(DATA_8);
    19. port->setStopBits(STOP_1);
    20. port->open( QIODevice::ReadOnly);
    21. //port->flush();
    22.  
    23.  
    24. QTimer *timer=new QTimer(this);
    25. connect(timer,SIGNAL(timeout()),this,SLOT(timeupdate()));
    26. timer->start(1000);
    27. }
    28.  
    29. MainWindow::~MainWindow()
    30. {
    31. delete ui;
    32. port->close();
    33. }
    34. void MainWindow::timeupdate()
    35. {
    36. port->flush();
    37. char buff[1024];
    38. if(port->isReadable())
    39. {
    40. qint64 i=port->read(buff,4);
    41.  
    42. buff[i]='\0';
    43. if(i!=-1)
    44. {
    45. QString str(buff);
    46. ui->lineEdit->setText(str);
    47.  
    48. }
    49.  
    50. } else
    51. qDebug("cannot open the port");
    52. }
    To copy to clipboard, switch view to plain text mode 

    and
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <qextserialport.h>
    6.  
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20.  
    21. void run();
    22. public slots:
    23. void timeupdate();
    24.  
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28.  
    29. QextSerialPort *port;
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    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"
    Last edited by robotics; 21st May 2011 at 12:15.

  19. #13
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial port communication

    please help ...
    somebody somewhere might have the solution to this ......
    please,....
    your help shall be highly appreciated

  20. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial port communication

    If your not going to be using the signals of QextSerialPort then you should use QextSerialPort::Polling in your constructor.

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

    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.

  22. #16
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

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

    Default 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)

  24. #18
    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

    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.

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

    Default 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

    Qt Code:
    1. # -------------------------------------------------
    2. # Project created by QtCreator 2011-08-29T12:45:21
    3. # -------------------------------------------------
    4. QT += core gui
    5.  
    6. TARGET = SERIAL
    7. TEMPLATE = app
    8.  
    9. unix:include(qserialdevice/src/unix/ttylocker.pri)
    10. include(qserialdevice/src/qserialdeviceenumerator/qserialdeviceenumerator.pri)
    11. include(qserialdevice/src/qserialdevice/qserialdevice.pri)
    12.  
    13. SOURCES += main.cpp\
    14. mainwindow.cpp
    15. HEADERS += mainwindow.h
    16. FORMS += mainwindow.ui
    17.  
    18. win32 {
    19. LIBS += -lsetupapi -luuid -ladvapi32
    20. }
    21. unix:!macx {
    22. LIBS += -ludev
    23. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h file:-

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QApplication>
    6. #include <QHBoxLayout>
    7. #include <QPushButton>
    8. #include <QtGui>
    9. #include <QDialog>
    10. #include <QtCore/QObject>
    11. #include <abstractserial.h>
    12. #include <serialdeviceenumerator.h>
    13.  
    14. namespace Ui {
    15. class MainWindow;
    16. }
    17. class SerialDeviceEnumerator;
    18. class AbstractSerial;
    19. class MainWindow : public QMainWindow {
    20. Q_OBJECT
    21. signals:
    22. void sendSerialData(const QByteArray &data);
    23. public:
    24. MainWindow(QWidget *parent = 0);
    25. ~MainWindow();
    26. //void setTitle(const QString);
    27. void start(bool enable);
    28. public slots:
    29. void pushButton_Clicked();
    30. void pushButton2_Clicked();
    31. void pushButton3_Clicked();
    32. void printTrace();
    33.  
    34. protected:
    35. void changeEvent(QEvent *e);
    36.  
    37. private:
    38. AbstractSerial *port;
    39. QTimer *timer;
    40. Ui::MainWindow *ui;
    41. int responseTimeout;
    42. };
    43.  
    44. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 




    mainwindow.cpp file:-


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QApplication>
    4. #include <QHBoxLayout>
    5. #include <QPushButton>
    6. #include <QtGui>
    7. #include <QDialog>
    8. #include <QMessageBox>
    9. #include <QtCore/QCoreApplication>
    10. #include <abstractserial.h>
    11. #include <serialdeviceenumerator.h>
    12.  
    13.  
    14.  
    15. MainWindow::MainWindow(QWidget *parent) :
    16. QMainWindow(parent),
    17. ui(new Ui::MainWindow),responseTimeout(200)//, m_queryLen(MinQueryLen)
    18. {
    19. ui->setupUi(this);
    20. port = new AbstractSerial(this);
    21.  
    22. connect ( ui->pushButton, SIGNAL( clicked() ), this, SLOT( pushButton_Clicked() ) );
    23. connect ( ui->pushButton_2, SIGNAL( clicked() ), this, SLOT( pushButton2_Clicked() ) );
    24. connect ( ui->pushButton_3, SIGNAL( clicked() ), this, SLOT( pushButton3_Clicked() ) );
    25. this->ui->comboBox->addItem("/dev/ttyS0",QVariant::Char);
    26. this->ui->comboBox->addItem("/dev/ttyS1",QVariant::Char);
    27. this->ui->comboBox->addItem("/dev/ttyUSB0",QVariant::Char);
    28. this->ui->comboBox->addItem("/dev/ttyACM0",QVariant::Char);
    29. this->ui->comboBox->setCurrentIndex(0);
    30.  
    31. timer = new QTimer(this);
    32. timer->setInterval(20); //polling interval
    33. connect(timer, SIGNAL(timeout()), this, SLOT(printTrace()));
    34. }
    35.  
    36. MainWindow::~MainWindow()
    37. {
    38. delete ui;
    39. port->close();
    40. start(false);
    41. }
    42.  
    43. void MainWindow::changeEvent(QEvent *e)
    44. {
    45.  
    46. QMainWindow::changeEvent(e);
    47. switch (e->type()) {
    48. case QEvent::LanguageChange:
    49. ui->retranslateUi(this);
    50. break;
    51. default:
    52. break;
    53. }
    54. }
    55. void MainWindow::pushButton_Clicked()
    56.  
    57. { //QMessageBox::information( this, "Information", ui->comboBox->currentText() +" Selected" );
    58. port->setDeviceName(ui->comboBox->currentText());
    59. port->setBaudRate(AbstractSerial::BaudRate115200);
    60. port->setDataBits(AbstractSerial::DataBits8);
    61. port->setParity(AbstractSerial::ParityNone);
    62. port->setStopBits(AbstractSerial::StopBits1);
    63. port->setFlowControl(AbstractSerial::FlowControlOff);
    64.  
    65. if ( !port->open(AbstractSerial::ReadWrite | AbstractSerial::Unbuffered) )
    66. {
    67. QMessageBox::information( this, "Information", port->deviceName() +" open fail." );
    68. return;
    69. }
    70. else
    71. {
    72. QMessageBox::information( this, "Information", port->deviceName() +" open Successful" );
    73.  
    74. }
    75.  
    76. }
    77. void MainWindow::pushButton2_Clicked()
    78. {
    79. data.append(ui->lineEdit->text());
    80.  
    81. if (data.size() > 0)
    82. {
    83. port->flush();
    84. port->write(data);
    85. this->printTrace();
    86. }
    87.  
    88. }
    89. void MainWindow::printTrace()
    90. {
    91.  
    92.  
    93. port->flush();
    94. QByteArray data1;
    95.  
    96. if ((port->bytesAvailable() > 0) || port->waitForReadyRead(responseTimeout))
    97. {
    98. s = port->readAll();
    99. ui->textEdit->insertPlainText(s);
    100.  
    101.  
    102. }
    103.  
    104. start(true);
    105.  
    106. }
    107. void MainWindow::pushButton3_Clicked()
    108. {
    109. port->close();
    110. start(false);
    111. }
    112. void MainWindow::start(bool enable)
    113. {
    114. if (enable)
    115. timer->start();
    116. else
    117. timer->stop();
    118. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 28th September 2011 at 15:10. Reason: code tags

  26. #20
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: serial port communication

    Why do you use both QextSerialPort and QSerialDevice?
    They both are meant for the same thing...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 13
    Last Post: 8th August 2012, 08:26
  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.