Read byte by byte from serial
-
hello everyone, i'm newbie in qt. i have 5 byte data from serial device and i wanna display the data byte by byte, anyone have sample code? i'm really really newbie in qt.. thanks before
-
Hi,
Have a look at the "QSerialPort documentation":http://qt-project.org/doc/qt-5.1/qtserialport/qserialport.html
-
thank You JKSH for your reply.. but i use qextserialport in qt, my data in hex, 5 byte and i want to display data by byte, for example: lable1.setText(byte1), lable2.setText(byte2) and others. now i only can diplay all data in one lable and in string. my device send "0x55" but in qt i get "U". how to get 0x55 in qt?
i'm sorry if my english is poor
-
Hi,
U is the char corresponding to 0x55. If you want 0x55 literally you can use QByteArray::toHex()
-
i try to use QByteArray::toHex() bute still doesnt work, and other case ia want to display one byte data to one label, in visual C i can use label.text = serialport.ReadByte().. how to use it in qt?
thank you
-
Hi,
Usually you need to describe what you mean by "it doesn't work". Otherwise, people won't know what your problem is.
@
QByteArray ba = serialPort.readAll(); // Get the 0x55 bytelabel.setText(ba.toHex()); // Label shows "55"
label.setText("0x" + ba.toHex()); // Label shows "0x55"
@ -
thank You JKSH, i successfully to display data in 55.. but i have other problems, my data from arduino is 5 byte like this: Serial.write(0x55,0x50,0x17,0x16,0x150);
how to display one byte in one label? -
You're welcome. :)
[quote]0×55,0×50,0×17,0×16,0×150[/quote]Is that a typo? 0x150 == 0b 0000 0001 0101 0000, which occupies 2 bytes. I'll assume your last byte is 0x15.
Anyway, when you convert your 5-byte array to hex, you will have a 10-character string, "5550171615". You'll then have to extract two characters at a time.
See "QByteArray::mid()":http://qt-project.org/doc/qt-5.1/qtcore/qbytearray.html#mid or "QString::mid()":http://qt-project.org/doc/qt-5.1/qtcore/qstring.html#mid
-
thank you guys for your reply.. i sned the data repeatedly from arduino without delay so when i use this code
@void MainWindow::serialRecived(){
QByteArray ba(port->readAll());
QString s = ba.toHex();
ui->label->setText(s);
}@the label display all byte repeatedly. i want to display each byte in one label so i can monitor any change in each byte.. can you give me an idea? thanks a lot before guys
-
Like I said, see "QByteArray::mid()":http://qt-project.org/doc/qt-5.1/qtcore/qbytearray.html#mid or "QString::mid()":http://qt-project.org/doc/qt-5.1/qtcore/qstring.html#mid