serial terminal messy code

  •   QByteArray data;
       QDataStream out(&data,QIODevice::ReadWrite);
       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();
      //  _serial->write(data);

    this is my writeData fun

      QByteArray data = _serial->readAll();

    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;
    if i write data in this format ,i will get right data;

    QString tmp = "serial";
     QByteArray ba  = tmp.toUtf8();
          const char * 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

  • Lifetime Qt Champion

    @chaochao What do you mean by "but what i get is messy"? What do you get?
    Also, where do you call

    QByteArray data = _serial->readAll();

    Calling readAll() does not mean that you get everything you sent before at once. You should use the signal, and read from serial port in the slot connected to this signal.

  • 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:

    // why not

Log in to reply