c++ - QIODevice::readAll() not working properly? -
i working on project involves serial communication between arduino , laptop. know arduino indeed sending data need, see picture: http://s1.postimg.org/w5wisaetr/help.png
now on other end laptop connected arduino , running program made using qt creator. however, when reading data serial port can't program display information.
i connected readdata() function executed when data received this:
connect(m_serialport, signal(readyread()), m_datagathering, slot(newdata()));
this works , newdata() function called whenever in transmitted arduino. function newdata() not display data need.
newdata():
void datagathering::newdata() { qbytearray rmsg = m_serial->readall(); qdebug() << rmsg.constdata(); }
this sends empty message display. this: http://s2.postimg.org/dkcyip2u1/empty.png
the following code works:
void datagathering::newdata() { qbytearray rmsg("\ntest...");// = m_serial->readall(); qdebug() << rmsg.constdata(); }
this code display message should. however, difference in output display when working code executed console displays lot of framing errors, assumed because baudrate of unwanted characters differs data need.
that why started questioning readall() function. obvious arduino not sending data need unwanted characters (see image in first link), don't see problem since filter out later.
all appreciated.
update: found out readall() function returning qbytearrays size() equals 0.
looks serial port qiodevice
not implement bytesavailable
, if returns 0. may why readall()
fails, depending on how implemented. @ least readall()
has problem of not being able report error.
try using read
method instead better diagnostics, (untested):
void datagathering::newdata() { qbytearray rmsg; for(;;) { char buf[256]; // read data in size chunks qint64 len = m_serial->read(buf, sizeof buf); if (len <= 0) { if (len < 0) { qdebug() << "newdata() read error" << m_serial->errorstring(); } break; // for(;;) } rmsg.append(buf, len); } qdebug() << "newdata() got byte array" << rmsg.size() << ":" << rmsg; }
it may not solve problem, luck give error message.
Comments
Post a Comment