Solved Binary data in SerialPort
-
Hello everyone;
I communicate Qt with fpga, ı send 8 bit binary data but I read that data in ascıı format. How can I read directly binary format, or how can ı that ascıı value into again binary data?
Thanks. -
@yasinn
Are you getting binary value in ASCII format??
i.e., you are getting 10101010 when you read but as ASCII values.. -
Yes,
let me explain again, I send 10101010 data from fpga and when I read from Qt for ex;QByteArray data= serial->readAll();
QString s_data = QString(data);
ui->textEdit->setText(s_data);I saw ascii characters in text edit. for ex,
that image. -
sorry for adding but;
when I send 00100101 data I saw "%" as ex, or
when I send 00100110 data I saw "& " that symbol.How can I turn those symbol into binary
-
@yasinn said in Binary data in SerialPort:
sorry for adding but;
when I send 00100101 data I saw "%" as ex, or
when I send 00100110 data I saw "& " that symbol.How can I turn those symbol into binary
Hello, the data is already binary, but you are interpreting them as ASCII characters (when you append the chars to a text edit).
What do you actually want to do with the data you receive?
-
@aha_1980
firstly thanks for your reply, I send distance data in binary and I wanna read as decimal as result.
for ex if I send 00000111 data , I wanna read 7.you said that " the data is already binary, but you are interpreting them as ASCII characters" , yes I know but how can I convert ıt into decimal ?
-
@yasinn said in Binary data in SerialPort:
@aha_1980
firstly thanks for your reply, I send distance data in binary and I wanna read as decimal as result.
for ex if I send 00000111 data , I wanna read 7.you said that " the data is already binary, but you are interpreting them as ASCII characters" , yes I know but how can I convert ıt into decimal ?
There's no need to convert, the binary data 00000111 is already 7. The problem you might facing is, that each byte is stored as a
char
and usally these are used to encode text.Nevertheless, a
char
is anint
and you can do arithmetics with it and compare it, etc. -
This post is deleted! -
@aha_1980 said in Binary data in SerialPort:
@yasinn said in Binary data in SerialPort:
@aha_1980
firstly thanks for your reply, I send distance data in binary and I wanna read as decimal as result.
for ex if I send 00000111 data , I wanna read 7.you said that " the data is already binary, but you are interpreting them as ASCII characters" , yes I know but how can I convert ıt into decimal ?
There's no need to convert, the binary data 00000111 is already 7. The problem you might facing is, that each byte is stored as a
char
and usally these are used to encode text.Nevertheless, a
char
is anint
and you can do arithmetics with it and compare it, etc.thaks for advice, but ı dont wanna do arithmatics with data, I just want to saw it as deciamal. I work on a radar project, I cant say "your target is 01010111 meters away from you " :)
Should I compare 255 characters for it ?
-
@yasinn said in Binary data in SerialPort:
thaks for advice, but ı dont wanna do arithmatics with data, I just want to saw it as deciamal. I work on a radar project, I cant say "your target is 01010111 meters away from you " :)
I'm sorry, but it seems you should really learn basic C programming - you will stuck soon again otherwise.
Given, that each byte you receive from the serial port is a distance in meters, you could do something like:
const QByteArray data = serial.readAll(); for (int i = 0; i < data.size(); ++i) { QString text = QObject::tr("The target is %1 meters away").arg(int(data.at(i))); ui->textEdit->append(text); }
-
QByteArray data= serial->readAll(); QString s_data; for (char singleByte : data) s_data.append(ui->textEdit->locale().toString(ststic_cast<qint16>(singleByte))); //if it's unsigned change qint16 to quint16 ui->textEdit->setText(s_data);
-
@aha_1980 said in Binary data in SerialPort:
@yasinn said in Binary data in SerialPort:
thaks for advice, but ı dont wanna do arithmatics with data, I just want to saw it as deciamal. I work on a radar project, I cant say "your target is 01010111 meters away from you " :)
I'm sorry, but it seems you should really learn basic C programming - you will stuck soon again otherwise.
Given, that each byte you receive from the serial port is a distance in meters, you could do something like:
const QByteArray data = serial.readAll(); for (int i = 0; i < data.size(); ++i) { QString text = QObject::tr("The target is %1 meters away").arg(int(data.at(i))); ui->textEdit->append(text); }
Thank u so much. Sorry for my ignorance..
-
@yasinn said in Binary data in SerialPort:
for (int i = 0; i < data.size(); ++i) {
QString text = QObject::tr("The target is %1 meters away").arg(int(data.at(i)));
ui->textEdit->append(text);
}Thank u so much. Sorry for my ignorance..
Never mind! We all have started at the bottom... Good luck with your project.
-
Thinking about the problem again, casting to uchar seems better so you also get the distances between 128 and 255 meters.
const QByteArray data = serial.readAll(); for (int i = 0; i < data.size(); ++i) { QString text = QObject::tr("The target is %1 meters away").arg(uchar(data.at(i))); ui->textEdit->append(text); }