Results 1 to 13 of 13

Thread: First attempt to display serial port data on GUI

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: First attempt to display serial port data on GUI

    No, you just pass the same string, reinitialized. You don't delete it, just empty it. You will delete it just once, in the destructor of the worker thread.

    You only have to be careful that you don't use the pointer to the QString after you delete the worker thread.

    Regards

  2. The following user says thank you to marcel for this useful post:

    ShaChris23 (2nd May 2007)

  3. #2
    Join Date
    Apr 2007
    Posts
    62
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    43
    Thanked 1 Time in 1 Post

    Default Re: First attempt to display serial port data on GUI

    Here's my "final" implementation. It uses a thread to read data from the serial port, and sends a signal to a GUI class to slot update the value to the screen. The CPU load on this is 0-1% as the Serial Port Read is a true blocking function call.


    There are 2 remarks about this:

    1) I notice that as time goes on, the program's memory keeps increasing (I checked from Windows task manager. Not a lot, about 4 KB per minute. I couldnt think of anything that could cause this. I tried taking out the serial port Read code, and the program's size still kept increasing. Can it be because of signal/slot?

    2) I'm currently passing signal/slot by value. If somebody knows a better way of passing the QString and could show it to me, that'd be great.

    That was fun though, now I start to get a hang of it, and I'm going to start adding features.

    SerialPortReader.hpp
    Qt Code:
    1. #pragma once
    2.  
    3. #include "CSerial.hpp"
    4. #include <QString>
    5. #include <QStringList>
    6. #include <QThread>
    7.  
    8. class SerialPortReader : public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. SerialPortReader();
    14. void run();
    15.  
    16. signals:
    17. void newString( QString string );
    18.  
    19. private:
    20. QString* str;
    21. QStringList* list;
    22. CSerial* port;
    23. };
    To copy to clipboard, switch view to plain text mode 

    SerialPortReader.cpp
    Qt Code:
    1. #include "SerialPortReader.hpp"
    2.  
    3. #include <tchar.h>
    4. #include <assert.h>
    5.  
    6. SerialPortReader::SerialPortReader()
    7. {
    8. str = new QString;
    9. list = new QStringList;
    10. port = new CSerial;
    11.  
    12. LONG stat;
    13.  
    14. stat = port->Open( _T("COM1"), 0, 0, true );
    15. assert( stat == ERROR_SUCCESS );
    16.  
    17. stat = port->Setup( CSerial::EBaud115200,
    18. CSerial::EData8,
    19. CSerial::EParNone,
    20. CSerial::EStop1 );
    21. assert( stat == ERROR_SUCCESS );
    22.  
    23. stat = port->SetupHandshaking( CSerial::EHandshakeOff );
    24. assert( stat == ERROR_SUCCESS );
    25.  
    26. stat = port->SetupReadTimeouts( CSerial::EReadTimeoutBlocking );
    27. assert( stat == ERROR_SUCCESS );
    28.  
    29. }
    30.  
    31. void SerialPortReader::run()
    32. {
    33. char data[100] = "Hey man how are you?";
    34.  
    35. while(1)
    36. {
    37. int charIdx = 0;
    38. DWORD bytesRead;
    39.  
    40. //
    41. // Find sync word
    42. do
    43. {
    44. data[charIdx] = 'A';
    45. port->Read( &data[charIdx],
    46. 1,
    47. &bytesRead );
    48. } while( data[charIdx] != '$' );
    49. charIdx++;
    50.  
    51. //
    52. // Read the rest of the data
    53. do
    54. {
    55. port->Read( &data[charIdx],
    56. 1,
    57. &bytesRead );
    58. charIdx++;
    59. } while( data[charIdx-1] != '*' );
    60.  
    61.  
    62. //
    63. // Read check sum
    64. port->Read( &data[charIdx],
    65. 2,
    66. &bytesRead );
    67. charIdx += 2;
    68. data[charIdx] = '\0';
    69.  
    70. //
    71. // Copy buffer to QString
    72. *str = data;
    73.  
    74. //
    75. // Indicate there's a new string
    76. emit newString( *str );
    77. }
    78. }
    To copy to clipboard, switch view to plain text mode 

    GUI.hpp
    Qt Code:
    1. #ifndef QTAPP_H
    2. #define QTAPP_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_qtapp.h"
    6. #include <QLabel>
    7. #include <QString>
    8. #include "SerialPortReader.hpp"
    9.  
    10. class QtApp : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. QtApp(QWidget *parent = 0, Qt::WFlags flags = 0);
    16.  
    17. private:
    18. Ui::QtAppClass ui;
    19. SerialPortReader* portThread;
    20.  
    21. public slots:
    22. void DisplayString( QString string );
    23. };
    24.  
    25. #endif // QTAPP_H
    To copy to clipboard, switch view to plain text mode 

    GUI.cpp
    Qt Code:
    1. #include "qtapp.h"
    2.  
    3. QtApp::QtApp(QWidget *parent, Qt::WFlags flags)
    4. : QMainWindow(parent, flags)
    5. {
    6. ui.setupUi(this);
    7.  
    8. // Start a new thread
    9. portThread = new SerialPortReader;
    10.  
    11. connect( portThread, SIGNAL(newString(QString)),
    12. this, SLOT(DisplayString(QString)) );
    13.  
    14. portThread->start();
    15. }
    16.  
    17. void QtApp::DisplayString( QString string )
    18. {
    19. ui.mLabel->setText( string );
    20. }
    To copy to clipboard, switch view to plain text mode 

    UI Generated by Designer
    Qt Code:
    1. class Ui_QtAppClass
    2. {
    3. public:
    4. QWidget *centralWidget;
    5. QLabel *mLabel;
    6. QStatusBar *statusBar;
    7.  
    8. //....
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to ShaChris23 for this useful post:

    Rocken (12th March 2012)

  5. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: First attempt to display serial port data on GUI

    I can't see anywhere why you get those mem leaks... Maybe I'm missing something. Anyway, you should really add destructors to your classes and delete all the pointers you allocate. You will have some leaks in the thread class.

    Anyway, why don't you just use a queue of qstrings instead of the single qstring (QQueue. You will pass a pointer to this QQueue to the main GUI, in the rest it is a standard producer/consumer scenario. The GUI thread takse QStrings of the top of the Queue, the worker thread appends them as soon as it gets them.

    I realize now you can't pass a single QString pointer because it could get overwritten in the mean time, before it is displayed in the GUI.

    Regards

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

    ShaChris23 (3rd May 2007)

  7. #4
    Join Date
    Dec 2006
    Posts
    123
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: First attempt to display serial port data on GUI

    ShaChris23:

    can you say me how did you link the qextserial package to qt ? actually i'm trying to read the data's coming from the serial port.i found in the some forum posts that we can use qextserial package. i downloaded and compiled it for my linux PC also .but i could not figure how to use it with the qt. when i compiled it, i found only the libraries where created. In your code i found that you had added the header file "#include "qextserialport.h"" and created an object for qextserial. so i my code if i simply add the header will it work ? or should i place the library in some location in qt-embedded

Similar Threads

  1. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 02:38
  2. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 02:05
  3. Serial Port access in Qt
    By Doug Broadwell in forum Newbie
    Replies: 2
    Last Post: 18th October 2006, 21:03
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.