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. serialport
Forum Updated to NodeBB v4.3 + New Features

serialport

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 808 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.
  • sankarapandiyanS Offline
    sankarapandiyanS Offline
    sankarapandiyan
    wrote on last edited by VRonin
    #1
    void MainWindow::on_clickbutn_clicked()
    {
        QString qstr;
        qstr=ui->senderline->text();
        QByteArray array;
        array.append(qstr);
    
        qstr=ui->senderline->text();
        
        qDebug() << array;
    
        serial->write(array);
        connect(serial,SIGNAL(readyRead()),this,SLOT(read()));
      }
    void MainWindow::read()
    {
    // qDebug() << serial->readAll();
       QByteArray datas = serial->readAll();
        ui->receline->setText(datas);
        
    }
    

    this is my code ,my concept is i have to give a input as a text in the line edit(input text) (FIRST LINEEDIT) and i have another line edit(SECOND LINE EDIT) also ,By clicking the pushbutton it shows text in the second line edit what we type in the first line edit.....

    And my problem i gave a input as a text in a first line edit and i got a output in a second line edit **but the last letter alone display in the second line edit i want to display whole text **
    i dont know what to do ..
    i want a rply soon guys........

    this is my output

    0_1528889719516_Screenshot from 2018-06-13 17:01:51.png

    1 Reply Last reply
    0
    • aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @sankarapandiyan

      I won't comment your code, but it looks like it could be cleaned up a lot.

      I think your actual problem is, that MainWindow::read() is called multiple times when a new chunk of data comes in, but you just display this chunk in the line edit: ui->receline->setText(datas);

      That means, ui->receline contains the last chunk you received from the serial port.

      i want a rply soon guys........

      You probably mean, you would be very thankful if somebody could help you with your problem.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      6
      • sankarapandiyanS Offline
        sankarapandiyanS Offline
        sankarapandiyan
        wrote on last edited by
        #3

        ok sure ,
        thanks a lot @aha_1980

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4
          1. Why are you using a QIODevice (QSerialPort?) to send data from one method of MainWindow to another?
          2. connect is not unique by default. If you click the button twice the connection will be repeated and the slot will execute 2 times for each signal. either add Qt::UniqueConnection as last argument or, better, move the connect somewhere else (the constructor, maybe)
          3. readyRead is emitted when some data is available, there's no guarantee all the data can be read
          4. use streams to safely serialise data:
          const QString qstr=ui->senderline->text();
          QDataStream serialWriteStream(serial);
          serialWriteStream<< qstr;
          
          QDataStream serialReadStream(serial);
          serialReadStream.startTransaction();
          QString receivedText;
          serialReadStream >> receivedText;
          if(serialReadStream.commitTransaction())
          ui->receline->setText(receivedText);
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          5
          • sankarapandiyanS Offline
            sankarapandiyanS Offline
            sankarapandiyan
            wrote on last edited by
            #5

            thanks @VRonin

            1 Reply Last reply
            0

            • Login

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