Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Not the copying the data in a file with same file size?



  • Hi,

    I am trying to store the data through UART from my processor. You can say that I am making the log file but the data is storing with more size more than expected. The data should be size of 500KB but it is storing more than 2MB size.
    The data is fine in file. There is no extra data in that file and there is also not corrupt data. I am storing the data with baudrate of 19200 in the file.

    QFile logFile("Data.txt");
        if (!logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
            qFatal("Can't open the log file: %s", logFile.errorString().toLocal8Bit().constData());
    QTextStream log(&logFile);
        logFile.setObjectName("Log File");
        log << serialBuffer;
        log.flush();
        logFile.close();
    

    here, serialBuffer is buffer to store the data.
    Can I know how this is possible and why this is happening?


  • Lifetime Qt Champion

    @mohit-tripathi said in Not the copying the data in a file with same file size?:

    QIODevice::Append

    You are appending to the file.
    Remove QIODevice::Append ...



  • @jsulm

    I am making the log file. The data come one by one through UART. If, we will not append the data will overwrite. It means that the new data will not add at end of the previous data.


  • Lifetime Qt Champion

    @mohit-tripathi Then I don't understand the problem. If you append to the file then it will grow and will not have fix size. So, why should it be exactly 500KB?


  • Moderators

    @mohit-tripathi you're appending the raw serialData, are you sure, you don't have some kind of protocol on top if it? Telegramsize, checksum etc.

    Have you compared the raw Data that you get? Just because your text browser doesn't display them, doesn't mean there are unwanted bytes in your file.


  • Lifetime Qt Champion

    @j-hilk Now I understand what the problem is :-)



  • @jsulm

    I am reading the SD card file through UART from SD card. The SD file is 500KB but the data which I am storing in file on my PC using Qt application. The data is same but the size is showing above 2 MB which is impossible.
    That's why I am not getting here. if I am reading the same file and storing in other file. the file size should be same.


  • Lifetime Qt Champion

    @mohit-tripathi After the comment from @J-Hilk I understood your problem. Take a look at his post.



  • @jsulm @J-Hilk
    Hi,

    As, I can see that the data is copying there or four times in my file.

    void SettingsDialog::on_pushButton_clicked()
    {
    //    const QStringList list = m_ui->serialPortInfoListBox->itemData(mohit).toStringList();
    //    qDebug()<<list;
    //    QList<QString> List;
    //    List = list.mid(0,1);
    //    qDebug()<<"COM:"<<List;
    
        foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){
        qDebug() << "Name  :" << info.portName();
        QString List1 = info.portName();
        qDebug() <<List1;
        serial->setPortName(List1);
    }
    
        serial->open(QSerialPort::ReadWrite);
        serial->setBaudRate(QSerialPort::Baud19200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        connect(serial,&QSerialPort::readyRead,this,&SettingsDialog::serialReceived);
    
        static const char mydata[] ={'M','N'};
        QByteArray sendPacket = QByteArray::fromRawData(mydata, sizeof(mydata));
       // QByteArray sendPacket("MN");
    //    serial->write(sendPacket);
        sendPacket.resize(2);
        if(serial->isWritable())
        {
    
            serial->write(sendPacket.toStdString().c_str(), sendPacket.size());
            serial->flush();
            serial->waitForBytesWritten(100);
        }
        else
        {
            qDebug()<<"write to serial";
        }
    
    }
    
    void SettingsDialog::serialReceived()
    {
        QFile logFile("Data.txt");
        if (!logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
            qFatal("Can't open the log file: %s", logFile.errorString().toLocal8Bit().constData());
         QTextStream log(&logFile);
    
        QStringList buffer_split = serialBuffer.split("++--++--++--++--++--++--++--++--++");
        if(buffer_split.length() < 3){
            serialData = serial->readAll();
            serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
            serialData.clear();
        }else{
            serialBuffer = "";
            qDebug() << buffer_split << "\n";
            //   Dialog::updatelcd1(buffer_split[0]);
            QMessageBox::about(this, "Data Copy Status", "SD Card Data Copied.\n\nPlease remove the device");
            qDebug()<<"No reading";
        }
    
      //  logFile.setObjectName("Log File");
        log << serialBuffer;
        log.flush();
        log.reset();
    
    }
    

    Can you please check?


  • Lifetime Qt Champion

    @mohit-tripathi said in Not the copying the data in a file with same file size?:

    serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());

    Why do you append to serialBuffer? After appending you write to the log, that means as long as buffer_split.length() < 3 you write same data into the log...


  • Moderators

    @mohit-tripathi

    what's your thought behind this:

    QStringList buffer_split = serialBuffer.split("++--++--++--++--++--++--++--++--++");
        if(buffer_split.length() < 3){
            serialData = serial->readAll();
            serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
            serialData.clear();
        }else{
            serialBuffer = "";
            qDebug() << buffer_split << "\n";
            //   Dialog::updatelcd1(buffer_split[0]);
            QMessageBox::about(this, "Data Copy Status", "SD Card Data Copied.\n\nPlease remove the device");
            qDebug()<<"No reading";
        }
    

    the else path will never be executed!
    therefore, your serialBuffer will never be cleared, but you append to it all the time



  • Thanks,

    @jsulm @J-Hilk
    I got the issue.
    I have more question. How can I store the data in my file as much as in less time. I need the data transmission through UART faster. The baudrate is only 115200. I am able to receive the data 4mb in 5 minutes. I think that this is very less.
    Is there any way to make data transmission faster using UART?


  • Lifetime Qt Champion

    @mohit-tripathi Serial port communication is slow as it really antique. If you can use higher frequency on bot sides (>115200) then you can increase the speed.
    See "Speed" in https://en.wikipedia.org/wiki/Serial_port.

    Also you can compress data before sending it and then decompress on receiver side.



  • @jsulm

    How can I define in Qt over 115200 baudrate?


  • Moderators

    @mohit-tripathi

    setBaudRate(qint32 baudRate, QSerialPort::Directions directions = AllDirections)

    any positive qint32 value will do > max = 2 147 483 647



  • This post is deleted!

Log in to reply