Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How we can read particular string/data from any device to print on particular LCD in qt?

How we can read particular string/data from any device to print on particular LCD in qt?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
qtcreatorqtwidgetsqt5.5qt4read serial
25 Posts 5 Posters 10.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mohit Tripathi

    @jsulm Yes, i am able to read the data serially. I am using the LCD Number widget into application means (.ui) file. LCDNumber widget displays a number with LCD-like digits.
    I am getting the data1 for LCD number1 and data2 for LCD number2 but I want to read the data2 for LCD number1 and data1 for LCD number2. The data is in string format.

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by J.Hilk
    #7

    @Mohit-Tripathi
    might be, that I missunderstand you,
    but you asign LCD 1 and 2 the savesame value

    Dialog::updatelcd1(buffer_split[1]);
    Dialog::updatelcd2(buffer_split[1]);
    

    those could never be different.


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    M 1 Reply Last reply
    1
    • J.HilkJ J.Hilk

      @Mohit-Tripathi
      might be, that I missunderstand you,
      but you asign LCD 1 and 2 the savesame value

      Dialog::updatelcd1(buffer_split[1]);
      Dialog::updatelcd2(buffer_split[1]);
      

      those could never be different.

      M Offline
      M Offline
      Mohit Tripathi
      wrote on last edited by
      #8

      @J.Hilk Yes, it is giving the same value on both LCD widget. How can I read different value for particular LCd widget?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #9

        Hi,

        If buffer split contains the two values you want to show, use the correct index e.g.:

        updatelcd1(buffer_split[0]);
        updatelcd2(buffer_split[1]);
        

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          If buffer split contains the two values you want to show, use the correct index e.g.:

          updatelcd1(buffer_split[0]);
          updatelcd2(buffer_split[1]);
          
          M Offline
          M Offline
          Mohit Tripathi
          wrote on last edited by Mohit Tripathi
          #10

          @SGaist I have changed the code but still I am getting the same problem.

          void Dialog::serialReceived()
          {
             int data1  =0;
              char *dataBuffer;
              int size = arduino->bytesAvailable();
              dataBuffer = new char[size+1];
              data1 = arduino->read(dataBuffer,size);
              dataBuffer[data1]='\0';
              if(data1 < 3){
                  serialData = arduino->readAll();
                  serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
                  serialData.clear();
              }else{
                  serialBuffer = "";
                  qDebug() << dataBuffer << "\n";
                  Dialog::updateTemperature(dataBuffer);
                  Dialog::updateTemperature(dataBuffer);
              }
          
          }
          
          void Dialog::updateTemperature(QString sensor_reading)
          {
             ui->temp_lcdNumber_2->display(sensor_reading);
             ui->temp_lcdNumber_3->display(sensor_reading);
          }
          
          

          I tried with two function.

           if(data1 < 3){
                  serialData = arduino->readAll();
                  serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
                  serialData.clear();
              }else{
                  serialBuffer = "";
                  qDebug() << dataBuffer << "\n";
                  Dialog::updateTemperature(dataBuffer);
                  Dialog::updateTemperature1(dataBuffer);
              }
          void Dialog::updateTemperature(QString sensor_reading)
          {
             ui->temp_lcdNumber_2->display(sensor_reading);
          }
          
          void Dialog::updateTemperature1(QString sensor_reading)
          {
             ui->temp_lcdNumber_3->display(sensor_reading);
          }
          

          I am getting the same value on both LCD widget. I am writing the two values and I am able to read the only last. It means that I see only last value on the LCD widget, not the first value. The both values are same on LCD widget.

          jsulmJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #11

            You still are passing the exact same input to all your widgets. It's not because one function is named updateTemperature and the other updateTemperature1 that they will parse your data to extract the correct value to show. That's your job.

            Note that you are using pretty convoluted code to handle your serial port data. Use QByteArray to cumulate the data. There's no need for all these conversions you are doing.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • M Mohit Tripathi

              @SGaist I have changed the code but still I am getting the same problem.

              void Dialog::serialReceived()
              {
                 int data1  =0;
                  char *dataBuffer;
                  int size = arduino->bytesAvailable();
                  dataBuffer = new char[size+1];
                  data1 = arduino->read(dataBuffer,size);
                  dataBuffer[data1]='\0';
                  if(data1 < 3){
                      serialData = arduino->readAll();
                      serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
                      serialData.clear();
                  }else{
                      serialBuffer = "";
                      qDebug() << dataBuffer << "\n";
                      Dialog::updateTemperature(dataBuffer);
                      Dialog::updateTemperature(dataBuffer);
                  }
              
              }
              
              void Dialog::updateTemperature(QString sensor_reading)
              {
                 ui->temp_lcdNumber_2->display(sensor_reading);
                 ui->temp_lcdNumber_3->display(sensor_reading);
              }
              
              

              I tried with two function.

               if(data1 < 3){
                      serialData = arduino->readAll();
                      serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
                      serialData.clear();
                  }else{
                      serialBuffer = "";
                      qDebug() << dataBuffer << "\n";
                      Dialog::updateTemperature(dataBuffer);
                      Dialog::updateTemperature1(dataBuffer);
                  }
              void Dialog::updateTemperature(QString sensor_reading)
              {
                 ui->temp_lcdNumber_2->display(sensor_reading);
              }
              
              void Dialog::updateTemperature1(QString sensor_reading)
              {
                 ui->temp_lcdNumber_3->display(sensor_reading);
              }
              

              I am getting the same value on both LCD widget. I am writing the two values and I am able to read the only last. It means that I see only last value on the LCD widget, not the first value. The both values are same on LCD widget.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #12

              @Mohit-Tripathi You send exact same data to both functions:

              Dialog::updateTemperature(dataBuffer);
              Dialog::updateTemperature1(dataBuffer);
              

              How is it supposed to show different values if you use same value for both?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #13

                @Mohit-Tripathi it looks like you are somehow double posting your issue, since this is the same problem as you mention here. Again, it seems you need to define a protocol so you the contents of dataBuffer can tell you both the value and the component to update.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mohit Tripathi
                  wrote on last edited by Mohit Tripathi
                  #14
                   void Dialog::serialReceived()
                      {
                         QStringList buffer_split;
                          if(buffer_split.length()< 3){
                              serialData = arduino->readAll();
                              serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
                              serialData.clear();
                          }else{
                              serialBuffer = "";
                              qDebug() << buffer_split << "\n";
                              Dialog::updateTemperature(dataBuffer[0]);
                              Dialog::updateTemperature1(dataBuffer[1]);
                          }
                  
                      }
                  
                      void Dialog::updateTemperature(QString sensor_reading)
                      {
                         ui->temp_lcdNumber_2->display(sensor_reading);
                      }
                  
                      void Dialog::updateTemperature1(QString sensor_reading1)
                      {
                          ui->temp_lcdNumber_3->display(sensor_reading1);
                      }
                  
                  
                      void Dialog::on_pushButton_clicked()
                      {
                          QMessageBox::information(this,"Motor Controller","Write data");
                          QString output1 = ui->lineEdit->text();
                          //qDebug()<<output1;
                          QString output2 = ui->lineEdit_2->text();
                          if(arduino->isWritable())
                          {
                               //arduino->write();
                             arduino->write(output1.toStdString().c_str());
                             arduino->write(output2.toStdString().c_str());
                               arduino->flush();
                               arduino->waitForBytesWritten(100);
                               qDebug()<<output1;
                              qDebug()<<output2;
                          }
                      }
                  

                  I am still getting the same problem.
                  I can write the data in arduino in form of string using push widget through Line Edit widget. I can read the data from arduino also. But, I can not print the data on LCD number widget. I am unable to print the particular string on particular LCD widget. I am getting the data1 for LCD number2 and data2 for LCD number1 but I want to read the data1 for LCD number1 and data1 for LCD number1 and sometime getting the data2 for both LCD widget.

                  Can I know why I am getting the wrong output for particular LCD widget?
                  0_1521927306240_6dlhB.png

                  1 Reply Last reply
                  -1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    Hi,

                    Because as already written in another thread you are not handling the data from your serial port correctly. You should cumulate the data you get until you know you have enough then parse it and update your lcd widget accordingly.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      Hi,

                      Because as already written in another thread you are not handling the data from your serial port correctly. You should cumulate the data you get until you know you have enough then parse it and update your lcd widget accordingly.

                      M Offline
                      M Offline
                      Mohit Tripathi
                      wrote on last edited by
                      #16

                      @SGaist @jsulm @J-Hilk
                      The ardunio code is

                      String 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.

                      J.HilkJ 1 Reply Last reply
                      0
                      • M Mohit Tripathi

                        @SGaist @jsulm @J-Hilk
                        The ardunio code is

                        String 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.

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #17

                        @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 use readLine 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";
                        }
                        

                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        M 1 Reply Last reply
                        1
                        • J.HilkJ J.Hilk

                          @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 use readLine 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";
                          }
                          
                          M Offline
                          M Offline
                          Mohit Tripathi
                          wrote on last edited by Mohit Tripathi
                          #18

                          @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.

                          0_1522146624326_app1.png

                          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);
                          }
                          

                          0_1522146831784_app2.png

                          I am getting the same problem always.

                          J.HilkJ 1 Reply Last reply
                          0
                          • M Mohit Tripathi

                            @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.

                            0_1522146624326_app1.png

                            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);
                            }
                            

                            0_1522146831784_app2.png

                            I am getting the same problem always.

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #19

                            @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


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            M 1 Reply Last reply
                            2
                            • J.HilkJ J.Hilk

                              @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

                              M Offline
                              M Offline
                              Mohit Tripathi
                              wrote on last edited by
                              #20

                              @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.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #21

                                @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 and temp_lcdNumber_3 with the same value each time you call that function.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                M 1 Reply Last reply
                                2
                                • SGaistS SGaist

                                  @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 and temp_lcdNumber_3 with the same value each time you call that function.

                                  M Offline
                                  M Offline
                                  Mohit Tripathi
                                  wrote on last edited by Mohit Tripathi
                                  #22

                                  @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.

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #23

                                    That's simple and what @J-Hilk showed in his example. Properly parse the data you receive and update each LCD separately.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    M 1 Reply Last reply
                                    2
                                    • SGaistS SGaist

                                      That's simple and what @J-Hilk showed in his example. Properly parse the data you receive and update each LCD separately.

                                      M Offline
                                      M Offline
                                      Mohit Tripathi
                                      wrote on last edited by Mohit Tripathi
                                      #24

                                      @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;
                                          }
                                      }
                                      
                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #25

                                        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.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        2

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved