Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Serial port Communication with pc through serial Adapter
Forum Updated to NodeBB v4.3 + New Features

Serial port Communication with pc through serial Adapter

Scheduled Pinned Locked Moved General and Desktop
45 Posts 6 Posters 6.2k Views 2 Watching
  • 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.
  • artwawA artwaw

    From the top of my head, I'd serialise communication via QByteArray, so the workflow would be: QString > QByteArray -----> QByteArray > QString.

    Further on: I am not sure if I understand exactly what your workflow should be, so please correct me if I'm wrong.
    What I understand is, that you send line1 and wait for ack? I am not sure if your implementation is valid then, considering asynchronous nature of QSerialPort.

    Also, is there a reason you convert QString to plain text?

    sankarapandiyanS Offline
    sankarapandiyanS Offline
    sankarapandiyan
    wrote on last edited by
    #4

    @artwaw said in Serial port Communication with pc through serial Adapter:

    What I understand is, that you send line1 and wait for ack?

    Yes you are cent percent right ... Is there is any possibility to get a ack from the PC by the code...

    1 Reply Last reply
    0
    • KroMignonK KroMignon

      @sankarapandiyan said in Serial port Communication with pc through serial Adapter:

      serial->write(Data);

      If you use QSerialPort::write(const char *data), write will stop on first 0 byte according to documentation => https://doc.qt.io/qt-5/qiodevice.html#write-1

      You don't need to convert to plaint text, for binary data it is easier to use QByteArray

      QTextStream stream(&inputFile);
          line = stream.readLine();
          while (!line.isNull()) {
                  QByteArray outBytes = line.toUtf8();
                  serial->write(outBytes);
                  line = stream.readLine();
          }
      
      sankarapandiyanS Offline
      sankarapandiyanS Offline
      sankarapandiyan
      wrote on last edited by sankarapandiyan
      #5

      @KroMignon said in Serial port Communication with pc through serial Adapter:

      QTextStream stream(&inputFile);
      line = stream.readLine();
      while (!line.isNull()) {
      QByteArray outBytes = line.toUtf8();
      serial->write(outBytes);
      line = stream.readLine();
      }

      Yes Even i have tried liked this , Even the output is like

      alternate text

      Still the output is in single line i want it as line by line but i cant , give me some idea to do this task ..

      jsulmJ 1 Reply Last reply
      0
      • sankarapandiyanS sankarapandiyan

        @KroMignon said in Serial port Communication with pc through serial Adapter:

        QTextStream stream(&inputFile);
        line = stream.readLine();
        while (!line.isNull()) {
        QByteArray outBytes = line.toUtf8();
        serial->write(outBytes);
        line = stream.readLine();
        }

        Yes Even i have tried liked this , Even the output is like

        alternate text

        Still the output is in single line i want it as line by line but i cant , give me some idea to do this task ..

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

        @sankarapandiyan said in Serial port Communication with pc through serial Adapter:

        i want it as line by line

        Then put it line by line into your text edit. What is the problem with this?
        You can simply put a new line after each line:

        ui->textEdit->setText(ui->textEdit->text() + '\n' + line);
        

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

        sankarapandiyanS 1 Reply Last reply
        3
        • jsulmJ jsulm

          @sankarapandiyan said in Serial port Communication with pc through serial Adapter:

          i want it as line by line

          Then put it line by line into your text edit. What is the problem with this?
          You can simply put a new line after each line:

          ui->textEdit->setText(ui->textEdit->text() + '\n' + line);
          
          sankarapandiyanS Offline
          sankarapandiyanS Offline
          sankarapandiyan
          wrote on last edited by sankarapandiyan
          #7

          @jsulm

          QTextStream stream(&inputFile);
             line = stream.readLine();
             while (!line.isNull()) {
                     QByteArray inBytes;
                     inBytes = line.toUtf8();
                     serial->write(inBytes+ "\r" "\n"); // here i have added the "\r" and "\n" for split the word one by one 
                     line = stream.readLine();
             }
          

          I have a output like this ...
          alternate text

          now its fine ...
          But , if i press the send button its sends all the lines at the time. i need aknlment before sending the next word , so its want to repeat

          Example , if i press the send button

          line1 is in the recevier (another pc) ------- sended line1 in the aknowldgement (in my pc )
          after getting aknwldgmnt ,
          the line 2 wants to send (another pc) ---------- sended line2 in the aknowldgmnt (in my pc )

          like wis it repeats for the four lines in the text file ..

          Here how to get a aknowldgement in my code this is my code .. Give me some suggestions ..

          void MainWindow::on_send_clicked()
          {
               line   = ui->sender->toPlainText();
              QString txt1 = ui->receive->toPlainText();
              /*QString*/ txt3 = ui->aknowmnt->toPlainText();
              QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
              inputFile.open(QIODevice::ReadOnly);
              if (!inputFile.isOpen())
                  return;
          
              QTextStream stream(&inputFile);
              line = stream.readLine();
              while (!line.isNull()) {
                      QByteArray inBytes;
                      inBytes = line.toUtf8();
                      serial->write(inBytes+ "\r" "\n");
                      line = stream.readLine();
              }
              ui->aknowmnt->setText("Sent Message -->" "\n"+  line + "\n");
          }
          
          void MainWindow::serialReceived()
          {
              QString glen = serial->readAll();
              if (glen.length()>0){
              /*QString*/ line = ui->receive->toPlainText();
              ui->receive->setText(ui->receive->toPlainText()+line+"\n");
              ui->receive->setText(line + "\r\n" + glen + "\r\n");
              line = txt3;
              }
          }
          
          
          jsulmJ J.HilkJ 2 Replies Last reply
          0
          • sankarapandiyanS sankarapandiyan

            @jsulm

            QTextStream stream(&inputFile);
               line = stream.readLine();
               while (!line.isNull()) {
                       QByteArray inBytes;
                       inBytes = line.toUtf8();
                       serial->write(inBytes+ "\r" "\n"); // here i have added the "\r" and "\n" for split the word one by one 
                       line = stream.readLine();
               }
            

            I have a output like this ...
            alternate text

            now its fine ...
            But , if i press the send button its sends all the lines at the time. i need aknlment before sending the next word , so its want to repeat

            Example , if i press the send button

            line1 is in the recevier (another pc) ------- sended line1 in the aknowldgement (in my pc )
            after getting aknwldgmnt ,
            the line 2 wants to send (another pc) ---------- sended line2 in the aknowldgmnt (in my pc )

            like wis it repeats for the four lines in the text file ..

            Here how to get a aknowldgement in my code this is my code .. Give me some suggestions ..

            void MainWindow::on_send_clicked()
            {
                 line   = ui->sender->toPlainText();
                QString txt1 = ui->receive->toPlainText();
                /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                inputFile.open(QIODevice::ReadOnly);
                if (!inputFile.isOpen())
                    return;
            
                QTextStream stream(&inputFile);
                line = stream.readLine();
                while (!line.isNull()) {
                        QByteArray inBytes;
                        inBytes = line.toUtf8();
                        serial->write(inBytes+ "\r" "\n");
                        line = stream.readLine();
                }
                ui->aknowmnt->setText("Sent Message -->" "\n"+  line + "\n");
            }
            
            void MainWindow::serialReceived()
            {
                QString glen = serial->readAll();
                if (glen.length()>0){
                /*QString*/ line = ui->receive->toPlainText();
                ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                ui->receive->setText(line + "\r\n" + glen + "\r\n");
                line = txt3;
                }
            }
            
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @sankarapandiyan Well, currently you send everything at once in one loop. Do you have a protocol? I mean, the other side has to send an ack. Then it would work like this: you connect a signal to readyRead(), send first line, in slot connected to readyRead you read the ack and send next line and so on until you sent everything.

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

            1 Reply Last reply
            3
            • sankarapandiyanS sankarapandiyan

              @jsulm

              QTextStream stream(&inputFile);
                 line = stream.readLine();
                 while (!line.isNull()) {
                         QByteArray inBytes;
                         inBytes = line.toUtf8();
                         serial->write(inBytes+ "\r" "\n"); // here i have added the "\r" and "\n" for split the word one by one 
                         line = stream.readLine();
                 }
              

              I have a output like this ...
              alternate text

              now its fine ...
              But , if i press the send button its sends all the lines at the time. i need aknlment before sending the next word , so its want to repeat

              Example , if i press the send button

              line1 is in the recevier (another pc) ------- sended line1 in the aknowldgement (in my pc )
              after getting aknwldgmnt ,
              the line 2 wants to send (another pc) ---------- sended line2 in the aknowldgmnt (in my pc )

              like wis it repeats for the four lines in the text file ..

              Here how to get a aknowldgement in my code this is my code .. Give me some suggestions ..

              void MainWindow::on_send_clicked()
              {
                   line   = ui->sender->toPlainText();
                  QString txt1 = ui->receive->toPlainText();
                  /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                  QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                  inputFile.open(QIODevice::ReadOnly);
                  if (!inputFile.isOpen())
                      return;
              
                  QTextStream stream(&inputFile);
                  line = stream.readLine();
                  while (!line.isNull()) {
                          QByteArray inBytes;
                          inBytes = line.toUtf8();
                          serial->write(inBytes+ "\r" "\n");
                          line = stream.readLine();
                  }
                  ui->aknowmnt->setText("Sent Message -->" "\n"+  line + "\n");
              }
              
              void MainWindow::serialReceived()
              {
                  QString glen = serial->readAll();
                  if (glen.length()>0){
                  /*QString*/ line = ui->receive->toPlainText();
                  ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                  ui->receive->setText(line + "\r\n" + glen + "\r\n");
                  line = txt3;
                  }
              }
              
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #9

              @sankarapandiyan
              here,
              a very crude implementation on how one could do it

              void MainWindow::checkAndSendData()
              {
                  if(!m_lines.isEmpty()){
                      QByteArray baToSend = m_lines.takeFirst().toUtf8();
                      serial->write(baToSend);
                      ui->aknowmnt->setText("Sent Message -->" "\n"+  QString(baToSend) + "\n");
                  } else {
                      ui->aknowmnt->setText("All send");
                  }
              }
              
              void MainWindow ::on_send_clicked()
              {
                  line   = ui->sender->toPlainText();
                      QString txt1 = ui->receive->toPlainText();
                      /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                      
                      //m_lines is a member QStringList
                      if(m_lines.isEmpty()){
                          QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                          inputFile.open(QIODevice::ReadOnly);
                          if (!inputFile.isOpen())
                              return;
                      
                          QTextStream stream(&inputFile);
                          while(!stream.atEnd()){
                              QString line = stream.readLine();
                              m_lines.append(line + QString("\r\n"));
                          }
                      }
                      
                      checkAndSendData();
              }
              
              void MainWindow::serialReceived()
              {
                  QString glen = serial->readAll();
                  if (glen.length()>0){
                  /*QString*/ line = ui->receive->toPlainText();
                  ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                  ui->receive->setText(line + "\r\n" + glen + "\r\n");
                  line = txt3;
                  }
                  
                  checkAndSendData();
              }
              

              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.

              sankarapandiyanS 1 Reply Last reply
              4
              • J.HilkJ J.Hilk

                @sankarapandiyan
                here,
                a very crude implementation on how one could do it

                void MainWindow::checkAndSendData()
                {
                    if(!m_lines.isEmpty()){
                        QByteArray baToSend = m_lines.takeFirst().toUtf8();
                        serial->write(baToSend);
                        ui->aknowmnt->setText("Sent Message -->" "\n"+  QString(baToSend) + "\n");
                    } else {
                        ui->aknowmnt->setText("All send");
                    }
                }
                
                void MainWindow ::on_send_clicked()
                {
                    line   = ui->sender->toPlainText();
                        QString txt1 = ui->receive->toPlainText();
                        /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                        
                        //m_lines is a member QStringList
                        if(m_lines.isEmpty()){
                            QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                            inputFile.open(QIODevice::ReadOnly);
                            if (!inputFile.isOpen())
                                return;
                        
                            QTextStream stream(&inputFile);
                            while(!stream.atEnd()){
                                QString line = stream.readLine();
                                m_lines.append(line + QString("\r\n"));
                            }
                        }
                        
                        checkAndSendData();
                }
                
                void MainWindow::serialReceived()
                {
                    QString glen = serial->readAll();
                    if (glen.length()>0){
                    /*QString*/ line = ui->receive->toPlainText();
                    ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                    ui->receive->setText(line + "\r\n" + glen + "\r\n");
                    line = txt3;
                    }
                    
                    checkAndSendData();
                }
                
                sankarapandiyanS Offline
                sankarapandiyanS Offline
                sankarapandiyan
                wrote on last edited by sankarapandiyan
                #10

                @J-Hilk

                Hi i have used the code , finally i got the acknowldgement

                alternate text

                But i want to send it automatically , Here i am pressing send button for every data , like if i click the send button first time , it sends the line 1 only , i want to send the line 1 and line 2 and line 3 and line 4 with single click but at the time the the ack also i want to get automatically .

                Is it possible to get like this ..

                this is my Edited Code ..

                void MainWindow::checkAndSendData()
                {
                    if(!m_lines.isEmpty()){
                        QByteArray inBytes;
                        QByteArray baToSend = m_lines.takeFirst().toUtf8();
                        serial->write(baToSend);
                        ui->aknowmnt->setText("Sent Message -->" "\n"+ QString(baToSend) + "\n");
                    } else {
                        ui->aknowmnt->setText("All send");
                    }
                }
                
                void MainWindow::on_send_clicked()
                {
                     line   = ui->sender->toPlainText();
                    QString txt1 = ui->receive->toPlainText();
                    /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                
                     if(m_lines.isEmpty()){
                    QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                    inputFile.open(QIODevice::ReadOnly);
                    if (!inputFile.isOpen())
                        return;
                
                    QTextStream stream(&inputFile);
                    while (!stream.atEnd()){
                            line = stream.readLine();
                            m_lines.append(line+QString("\r\n"));
                    }
                     }
                     checkAndSendData();
                }
                
                void MainWindow::serialReceived()
                {
                    QString glen = serial->readAll();
                    if (glen.length()>0){
                    /*QString*/ line = ui->receive->toPlainText();
                    ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                    ui->receive->setText(line + "\r\n" + glen + "\r\n");
                    line = txt3;
                    }
                   checkAndSendData();
                }
                
                

                Give_Me_Some_Solution_for_this_Sir.

                J.HilkJ 1 Reply Last reply
                0
                • sankarapandiyanS sankarapandiyan

                  @J-Hilk

                  Hi i have used the code , finally i got the acknowldgement

                  alternate text

                  But i want to send it automatically , Here i am pressing send button for every data , like if i click the send button first time , it sends the line 1 only , i want to send the line 1 and line 2 and line 3 and line 4 with single click but at the time the the ack also i want to get automatically .

                  Is it possible to get like this ..

                  this is my Edited Code ..

                  void MainWindow::checkAndSendData()
                  {
                      if(!m_lines.isEmpty()){
                          QByteArray inBytes;
                          QByteArray baToSend = m_lines.takeFirst().toUtf8();
                          serial->write(baToSend);
                          ui->aknowmnt->setText("Sent Message -->" "\n"+ QString(baToSend) + "\n");
                      } else {
                          ui->aknowmnt->setText("All send");
                      }
                  }
                  
                  void MainWindow::on_send_clicked()
                  {
                       line   = ui->sender->toPlainText();
                      QString txt1 = ui->receive->toPlainText();
                      /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                  
                       if(m_lines.isEmpty()){
                      QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                      inputFile.open(QIODevice::ReadOnly);
                      if (!inputFile.isOpen())
                          return;
                  
                      QTextStream stream(&inputFile);
                      while (!stream.atEnd()){
                              line = stream.readLine();
                              m_lines.append(line+QString("\r\n"));
                      }
                       }
                       checkAndSendData();
                  }
                  
                  void MainWindow::serialReceived()
                  {
                      QString glen = serial->readAll();
                      if (glen.length()>0){
                      /*QString*/ line = ui->receive->toPlainText();
                      ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                      ui->receive->setText(line + "\r\n" + glen + "\r\n");
                      line = txt3;
                      }
                     checkAndSendData();
                  }
                  
                  

                  Give_Me_Some_Solution_for_this_Sir.

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

                  @sankarapandiyan
                  🤔

                  It should do that automatically.
                  See the checkAndSendData();at the end of your serialReceived function. That will call the same function to write the next line.

                  same as button pressed again.

                  are you sure serialReceived is actually called?


                  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.

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

                    @sankarapandiyan
                    🤔

                    It should do that automatically.
                    See the checkAndSendData();at the end of your serialReceived function. That will call the same function to write the next line.

                    same as button pressed again.

                    are you sure serialReceived is actually called?

                    sankarapandiyanS Offline
                    sankarapandiyanS Offline
                    sankarapandiyan
                    wrote on last edited by
                    #12

                    @J-Hilk yes i called the function , But i didnot get it automatically

                    void MainWindow::serialReceived()
                    {
                        QString glen = serial->readAll();
                        if (glen.length()>0){
                        /*QString*/ line = ui->receive->toPlainText();
                        ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                        ui->receive->setText(line + "\r\n" + glen + "\r\n");
                        line = txt3;
                        }
                       checkAndSendData();
                    }
                    
                    
                    jsulmJ 1 Reply Last reply
                    0
                    • sankarapandiyanS sankarapandiyan

                      @J-Hilk yes i called the function , But i didnot get it automatically

                      void MainWindow::serialReceived()
                      {
                          QString glen = serial->readAll();
                          if (glen.length()>0){
                          /*QString*/ line = ui->receive->toPlainText();
                          ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                          ui->receive->setText(line + "\r\n" + glen + "\r\n");
                          line = txt3;
                          }
                         checkAndSendData();
                      }
                      
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      @sankarapandiyan Is your serialReceived() called?

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

                      sankarapandiyanS 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @sankarapandiyan Is your serialReceived() called?

                        sankarapandiyanS Offline
                        sankarapandiyanS Offline
                        sankarapandiyan
                        wrote on last edited by
                        #14

                        @jsulm said in Serial port Communication with pc through serial Adapter:

                        Is your serialReceived() called?

                        serial received called in where ? i didnt understand , actually i acalled checkAndSendData(); in Serial received ..

                        But i dont know where to call serialReceived() Function in my code

                        jsulmJ 1 Reply Last reply
                        0
                        • sankarapandiyanS sankarapandiyan

                          @jsulm said in Serial port Communication with pc through serial Adapter:

                          Is your serialReceived() called?

                          serial received called in where ? i didnt understand , actually i acalled checkAndSendData(); in Serial received ..

                          But i dont know where to call serialReceived() Function in my code

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

                          @sankarapandiyan serialReceived() is a slot and needs to be connected to readRead signal - did you connect it? Else it will not be called when you get ack.

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

                          sankarapandiyanS 1 Reply Last reply
                          2
                          • jsulmJ jsulm

                            @sankarapandiyan serialReceived() is a slot and needs to be connected to readRead signal - did you connect it? Else it will not be called when you get ack.

                            sankarapandiyanS Offline
                            sankarapandiyanS Offline
                            sankarapandiyan
                            wrote on last edited by
                            #16

                            @jsulm @J-Hilk I Have tried like this

                            void MainWindow::on_send_clicked()
                            {
                                 line   = ui->sender->toPlainText();
                                QString txt1 = ui->receive->toPlainText();
                                /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                            
                                 if(m_lines.isEmpty()){
                                QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                                inputFile.open(QIODevice::ReadOnly);
                                if (!inputFile.isOpen())
                                    return;
                            
                                QTextStream stream(&inputFile);
                                while (!stream.atEnd()){
                                        line = stream.readLine();
                                        m_lines.append(line+QString("\r\n"));
                                }
                            
                                 }
                                 checkAndSendData();
                                 serialReceived(); // Here I have call a function but it also make no sense i think.
                            }
                            
                            jsulmJ J.HilkJ small_birdS 3 Replies Last reply
                            0
                            • sankarapandiyanS sankarapandiyan

                              @jsulm @J-Hilk I Have tried like this

                              void MainWindow::on_send_clicked()
                              {
                                   line   = ui->sender->toPlainText();
                                  QString txt1 = ui->receive->toPlainText();
                                  /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                              
                                   if(m_lines.isEmpty()){
                                  QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                                  inputFile.open(QIODevice::ReadOnly);
                                  if (!inputFile.isOpen())
                                      return;
                              
                                  QTextStream stream(&inputFile);
                                  while (!stream.atEnd()){
                                          line = stream.readLine();
                                          m_lines.append(line+QString("\r\n"));
                                  }
                              
                                   }
                                   checkAndSendData();
                                   serialReceived(); // Here I have call a function but it also make no sense i think.
                              }
                              
                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #17

                              @sankarapandiyan This does not answer my question.
                              Did you connect serialReceived() to readyRead()? Can you please answer this question?

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

                              sankarapandiyanS 1 Reply Last reply
                              1
                              • sankarapandiyanS sankarapandiyan

                                @jsulm @J-Hilk I Have tried like this

                                void MainWindow::on_send_clicked()
                                {
                                     line   = ui->sender->toPlainText();
                                    QString txt1 = ui->receive->toPlainText();
                                    /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                                
                                     if(m_lines.isEmpty()){
                                    QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                                    inputFile.open(QIODevice::ReadOnly);
                                    if (!inputFile.isOpen())
                                        return;
                                
                                    QTextStream stream(&inputFile);
                                    while (!stream.atEnd()){
                                            line = stream.readLine();
                                            m_lines.append(line+QString("\r\n"));
                                    }
                                
                                     }
                                     checkAndSendData();
                                     serialReceived(); // Here I have call a function but it also make no sense i think.
                                }
                                
                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #18

                                @sankarapandiyan
                                you're right, it makes no sense there.

                                serialReceived should be a slot (and it has to be markt as such inside your header file) that is called when the serial port emits the readyRead signal.

                                Ideale, you should never call this slot manually, from within your own source code.

                                change your connect to the new syntax
                                from

                                connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived()));

                                to

                                connect(serial, &QSerialPort::readyRead,this, &MainWindow::serialReceived);

                                It should/could solve a couple of issues


                                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.

                                sankarapandiyanS 1 Reply Last reply
                                2
                                • jsulmJ jsulm

                                  @sankarapandiyan This does not answer my question.
                                  Did you connect serialReceived() to readyRead()? Can you please answer this question?

                                  sankarapandiyanS Offline
                                  sankarapandiyanS Offline
                                  sankarapandiyan
                                  wrote on last edited by
                                  #19

                                  @jsulm said in Serial port Communication with pc through serial Adapter:

                                  This does not answer my question.
                                  Did you connect serialReceived() to readyRead()? Can you please answer this question?
                                  Yes Connected..

                                  void MainWindow::on_pushButton_clicked()
                                  {
                                      serial = new QSerialPort (this) ;
                                      serial->setPortName(ui->comboBox->currentText());
                                      serial->setBaudRate (QSerialPort::Baud115200);
                                      serial->setDataBits(QSerialPort::Data8);
                                      serial->setParity (QSerialPort::NoParity);
                                      serial->setStopBits (QSerialPort::OneStop);
                                      serial->setFlowControl(QSerialPort::NoFlowControl);
                                      serial->open(QIODevice::ReadWrite);
                                      connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived())); //**HERE_I_CONNECTED_THE_SERIALRECEIVED_SLOT**
                                      ui->progressBar->setValue(100);
                                  //    ui->receive->clear();
                                  }
                                  
                                  jsulmJ KroMignonK 2 Replies Last reply
                                  0
                                  • sankarapandiyanS sankarapandiyan

                                    @jsulm said in Serial port Communication with pc through serial Adapter:

                                    This does not answer my question.
                                    Did you connect serialReceived() to readyRead()? Can you please answer this question?
                                    Yes Connected..

                                    void MainWindow::on_pushButton_clicked()
                                    {
                                        serial = new QSerialPort (this) ;
                                        serial->setPortName(ui->comboBox->currentText());
                                        serial->setBaudRate (QSerialPort::Baud115200);
                                        serial->setDataBits(QSerialPort::Data8);
                                        serial->setParity (QSerialPort::NoParity);
                                        serial->setStopBits (QSerialPort::OneStop);
                                        serial->setFlowControl(QSerialPort::NoFlowControl);
                                        serial->open(QIODevice::ReadWrite);
                                        connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived())); //**HERE_I_CONNECTED_THE_SERIALRECEIVED_SLOT**
                                        ui->progressBar->setValue(100);
                                    //    ui->receive->clear();
                                    }
                                    
                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #20

                                    @sankarapandiyan OK, I didn't noticed, sorry.
                                    Check @J-Hilk response and make sure your slot is called (put a qDebug() there).

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

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

                                      @sankarapandiyan
                                      you're right, it makes no sense there.

                                      serialReceived should be a slot (and it has to be markt as such inside your header file) that is called when the serial port emits the readyRead signal.

                                      Ideale, you should never call this slot manually, from within your own source code.

                                      change your connect to the new syntax
                                      from

                                      connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived()));

                                      to

                                      connect(serial, &QSerialPort::readyRead,this, &MainWindow::serialReceived);

                                      It should/could solve a couple of issues

                                      sankarapandiyanS Offline
                                      sankarapandiyanS Offline
                                      sankarapandiyan
                                      wrote on last edited by
                                      #21

                                      @J-Hilk said in Serial port Communication with pc through serial Adapter:

                                      connect(serial, &QSerialPort::readyRead,this, &MainWindow::serialReceived);

                                      Thanks a lot . I have changed the code
                                      from

                                      connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived()));

                                      to

                                      connect(serial, &QSerialPort::readyRead,this, &MainWindow::serialReceived);
                                      but same output is came

                                      1 Reply Last reply
                                      0
                                      • sankarapandiyanS sankarapandiyan

                                        @jsulm said in Serial port Communication with pc through serial Adapter:

                                        This does not answer my question.
                                        Did you connect serialReceived() to readyRead()? Can you please answer this question?
                                        Yes Connected..

                                        void MainWindow::on_pushButton_clicked()
                                        {
                                            serial = new QSerialPort (this) ;
                                            serial->setPortName(ui->comboBox->currentText());
                                            serial->setBaudRate (QSerialPort::Baud115200);
                                            serial->setDataBits(QSerialPort::Data8);
                                            serial->setParity (QSerialPort::NoParity);
                                            serial->setStopBits (QSerialPort::OneStop);
                                            serial->setFlowControl(QSerialPort::NoFlowControl);
                                            serial->open(QIODevice::ReadWrite);
                                            connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived())); //**HERE_I_CONNECTED_THE_SERIALRECEIVED_SLOT**
                                            ui->progressBar->setValue(100);
                                        //    ui->receive->clear();
                                        }
                                        
                                        KroMignonK Offline
                                        KroMignonK Offline
                                        KroMignon
                                        wrote on last edited by KroMignon
                                        #22

                                        @sankarapandiyan said in Serial port Communication with pc through serial Adapter:

                                        connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived())); //HERE_I_CONNECTED_THE_SERIALRECEIVED_SLOT

                                        You should prefer using new connect style, so connect validity will be checked at compilation:

                                        connect(serial, &QSerialPort::readyRead,this, &MainWindow::serialReceived);
                                        

                                        And then in the slot:

                                        void MainWindow::serialReceived()
                                        {
                                            QByteArray rawData;
                                            while(!serial->atEnd())
                                            {
                                                rawData.append(serial->readAll());
                                            }
                                            if(!rawData.isEmpty())
                                            {
                                                QString glen = QString(rawDaa);
                                               /*QString*/ line = ui->receive->toPlainText();
                                               ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                                               ui->receive->setText(line + "\r\n" + glen + "\r\n");
                                               line = txt3;
                                            }
                                            checkAndSendData();
                                        }
                                        

                                        Or if you are sure always receiving a '\r' or '\n' from your device, you can do it like this:

                                        void MainWindow::serialReceived()
                                        {
                                            while(serial->canReadLine())
                                            {
                                                QString glen = QString(serial->readLine());
                                               /*QString*/ line = ui->receive->toPlainText();
                                               ui->receive->setText(ui->receive->toPlainText()+line+"\n");
                                               ui->receive->setText(line + "\r\n" + glen + "\r\n");
                                               line = txt3;
                                            }
                                            checkAndSendData();
                                        }
                                        

                                        look at documentation ==> https://doc.qt.io/qt-5/qserialport.html#canReadLine

                                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                        sankarapandiyanS 1 Reply Last reply
                                        1
                                        • J.HilkJ Offline
                                          J.HilkJ Offline
                                          J.Hilk
                                          Moderators
                                          wrote on last edited by
                                          #23

                                          As I don't have a serial dive at hand that returns data I can't debug it for you.

                                          Start the debugger (F5) set a breakpoint in serialReceived and step over/in via F10/F11 and see what happens

                                          That said, I vaguely remember that one should not call write from the slot that is connected to readyRead ( my be wrong here )

                                          So you could try:

                                          void MainWindow::serialReceived()
                                          {
                                              ....
                                              QMetaObject::invokeMethod(this, &MainWindow::checkAndSendData, Qt::QueuedConnection);
                                          }
                                          

                                          to push the write to the next event loop cycle


                                          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.

                                          sankarapandiyanS 1 Reply Last reply
                                          1

                                          • Login

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