serial terminal messy code
-
wrote on 28 Nov 2016, 03:28 last edited by
QByteArray data; QDataStream out(&data,QIODevice::ReadWrite); out.setVersion(QDataStream::Qt_5_7); quint8 year = QDateTime::currentDateTime().toString("yyyy").toInt()-2000; quint8 month = QDateTime::currentDateTime().toString("MM").toInt(); quint8 day= QDateTime::currentDateTime().toString("dd").toInt(); quint8 hour= QDateTime::currentDateTime().toString("hh").toInt(); quint8 min= QDateTime::currentDateTime().toString("mm").toInt(); quint8 sec= QDateTime::currentDateTime().toString("ss").toInt(); out<<year<<month<<day<<hour<<min<<sec; _serial->write(data.toStdString().data()); // _serial->write(data);
this is my writeData fun
QByteArray data = _serial->readAll(); ui->textBrowserRec->append(QString(data));
this is my readData fun
i connect RX to TX,
but what i get is messy;
i am sure the serial is ok to work;
because
if i write data in this format ,i will get right data;QString tmp = "serial"; QByteArray ba = tmp.toUtf8(); const char * data = ba.data();
i want to know why i use QByteArray and QDataStream to send data is wrong. Or does it need a convert? thank your very much
-
QByteArray data; QDataStream out(&data,QIODevice::ReadWrite); out.setVersion(QDataStream::Qt_5_7); quint8 year = QDateTime::currentDateTime().toString("yyyy").toInt()-2000; quint8 month = QDateTime::currentDateTime().toString("MM").toInt(); quint8 day= QDateTime::currentDateTime().toString("dd").toInt(); quint8 hour= QDateTime::currentDateTime().toString("hh").toInt(); quint8 min= QDateTime::currentDateTime().toString("mm").toInt(); quint8 sec= QDateTime::currentDateTime().toString("ss").toInt(); out<<year<<month<<day<<hour<<min<<sec; _serial->write(data.toStdString().data()); // _serial->write(data);
this is my writeData fun
QByteArray data = _serial->readAll(); ui->textBrowserRec->append(QString(data));
this is my readData fun
i connect RX to TX,
but what i get is messy;
i am sure the serial is ok to work;
because
if i write data in this format ,i will get right data;QString tmp = "serial"; QByteArray ba = tmp.toUtf8(); const char * data = ba.data();
i want to know why i use QByteArray and QDataStream to send data is wrong. Or does it need a convert? thank your very much
@chaochao What do you mean by "but what i get is messy"? What do you get?
Also, where do you callQByteArray data = _serial->readAll(); ui->textBrowserRec->append(QString(data));
Calling readAll() does not mean that you get everything you sent before at once. You should use the http://doc.qt.io/qt-5/qiodevice.html#readyRead signal, and read from serial port in the slot connected to this signal.
-
wrote on 28 Nov 2016, 18:54 last edited by
A couple of thoughts...
You should probably use only one call to QDateTime::currentDateTime(). Although unlikely there is a possibility the date and time might change between the first and last call. You can create an instance of this class and use this to get the information you are looking for.
This seems unnecessary:
_serial->write(data.toStdString().data()); // why not _serial->write(data);
1/3