Results 1 to 12 of 12

Thread: rfcomm0 read fails in Qt

  1. #1
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default rfcomm0 read fails in Qt

    Hello,

    I have managed to open and read /dev/rfcomm0 using a simple C app i worte, using fopen, fread and fwrite, on Ubuntu 10.04.
    now i want to do that in Qt.
    I managed to open the file (doesnt fail in first 2 checks), but returns 0 on number of bytes available for read. and if i skip bytesAvailable() and just read the device it gets stuck there infinitely. hints plz in opening reading device files using QT?
    here is the code. i tried that as well on N900 and same. Thanks in advance.
    Qt Code:
    1. #include <QApplication>
    2. #include <QFile>
    3. #include <QDebug>
    4. #include <QLabel>
    5. #include <QIODevice>
    6.  
    7. QIODevice *fd; //i tried QFile as well but no success
    8.  
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication app(argc, argv);
    13. QLabel hello("Hello world!");
    14. qint64 i;
    15. fd = new QFile("/dev/rfcomm0");
    16.  
    17. hello.resize(300, 100);
    18. hello.show();
    19.  
    20. if( !fd->open(QIODevice::ReadWrite) )
    21. {
    22. qDebug() << "Failed opening";
    23. return 0;
    24. }
    25.  
    26. if( !fd->isReadable() )
    27. {
    28. qDebug() << "Not Readable";
    29. return 0;
    30. }
    31.  
    32. qDebug() << "Please Wait";
    33. sleep(1);
    34.  
    35. i = fd->bytesAvailable();
    36. qDebug() << "i:" << i;
    37.  
    38. if(i == 0)
    39. {
    40. return 0;
    41. }
    42.  
    43. ba = fd->read(1);
    44. qDebug() << "DATA:" << ba;
    45.  
    46. fd->close();
    47.  
    48. return app.exec();
    49. }
    To copy to clipboard, switch view to plain text mode 

  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: rfcomm0 read fails in Qt

    Is there any data to read in rfcomm? You might use QSocketNotifier to check that out before you do any reading.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: rfcomm0 read fails in Qt

    Quote Originally Posted by wysota View Post
    Is there any data to read in rfcomm? You might use QSocketNotifier to check that out before you do any reading.
    yes,,,i have managed to read it using a test app in C using fopen and fread. any alternative QT API or initialization to do in before reading device file?
    thanx

  4. #4
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: rfcomm0 read fails in Qt

    the only way i found to get around is by using in my Qt example the C functions fopen, fread and fwrite.
    if you know how to open read a device file in Qt plz let me know.

    --------------------------------------
    http://howinuk.co.uk

  5. #5
    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: rfcomm0 read fails in Qt

    Did you try QSocketNotifier and QLocalSocket?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: rfcomm0 read fails in Qt

    Quote Originally Posted by wysota View Post
    Did you try QSocketNotifier and QLocalSocket?
    hello i tried QSocketNotifier as follows:
    Qt Code:
    1. fd = new QFile("/dev/rfcomm0");
    2. i = fd->open(QIODevice::ReadOnly);
    3. note = new QSocketNotifier(i, QSocketNotifier::Read,this);
    4. connect(note, SIGNAL(activated(int)), this, SLOT(sighndl()));
    To copy to clipboard, switch view to plain text mode 
    but no luck, regarding QLocalSocket i cant use it because "This class is not part of the Qt GUI Framework Edition."
    any further ideas?
    thanks

  7. #7
    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: rfcomm0 read fails in Qt

    Quote Originally Posted by raedbenz View Post
    hello i tried QSocketNotifier as follows:
    Qt Code:
    1. fd = new QFile("/dev/rfcomm0");
    2. i = fd->open(QIODevice::ReadOnly);
    3. note = new QSocketNotifier(i, QSocketNotifier::Read,this);
    4. connect(note, SIGNAL(activated(int)), this, SLOT(sighndl()));
    To copy to clipboard, switch view to plain text mode 
    but no luck
    QFile::open() returns a bool saying if the file was opened or not and you are passing it as a socket descriptor to QSocketNotifier. This hardly has a chance to work

    , regarding QLocalSocket i cant use it because "This class is not part of the Qt GUI Framework Edition."
    So why can't you use it exactly?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: rfcomm0 read fails in Qt

    Quote Originally Posted by wysota View Post
    QFile::open() returns a bool saying if the file was opened or not and you are passing it as a socket descriptor to QSocketNotifier. This hardly has a chance to work


    So why can't you use it exactly?
    hello, thanks for the reply.
    the reason i cant use QLocalSocket is the compiler gives an error:
    error: QLocalSocket: No such file or directory
    although #include <QLocalSocket> is added , any hints?

    one more thing, the first argument of QSocketNotifier is a socket identifier of type integer, so how can i pass the correct argument since socket function is not part of QFile or QIODevice?
    do i have to cast file descriptor ?
    Thanks

  9. #9
    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: rfcomm0 read fails in Qt

    Quote Originally Posted by raedbenz View Post
    hello, thanks for the reply.
    the reason i cant use QLocalSocket is the compiler gives an error:

    although #include <QLocalSocket> is added , any hints?
    Did you enable the QtNetwork module?

    one more thing, the first argument of QSocketNotifier is a socket identifier of type integer, so how can i pass the correct argument since socket function is not part of QFile or QIODevice?
    You have to get the file descriptor that you can pass there (i.e. one returned by ::open()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: rfcomm0 read fails in Qt

    Quote Originally Posted by wysota View Post
    Did you enable the QtNetwork module?


    You have to get the file descriptor that you can pass there (i.e. one returned by :pen()
    hello,
    how do i enable QtNetwork module? do i add #include <QtNetwork> and to project file 'QT += network' ?

    thanx

  11. #11
    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: rfcomm0 read fails in Qt

    Quote Originally Posted by raedbenz View Post
    do i add #include <QtNetwork> and to project file 'QT += network' ?
    Wouldn't it be faster to actually do that and see for yourself instead of writing this post here?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: rfcomm0 read fails in Qt

    Hello,
    i managed to read data using QSocketNotifier and it works fine, here is the code snippet:
    Qt Code:
    1. int socfd;
    2. socfd = open("/dev/rfcomm0", O_RDONLY|O_NONBLOCK);
    3. note = new QSocketNotifier(socfd, QSocketNotifier::Read, this);
    4. connect(note, SIGNAL(activated(int)), this, SLOT(readbytes()));
    To copy to clipboard, switch view to plain text mode 
    but still cant manage to open the device file using QLocalSocket.
    what i did is:
    Qt Code:
    1. QLocalSocket *Socket;
    2. Socket = new QLocalSocket(this);
    3. Socket->connectToServer("/dev/rfcomm0", QIODevice::ReadOnly);
    4. connect(Socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
    To copy to clipboard, switch view to plain text mode 
    the connections was not established at all. any hints?
    Thanks

Similar Threads

  1. ASSERT(visualRow!=-1 ) Fails !
    By fmariusd in forum Qt Programming
    Replies: 4
    Last Post: 15th May 2016, 00:00
  2. Compiling of Qt4.6 fails
    By Wonko in forum Installation and Deployment
    Replies: 5
    Last Post: 18th February 2010, 16:18
  3. Qt 4.6.1 Compilation fails!
    By Diegol in forum Installation and Deployment
    Replies: 1
    Last Post: 10th February 2010, 16:36
  4. qt 4.5.2: make fails
    By unknown in forum Installation and Deployment
    Replies: 13
    Last Post: 3rd September 2009, 16:53
  5. Compiling QSA 1.2.0 fails
    By detheang in forum Installation and Deployment
    Replies: 11
    Last Post: 4th February 2006, 20:09

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.