Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. serial terminal messy code
Forum Update on Monday, May 27th 2025

serial terminal messy code

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 715 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chaochao
    wrote on 28 Nov 2016, 03:28 last edited by
    #1
      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

    J 1 Reply Last reply 28 Nov 2016, 05:31
    0
    • C chaochao
      28 Nov 2016, 03:28
        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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Nov 2016, 05:31 last edited by
      #2

      @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();
      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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • R Offline
        R Offline
        Rondog
        wrote on 28 Nov 2016, 18:54 last edited by
        #3

        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 Reply Last reply
        1

        1/3

        28 Nov 2016, 03:28

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved