Results 1 to 7 of 7

Thread: Hints to design threaded serial-reader/writer interface ?

  1. #1
    Join Date
    Feb 2014
    Posts
    15
    Thanks
    3

    Default Hints to design threaded serial-reader/writer interface ?

    Already created a GUI-program that reads & writes (mostly writes) through serial port with an event loop. I managed more or less that the GUI be responsive but logically the right way is to use threads. So my intention is to move the reading & writing of serial port on a separate thread.

    What I have until now:

    Qt Code:
    1. class SerialIO : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. SerialIO(QObject *parent = 0);
    7. ~SerialIO();
    8. void run();
    9. void addData(const QByteArray); //to out_buffer
    10. const QByteArray getData(); //from in_buffer
    11.  
    12. signals:
    13. //not sure...
    14. void readyToSend();
    15. void readyToRecv();
    16.  
    17. public slots:
    18. void send();
    19. void recv();
    20.  
    21. private:
    22. QSerialPort *serial;
    23. QBuffer out_buffer;
    24. QBuffer in_buffer;
    25.  
    26.  
    27. };
    To copy to clipboard, switch view to plain text mode 

    As the main task is to write a file through the serial port, my idea is to inject the file line by line, filling the buffer and the thread class will send it at his own pace. What I receive are ACK's basically.
    Last edited by oldFox64; 24th April 2014 at 08:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Hints to design threaded serial-reader/writer interface ?

    I am not sure what you mean with "logically right way".

    Usually taking something that works and making it harder to debug and maintain is more likely the opposite of logical

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    15
    Thanks
    3

    Default Re: Hints to design threaded serial-reader/writer interface ?

    it """works""", I mean, I made something very dirty.

    I know that the proper way to do this is with threads. I did another very similar project but in Java, which was easier for me thanks to a more complete API.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Hints to design threaded serial-reader/writer interface ?

    My hint is - don't mix the logic and implementation. Application "logic" requires that there should be an object that handles communication with the serial port, but the fact that it should/shouldn't use threads is specific to implementation (another "logic" requirement could be that it shouldn't block the ui, but that can be achieved without threads). So my suggestion is to wrap the serial communication mechanism into a class interface, and use some threading mechanism as another layer - through QThread, thread pools, QtConcurrent or whatever suits your needs.
    So don't do this:
    Qt Code:
    1. class SerialIO : public QThread
    2. {
    3. ...
    4. }
    5. // what does it even mean that "Every SerialIO object is a kind of Thread object" ?
    To copy to clipboard, switch view to plain text mode 
    but rather:
    Qt Code:
    1. class SerialIO : public QObject
    2. {
    3. ...
    4. }
    5. ...
    6.  
    7. SerialIO * io = new SerialIO(...);
    8. QThread * thread = new QThread(...);
    9. io->moveToThread(thread);
    To copy to clipboard, switch view to plain text mode 
    this way you have a possibility to enable / disable threading support for the serial io without changing the core communication code.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Hints to design threaded serial-reader/writer interface ?

    Quote Originally Posted by oldFox64 View Post
    I know that the proper way to do this is with threads. I did another very similar project but in Java, which was easier for me thanks to a more complete API.
    Java is a bad example in this context, because it lacked asynchronous I/O for a long time and a lot if its APIs are still blocking by default.

    Qt, on the other hand, is primarily designed to be non-blocking, I/O being handled asynchronously (operation returning immediately, results being signalled).

    If you really must increase the complexity of your program then the approach suggested by stampede is usually the way to go.
    It allows everything to be tested with a single thread, etc.

    Cheers,
    _

  6. #6
    Join Date
    Feb 2014
    Posts
    15
    Thanks
    3

    Default Re: Hints to design threaded serial-reader/writer interface ?

    Now I discover that QBuffer's aren't usable for my case, as they only "grow" and don't remove data when they are been read.

    Which data structure should I use as a buffer if I want the buffer removes the data that has been read ?

    Something like this:
    Last edited by oldFox64; 5th May 2014 at 16:09.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Hints to design threaded serial-reader/writer interface ?

    Doesn't the internal buffer of the QIODevice already do that?

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 22nd September 2014, 04:25
  2. Seeking Suggestions for Multi-Threaded Application Design
    By swamyonline in forum Qt Programming
    Replies: 7
    Last Post: 1st May 2014, 16:19
  3. Replies: 12
    Last Post: 19th July 2012, 21:22
  4. Qt GUI interface like Adobe Acrobat Reader
    By indianinside in forum Qt Programming
    Replies: 14
    Last Post: 28th June 2012, 18:32
  5. Problem with design interface
    By tux-world in forum Newbie
    Replies: 5
    Last Post: 10th March 2010, 14:19

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.