Help with conversions of datatype.
-
Hello, i'm trying to write a software which receive serial data from the USB port from an Arduino, then plot it using the custom plotwidget, i'm having issue to convert data.
This is the code which is giving me issues
QString datiXok; QString datiYok; QString datiZok; QByteArray datiX; QByteArray datiY; QByteArray datiZ; double doubleX; double doubleY; double doubleZ;
Those are declared globally.
datiX = serial->readAll(); datiXok = QString(datiX); doubleX = atof (datiX); datiY = serial->readAll(); datiYok = QString(datiY); doubleY = atof (datiY); datiZ = serial->readAll(); datiZok = QString(datiZ); doubleZ = atof (datiZ);
datiX gets converted without issues, Y and Z doesn't
if i invert the code and convert first Y or Z before X, the first conversion works the rest doesn't
can anyone help me?
I know this is not the best way to convert, but i tried other Qt methods to 0 avail, this way at least 1 out of 3 gets converted, but in total i need to convert 4, either Qbytearray to double or float, or Qstring to double or float.[edit: koahnig, code tags added]
-
Hello, i'm trying to write a software which receive serial data from the USB port from an Arduino, then plot it using the custom plotwidget, i'm having issue to convert data.
This is the code which is giving me issues
QString datiXok; QString datiYok; QString datiZok; QByteArray datiX; QByteArray datiY; QByteArray datiZ; double doubleX; double doubleY; double doubleZ;
Those are declared globally.
datiX = serial->readAll(); datiXok = QString(datiX); doubleX = atof (datiX); datiY = serial->readAll(); datiYok = QString(datiY); doubleY = atof (datiY); datiZ = serial->readAll(); datiZok = QString(datiZ); doubleZ = atof (datiZ);
datiX gets converted without issues, Y and Z doesn't
if i invert the code and convert first Y or Z before X, the first conversion works the rest doesn't
can anyone help me?
I know this is not the best way to convert, but i tried other Qt methods to 0 avail, this way at least 1 out of 3 gets converted, but in total i need to convert 4, either Qbytearray to double or float, or Qstring to double or float.[edit: koahnig, code tags added]
Hi and welcome to devnet
readAll reads all bytes currently in the buffer. This may one byte or more depending in which stage your buffer is at the time when the routine is called. It does not know what it is reading or what you think it should read.
You need to chekc out what you are actually reading. E.g. with
datiX = serial->readAll(); qDebug() << datiX; // requires to add #include <QDebug> at the start of the source file datiXok = QString(datiX); doubleX = atof (datiX);
Possibly you are reading already all bytes at the beginning. Anyway, you need to think about how you like to handle and eventually collect the information, because it is unpredictable how much you are going receive each time.
There are some examples for reading from QSerialPort around. They will help you to get an understanding of proper handling of serial ports with Qt.
-
Hi and welcome to devnet
readAll reads all bytes currently in the buffer. This may one byte or more depending in which stage your buffer is at the time when the routine is called. It does not know what it is reading or what you think it should read.
You need to chekc out what you are actually reading. E.g. with
datiX = serial->readAll(); qDebug() << datiX; // requires to add #include <QDebug> at the start of the source file datiXok = QString(datiX); doubleX = atof (datiX);
Possibly you are reading already all bytes at the beginning. Anyway, you need to think about how you like to handle and eventually collect the information, because it is unpredictable how much you are going receive each time.
There are some examples for reading from QSerialPort around. They will help you to get an understanding of proper handling of serial ports with Qt.
@koahnig thanks for the reply.
I did check it before writing the post, the data is read correctly, just not using qDebug to visualize it, i used the text label and the lcd, the data get read correctly, is only the conversion which is always = to 0, apart from the first one. -
I did some more testing, now i get why date was being shown correctly, the data it's ending with \r\n, that made it so it would go down on the UI, and coincidence just as i planned the layout for each value.
How can i fix it without manipulate the string itself?
Is there a way to read data to that precise point \r, or i'm forced to manuallyl manipulate the whole string? -
I did some more testing, now i get why date was being shown correctly, the data it's ending with \r\n, that made it so it would go down on the UI, and coincidence just as i planned the layout for each value.
How can i fix it without manipulate the string itself?
Is there a way to read data to that precise point \r, or i'm forced to manuallyl manipulate the whole string?You could read the entire string into a variable and then use QString::split.
(untested example code follows)QString recvdString = QString(serial->readAll()); QStringList splitStrings = recvdString.split(QString("\r\n")); datiXok = splitStrings.at(0); doubleX = dataiXok.toDouble(); ...
-
Thanks a lot guys, i was able to fix it thanks to your help.
This is the new one
QString recvdString = QString(serial->readAll());
QStringList splitStrings = recvdString.split(QString("\r\n"));datiXok = splitStrings.at(0);
datiYok = splitStrings.at(1);
datiZok = splitStrings.at(2);doubleX = datiXok.toDouble();
doubleY = datiYok.toDouble();
doubleZ = datiZok.toDouble(); -
Thanks a lot guys, i was able to fix it thanks to your help.
This is the new one
QString recvdString = QString(serial->readAll());
QStringList splitStrings = recvdString.split(QString("\r\n"));datiXok = splitStrings.at(0);
datiYok = splitStrings.at(1);
datiZok = splitStrings.at(2);doubleX = datiXok.toDouble();
doubleY = datiYok.toDouble();
doubleZ = datiZok.toDouble();Good to see that you have found a solution for your problem. Certainly the solution suggested by @mchinand seems to be the perfect solution since your parameters are separated by <CR><LF> characters. A similar solution could have been achieved by using readLine instead of readAll.
Anyway both solutions are somewhat dirty in the sense already mentioned above.
@koahnig said in Help with conversions of datatype.:Possibly you are reading already all bytes at the beginning. Anyway, you need to think about how you like to handle and eventually collect the information, because it is unpredictable how much you are going receive each time.
There are some examples for reading from QSerialPort around. They will help you to get an understanding of proper handling of serial ports with Qt.
Especially when using the readyRead signal for all real-time based communications such serial, TCP, UDP or whatever you can never rely that the complete information is already in the buffer. The buffer may contain one byte to all bytes. All depends on different aspects such as CPU speed, other tasks and speed of communication link. You might even have too much data in the buffer and throwing away some of the information when using readAll. The use of readLine is therefore somewhat less dirty.