Single line receive data from Serial Port
-
Hi guys. Currently successfully being able to send data from PC to serial port. Also can send data from serial port to PC but when i send the data it goes in column. Eg. if i send 'hey', it goes as:
h
e
y.Can't i read this in one line? Need help.
The code for the receive part is:void MainWindow::serialReceived() { QByteArray serialData; serialData = serial->readLine(); QString data = QString( serialData ).remove("\r").remove("\n"); serialData.clear(); ui->receiver->append(data); }
PFA of my screen.
image url) -
@Shivam-Sharma Simply read the data into a buffer and when you got everything (do you have a protocol?) then append it to ui->receiver.
Also, what type is ui->receiver? -
@Shivam-Sharma said in Single line receive data from Serial Port:
QByteArray serialData;
serialData = serial->readLine();
QString data = QString( serialData ).remove("\r").remove("\n");
serialData.clear();
ui->receiver->append(data);Please try this:
if (serial->canReadLine()) { const QByteArray serialData = serial->readLine().trimmed(); ui->receiver->append(serialData); }
Calling
readLine()
withoutcanReadLine()
before may give unexpected results.Regards
-
Nope, not working.
-
@Shivam-Sharma What did not work? The code proposed by @aha_1980 will only show a line in you GUI if you hit "Enter" on your keyboard.
-
@Shivam-Sharma
As @KroMignon has just said. You saying "Nope, not working." doesn't help us much. Does not compile? Does not return anything? Does not return a whole line? Returns wrong characters? Whole program crashes?If
canReadLine()
returnstrue
thenreadLine()
should return the first line. Unless you're claiming the Qt methods don't work as documented. -
Also, i think I've figured out that if I copy a text 'hello' all together then it would transfer the data as it is but if on gtk+ i give the input one by one as hey, it prints as
h
e
y.I'm guessing this is some inbuilt issue.
-
@Shivam-Sharma said in Single line receive data from Serial Port:
I'm guessing this is some inbuilt issue.
Can you tell us which version of Qt you are using? Just be sure there no confusion, I mean Qt Kit not QtCreator.
-
@Shivam-Sharma
If (and only if) you are using thecanReadline()
code and you are sayingreadLine()
then returns just one letter (or indeed anything) when you type one letter on the device but do not hit "Enter", or if you type multiple letters and hit "Enter" at the end but it returns just one letter, then something strange is going on.... -
@KroMignon Desktop Qt 5.12.6 GCC 64Bit
-
but if on gtk+ i give the input one by one as
you were told in your other post related to the subject (BTW you may want to close it, since you created this new post already) to go and use some other terminal program (i.e. minicom, moserial, etc.) to test your Qt app.
In any case, what happens if you connect your "gtk+" to some other terminal program?
the data you receive is a character per line as you see in your Qt app? Checking that way you can identify how that "gtk+" app is behaving...i guess this problem should be solved when the PC would be getting data from any sensor, no?
Well, that's not a good approach to assure the quality of your code...
What if the sensor behaves the same of your "gtk+" program?