I'm working on a 'RepRap calibration tool' which would use a mouse attached to the printing platform to measure the movement of the platform.
Right now I'm stuck trying to read raw mouse data from /dev/hidrawX
, but I'm unable to read any data.
So far I've tried:
QFile f("/dev/hidraw0");
f.readAll();
Reads nothing.
m_file = new QFile("/dev/hidraw0");
m_sn= new QSocketNotifier(m_file->handle(), QSocketNotifier::Read);
m_sn->setEnabled(true);
connect(m_sn, SIGNAL(activated(int)), this, SLOT(readyRead()));
then on the readyRead SLOT:
qDebug()<<"Ready Read!!"<<m_file.bytesAvailable();
QTextStream d(&m_file);
qDebug()<< d.read(64);
This code fires the readyRead slot once but it gets stuck on the read(64) call, if I comment the read(64) the slot will be fired each time the mouse its moved.
m_file.bytesAvailable()
always reports 0.
Which is the right way to read these devices with Qt?
I reworked the code like:
bool rcMouseHandler::openHidraw(QString device)
{
int fd =open(device.toLocal8Bit(),O_NONBLOCK);
if(fd <=0)
{
qDebug()<<"[WARN]rcMouseHandler::open-> Cant open!";
return false;
}
m_sn= new QSocketNotifier(fd, QSocketNotifier::Read);
m_sn->setEnabled(true);
connect(m_sn, SIGNAL(activated(int)), this, SLOT(readyRead()));
return true;
}
void rcMouseHandler::readyRead()
{
qDebug()<<"Ready Read!!";
char buffer[4] = {0,0,0,0};
read(m_sn->socket(),&buffer,4);
qDebug()<<(quint8)buffer[0]<<(quint8)buffer[1]<<(quint8)buffer[2]<<(quint8)buffer[3];
}
The right way I suppose here to not use Qt. Why you need portable wrapper above POSIX open and read, when this part of your code is not portable (part that work with /dev/*). Open device with "open" "man 2 open" in O_NONBLOCK and call "read" (man 2 read) to get data from it. And you can still use QSocketNotifier with handle that return "open".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With