Reading from SerialPort and convert it to QString?!
-
Hi,
No. I already add the plaintextEditor on the form. and below code:ui->plainTextEdit->insertPlainText(QString(ba));
I can see the results in the editor. No problem!... :(
Like below;A 720
A 720
A 720Regards,
Mucip:) -
Hi,
I checked.
When I highlight and copy from PlainTextEditor I can paste it on the TextEditor of windows.
But when I highlight and copy it from LineEdit I can not paste it on the TextEditor of windows.I think between 2th and 7th bits contain NULL when the value less then 6 digits like "200".
So what happend if the ByteArray has NULL and why PlainTextEditor shows the value (like A 720) well and Why LineEdit NOT?!...
Regards,
Mucip:) -
@J.Hilk said in Reading from SerialPort and convert it to QString?!:
for(int i(0); i < ba.size();i++)
qDebug() << i << QChar(ba[i]) << static_cast<int>(ba[i]);Hi,
I used it and result:
0 'A' 65
0 ' ' 32
0 ' ' 32
1 '1' 49
0 '4' 52
1 '5' 53
2 '0' 48
0 '\xd' 13And Idicator shows 1450... This is correct! :)
But How can I get the complate output like "A1450"???Regards,
Mucip:) -
@J.Hilk said in Reading from SerialPort and convert it to QString?!:
@Mucip Well I just wanted to see, what is actually in that QByteArray
You could use this function to "see" the content of your data:
for(int i(0); i < ba.size();i++)
qDebug() << i << QChar(ba[i]) << static_cast<int>(ba[i]);Easier would be to just convert the byte array to hex:
qDebug() << ba.toHex();
-
@J.Hilk said in Reading from SerialPort and convert it to QString?!:
for(int i(0); i < ba.size();i++)
qDebug() << i << QChar(ba[i]) << static_cast<int>(ba[i]);Hi,
I used it and result:
0 'A' 65
0 ' ' 32
0 ' ' 32
1 '1' 49
0 '4' 52
1 '5' 53
2 '0' 48
0 '\xd' 13And Idicator shows 1450... This is correct! :)
But How can I get the complate output like "A1450"???Regards,
Mucip:)@Mucip said in Reading from SerialPort and convert it to QString?!:
0 '\xd' 13
I believe thats the culprit a carriage Return ist difficult to display in a QLabel and/or QLineEdit
Also, seeing your index starting from 0 multiple times the text of the QLabel and QLineEdit are constantly overwritten. You'll need to construct a String out of your different received QByteArrays.
I suggest something like this. If it always end with QChar(0x0D)//Member String QString str; void MainWindow::serialReceived() { QByteArray ba; bool finished(false); ba= serial->readAll(); for(int i(0); i < ba.size();i++){ if(ba[i] != 0x0D) str.append(ba[i]); else finished = true; } if(finished){ ui->label_2->setText(str); ui->plainTextEdit->insertPlainText(str); str.clear(); } }
-
can you try:
void MainWindow::serialReceived() { QTextStream stream(serial); const QString receivedStr = stream.readAll(); ui->label_2->setText(receivedStr ); ui->plainTextEdit->insertPlainText(receivedStr ); }
Of course there is nothing here assuring you all data was received when the slot gets called
-
Hi,
No. Unfotunatelly same thing. I can see the data in plainTextEdit but not in lineEdit.Regards,
Mucip:) -
@Mucip said in Reading from SerialPort and convert it to QString?!:
0 '\xd' 13
I believe thats the culprit a carriage Return ist difficult to display in a QLabel and/or QLineEdit
Also, seeing your index starting from 0 multiple times the text of the QLabel and QLineEdit are constantly overwritten. You'll need to construct a String out of your different received QByteArrays.
I suggest something like this. If it always end with QChar(0x0D)//Member String QString str; void MainWindow::serialReceived() { QByteArray ba; bool finished(false); ba= serial->readAll(); for(int i(0); i < ba.size();i++){ if(ba[i] != 0x0D) str.append(ba[i]); else finished = true; } if(finished){ ui->label_2->setText(str); ui->plainTextEdit->insertPlainText(str); str.clear(); } }
@J.Hilk said in Reading from SerialPort and convert it to QString?!:
void MainWindow::serialReceived()
{
QByteArray ba;
bool finished(false);
ba= serial->readAll();
for(int i(0); i < ba.size();i++){
if(ba[i] != 0x0D)
str.append(ba[i]);
else finished = true;
}Hi,
I solved the problem above like solution...
I checked the data one by one and add to QString.
Thanks.