Results 1 to 20 of 20

Thread: serial port communication

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,540
    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)

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
  •  
Qt is a trademark of The Qt Company.