Results 1 to 6 of 6

Thread: Using QIODevice to read a GPS filenode?

  1. #1
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Using QIODevice to read a GPS filenode?

    Hello people,

    i am trying to achieve programmatically an async read on a IO node file which receives the outputs of a NMEA GPS.

    i have tried to subclass QIODevice like this (For testing):
    Qt Code:
    1. #include "QIOGpsReader.h"
    2. #include <QIODevice>
    3.  
    4. QIOGpsReader::QIOGpsReader (QString node, QObject *parent) : QIODevice(parent) {
    5. qDebug("QIOGpsReader::ctor");
    6. }
    7.  
    8. QIOGpsReader::~QIOGpsReader (void) {
    9. qDebug("QIOGpsReader::~ctor");
    10. }
    11.  
    12. qint64 QIOGpsReader::readData (char *data, qint64 maxSize) {
    13. qDebug("QIOGpsReader::readData");
    14. strcpy(data, "1");
    15. return 1;
    16. }
    17.  
    18. qint64 QIOGpsReader::writeData (const char *data, qint64 maxSize) {
    19. qDebug("QIOGpsReader::writeData");
    20. return 0;
    21. }
    22.  
    23. qint64 QIOGpsReader::bytesAvailable (void) const {
    24. qDebug("QIOGpsReader::bytesAvailable");
    25. return 1;
    26. }
    To copy to clipboard, switch view to plain text mode 

    Then in my main routine i instanciate a QIOGpsReader object and just connect the readyRead signal to a slot. Then i do a open(QIODevice::ReadOnly) "just to do it".

    What i dont understand is, on what is based the "readReady" signal? When i look to my debug, i don't see any of my qDebug messages... What will make the QIODevice trigger a readyRead signal? i thought at a first that returning always 1 from readData and bytesAvailable would "tell" the QIODevice that it has to trigger, but it doesnt...

    Basically, what is the method i have to reimplement which will tell the QIODevice object to signal a readReady event?

    Thanks,
    Pierre.

    [EDIT:] i think it will not help but anyway here is my .h for my subclass:
    Qt Code:
    1. #ifndef QIOGpsReader_h
    2. #define QIOGpsReader_h
    3.  
    4. #include <QIODevice>
    5.  
    6. class QIOGpsReader : public QIODevice {
    7.  
    8. Q_OBJECT
    9.  
    10. public:
    11. QIOGpsReader (QString node, QObject *parent);
    12. ~QIOGpsReader (void);
    13.  
    14. protected:
    15. qint64 readData (char*, qint64);
    16. qint64 writeData (const char *data, qint64 maxSize);
    17. qint64 bytesAvailable (void) const;
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by hickscorp; 3rd December 2007 at 16:41.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QIODevice to read a GPS filenode?

    QIODevice subclass has to have a way to poll the underlying device for new data, for example using a timer. It might help you if you take a look at Qt sources to see how some devices are implemented (for example QTcpSocket or QProcess).

  3. #3
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QIODevice to read a GPS filenode?

    Thanks wysota,

    i think i may have another problem. What i am exactly trying to do is to read in a FIFO (A special file created using mknod p).
    My problem is that even when i am at the "end" of the stream for now, atEnd never returns 0, and my readLine loop never ends...

    What i want is to read each 1 second on this file. i have tried with a timer, the problem is that each timer tick is starting the routine reading in the file, and EACH routine is stuck on the readAll loop =/

    i have also tried with QSocketNotifier, but the simple fact of creating this object with a FOPEN handle makes the program stuck...

    Any idea?
    Pierre.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QIODevice to read a GPS filenode?

    Quote Originally Posted by hickscorp View Post
    What i want is to read each 1 second on this file. i have tried with a timer, the problem is that each timer tick is starting the routine reading in the file, and EACH routine is stuck on the readAll loop =/
    Bad idea. You need a select() call on the FIFO to see if there is anything to read or :pen() with O_ASYNC passed as one of the flags. You only read when there is actually anything to read. Then you store it in an internal buffer and emit readyRead() to your user. And always return true for atEnd() because you are creating a sequential device which is always at end.

    Here is an article to read: http://doc.trolltech.com/qq/qq12-iodevice.html

    It's based on Qt3 but everything applies to Qt4 as well. And I suggest you take a look at QTcpSocket source code (or to be more exact the qnativesocketengine_unix.cpp file). You can copy most of its code to your class as FIFO acts exactly the same a a tcp socket (in fact FIFO is a socket in the UNIX domain).

    You can even try using QTcpSocket directly just passing it your FIFO file descriptor.

  5. #5
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QIODevice to read a GPS filenode?

    Sorry wysota i don't understand your example with select().
    Do you mean, i have to go back to my implementation of QIODevice, and create an internal system for my QIOGpsReader which uses Async reads with select()?

    Also i have tried using QTcpSocket with various methods but it didnt work. I'm not sure why, and since getting debug messages on my handdled platform is tricky i wont tell

    Thanks for your time,
    Pierre.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QIODevice to read a GPS filenode?

    Quote Originally Posted by hickscorp View Post
    Sorry wysota i don't understand your example with select().
    Do you mean, i have to go back to my implementation of QIODevice, and create an internal system for my QIOGpsReader which uses Async reads with select()?
    Yes, otherwise you'll block your whole application sooner or later. Of course you don't have to use select, but I think it's the most straightforward approach.

    Also i have tried using QTcpSocket with various methods but it didnt work. I'm not sure why, and since getting debug messages on my handdled platform is tricky i wont tell
    What did you try? Something like:
    Qt Code:
    1. QTcpSocket socket;
    2. socket.setSocketDescriptor(myFifo);
    To copy to clipboard, switch view to plain text mode 
    should have worked.

Similar Threads

  1. How to read CD with read?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 29th June 2007, 08:20
  2. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29

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.