How we can read particular string/data from any device to print on particular LCD in qt?
-
@SGaist @jsulm @J-Hilk
The ardunio code isString a[10]; void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { if(Serial.available()>0) { for(int i=0;i<2;i++) { a[i] = Serial.readString(); } for(int i=0;i<2;i++) { Serial.println(a[i]); } Serial.flush(); } }
I am getting the two strings while entering two string in arduino like "12" and "1" on ardunio IDE but why i am not getting the correct result on LCD widget in qt5 like "12" on LCD widget 1 and "1" on LCD widget 2. Please note that I entered the "12" in lineEdit and "1" lineEdit_2.
-
@Mohit-Tripathi with the assumption, that all data arrives simultaneously at your QSerialPort you could try the following.
According to the ardunino docs,
println
appends with \r\n so you can usereadLine
on Qt's side:void Dialog::serialReceived() { arduino->waitForReadyRead(100); if(arduino->canReadLine()){ QStringList myData; while(arduino->canReadLine()){ char buf[1024]; qint64 lineLength = file.readLine(buf, sizeof(buf)); if (lineLength != -1) { // the line is available in buf myData.append(QString(buf)); }else qDebug() << "Reading Line failed"; } if(myData.size() >0) updatelcd1(myData.at(0); if(myData.size() >1) updatelcd2(myData.at(1)); }else qDebug() << "Could not read lines"; }
-
@J.Hilk If I am using this code (according to your solution)
if(myData.size() >0) { updateLCD(myData.at(0)); } else if(myData.size() >1) { updateLCD1(myData.at(1)); }
I am still getting the same problem in LCD Widget. I am getting the both input on only one LCD widget.
If I am using the same function call for both. I am getting the same output on both LCD widget.
if(myData.size() >0) { updateLCD(myData.at(0)); } else if(myData.size() >1) { updateLCD(myData.at(1)); } void Dialog::updateLCD(QString sensor_reading) { ui->temp_lcdNumber_2->display(sensor_reading); ui->temp_lcdNumber_3->display(sensor_reading); }
I am getting the same problem always.
-
@Mohit-Tripathi said in How we can read particular string/data from any device to print on particular LCD in qt?:
@J.Hilk If I am using this code (according to your solution)
if(myData.size() >0) { updateLCD(myData.at(0)); } else if(myData.size() >1) { updateLCD1(myData.at(1)); }
are you kidding me?
how is that code supposed to be executed? Im sure even the compiler will rationalize it away.if myData is bigger than 0 updateLCD will be called and the else will never execute
if myData is noit bigger than 0 it can never bigger than 1
Theres a reason I omitted the else
-
@J.Hilk I am not kidding. I am getting the same problem when I started to do code. I am at beginner level in Qt. That's why I am facing the problem.
But, here if Data size is less than 0. It should print only one enter value, not both value on one LCD widget. But, It is printing the both value on one LCD widget. -
@Mohit-Tripathi said in How we can read particular string/data from any device to print on particular LCD in qt?:
void Dialog::updateLCD(QString sensor_reading)
{
ui->temp_lcdNumber_2->display(sensor_reading);
ui->temp_lcdNumber_3->display(sensor_reading);
}As already explained several times: you are updating both
temp_lcdNumber_2
andtemp_lcdNumber_3
with the same value each time you call that function. -
@SGaist
As per discussion above, I have defined that if I make two functions, I get both write values on one LCD. But, if I define one function :void Dialog::updateLCD(QString sensor_reading) { ui->temp_lcdNumber_2->display(sensor_reading); ui->temp_lcdNumber_3->display(sensor_reading); }
I get the same value on both LCD.
But, my question was "how can I get the one input in one only LCD if we have multiple LCD widget?". I have mentioned in my comment as well as question.
I have used two different functions, same functions and two arrays also. Please go through my recent code in above comment. -
@SGaist @jsulm @J-Hilk @Global-Moderators @Moderators @J.Hilk
Is this a right way to write the data of two textEdit using pushbutton. I think that I am storing both values in one string that's why I am getting the both values on one LCD widget.void Dialog::on_pushButton_clicked() { QString output1 = ui->lineEdit->text(); // qDebug()<<output1; QString output2 = ui->lineEdit_2->text(); // qDebug()<<output2; if(arduino->isWritable()) { arduino->write(output1.toStdString().c_str()); arduino->flush(); arduino->waitForBytesWritten(500); arduino->write(output2.toStdString().c_str()); arduino->flush(); arduino->waitForBytesWritten(500); qDebug()<<output1; qDebug()<<output2; } }
-
Don't @ the moderators unless you have an issue requiring moderation.
Depending on the content of your line edit, you can use toLatin1() or toUtf8() rather than making these two conversions.