PDA

View Full Version : QDataStream Inserting Spaces Between Characters



awpitt13
15th September 2013, 03:45
Hello all,

I have an application that is receiving data over serial using QtSerialPort that is displaying data over a terminal (much like the examples in QtSerialPort if you are familiar with it). I also want to be able to save this data into a .txt file for further analysis. My problem is that I'm using QDataStream and it seems to be I am inserting extra characters somehow.

For example, data coming over the terminal, and how it's supposed to
1, 3, 2500000, 100000, 23000000

but when I'm saving to a .txt file the data is saved as:
> 1 , 3 , 2 5 0 0 0 0 0 , 1 0 0 0 0 0 , 2 3 0 0 0 0 0 0

And yes, there's an extra ">" at the beginning


Soooo here's my code. Here is to setup the save file:



void MainWindow::on_pushButton_clicked()
{
//Savefile setup
if(ui->lineEdit_savefile->text().isEmpty()) on_pushButton_Browse_clicked(); //Check to make sure the user entered a file
QString fileName = ui->lineEdit_savefile->text();
if(!fileName.endsWith('.txt')){
fileName.append(".txt");
ui->lineEdit_savefile->setText(ui->lineEdit_savefile->text().append('.txt'));
}
saveFile.setFileName(fileName);
if(!saveFile.open(QIODevice::WriteOnly | QIODevice::Text)){
QMessageBox::information(this, tr("Unable to open file"),
saveFile.errorString());
return;
}
}


Here is how I write to the file:


void MainWindow::saveData(const QByteArray &data){
QDataStream out(&saveFile);
out.setVersion(QDataStream::Qt_4_5);
out << QString(data);
}



And this is how the console application handles the data and prints it to the screen. It works correctly.


void Console::putData(const QByteArray &data)
{
insertPlainText(QString(data));

QScrollBar *bar = verticalScrollBar();
bar->setValue(bar->maximum());
}


How do I get rid of these extra characters? Thank you very much in advance for your input(s)!

ChrisW67
15th September 2013, 04:19
You are asking QDataStream to serialise a QString full of Unicode characters into a binary format that it can restore later: it writes a four byte string length followed by that many two byte characters. QDataStream is not inserting extra anything, just enough to fulfil its purpose.

If you want to write raw bytes to a file use the QIODevice::write() interface directly with your received QByteArray.

anda_skoa
15th September 2013, 11:42
And if you want to write into a text file use QTextStream.

Cheers,
_

awpitt13
21st September 2013, 03:23
Thanks, Chris... I was only able to get to this and try this today. I assumed to get this to work I had to use

saveFile.write(data);

This gets rid of the spaces in the text file but leaves a really good chunk of data out. I know I'm missing data since my terminal window that uses the same data array prints out all the data correctly.

To anda_skoa, thank you for your reply, I'll try this and let you know how it goes.

ChrisW67
21st September 2013, 08:11
The write() functions takes all the bytes in the array and puts them in the file (or whatever the QIODevice is). It does not interpret the bytes, trim the bytes, skip bytes or encode bytes. If the file does not actually contain the bytes then they were not in the array to start with (or there was insufficient space to write the file etc.). If you are writing bytes that might contain non-printable characters and subsequently inspect that with something expecting pure text then it may seem that things are missing when they are not. Exactly which way your result is "broken" for you we cannot tell.