PDA

View Full Version : rfcomm0 read fails in Qt



raedbenz
17th June 2010, 22:37
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.

#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QLabel>
#include <QIODevice>

QIODevice *fd; //i tried QFile as well but no success


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel hello("Hello world!");
qint64 i;
QByteArray ba;
fd = new QFile("/dev/rfcomm0");

hello.resize(300, 100);
hello.show();

if( !fd->open(QIODevice::ReadWrite) )
{
qDebug() << "Failed opening";
return 0;
}

if( !fd->isReadable() )
{
qDebug() << "Not Readable";
return 0;
}

qDebug() << "Please Wait";
sleep(1);

i = fd->bytesAvailable();
qDebug() << "i:" << i;

if(i == 0)
{
return 0;
}

ba = fd->read(1);
qDebug() << "DATA:" << ba;

fd->close();

return app.exec();
}

wysota
18th June 2010, 01:17
Is there any data to read in rfcomm? You might use QSocketNotifier to check that out before you do any reading.

raedbenz
19th June 2010, 12:07
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

raedbenz
6th July 2010, 13:51
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

wysota
8th July 2010, 14:14
Did you try QSocketNotifier and QLocalSocket?

raedbenz
10th July 2010, 00:52
Did you try QSocketNotifier and QLocalSocket?
hello i tried QSocketNotifier as follows:


fd = new QFile("/dev/rfcomm0");
i = fd->open(QIODevice::ReadOnly);
note = new QSocketNotifier(i, QSocketNotifier::Read,this);
connect(note, SIGNAL(activated(int)), this, SLOT(sighndl()));
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

wysota
10th July 2010, 09:25
hello i tried QSocketNotifier as follows:


fd = new QFile("/dev/rfcomm0");
i = fd->open(QIODevice::ReadOnly);
note = new QSocketNotifier(i, QSocketNotifier::Read,this);
connect(note, SIGNAL(activated(int)), this, SLOT(sighndl()));
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?

raedbenz
10th July 2010, 16:01
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

wysota
10th July 2010, 16:11
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()

raedbenz
10th July 2010, 17:41
Did you enable the QtNetwork module?


You have to get the file descriptor that you can pass there (i.e. one returned by ::open()

hello,
how do i enable QtNetwork module? do i add #include <QtNetwork> and to project file 'QT += network' ?

thanx

wysota
10th July 2010, 18:35
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?

raedbenz
16th July 2010, 23:12
Hello,
i managed to read data using QSocketNotifier and it works fine, here is the code snippet:


QSocketNotifier *note;
int socfd;
socfd = open("/dev/rfcomm0", O_RDONLY|O_NONBLOCK);
note = new QSocketNotifier(socfd, QSocketNotifier::Read, this);
connect(note, SIGNAL(activated(int)), this, SLOT(readbytes()));

but still cant manage to open the device file using QLocalSocket.
what i did is:


QLocalSocket *Socket;
Socket = new QLocalSocket(this);
Socket->connectToServer("/dev/rfcomm0", QIODevice::ReadOnly);
connect(Socket, SIGNAL(readyRead()), this, SLOT(readSocket()));

the connections was not established at all. any hints?
Thanks