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

Serial not writing directly

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 450 Views 1 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.
  • U Offline
    U Offline
    User94
    wrote on last edited by User94
    #1

    I'm writing on serial but it ain't writing right away it is sending when it hits timeout (this is at the first time that I send data) then when I send data again it sends directly but the buffer is already not empty so what I get is the old values... Can anyone help?

    This is the code:

    if(serial->isWritable()){
                QString datasend = "123";
                serial->write(datasend.toStdString().c_str());
            }
    
            QString thisSerial = "";
            long long counter = 0;
            while(true){
                thisSerial = ReadInput();
                if(thisSerial != ""){
                      // do something
                      break;
                }
                else if(counter == 650000){
                    QMessageBox msgBox;
                    msgBox.setText("Couldn't read data");
                    msgBox.setWindowTitle("Capture Error");
                    msgBox.exec();
                    return;
                }
                counter++;
         }
    JonBJ 1 Reply Last reply
    0
    • U User94

      I'm writing on serial but it ain't writing right away it is sending when it hits timeout (this is at the first time that I send data) then when I send data again it sends directly but the buffer is already not empty so what I get is the old values... Can anyone help?

      This is the code:

      if(serial->isWritable()){
                  QString datasend = "123";
                  serial->write(datasend.toStdString().c_str());
              }
      
              QString thisSerial = "";
              long long counter = 0;
              while(true){
                  thisSerial = ReadInput();
                  if(thisSerial != ""){
                        // do something
                        break;
                  }
                  else if(counter == 650000){
                      QMessageBox msgBox;
                      msgBox.setText("Couldn't read data");
                      msgBox.setWindowTitle("Capture Error");
                      msgBox.exec();
                      return;
                  }
                  counter++;
           }
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @User94
      What "timeout"? And how do you know from your code whether it is the serial->write() which is not writing/sending immediately versus your ReadInput() (whose code you don't show us anyway) not receiving a reply right away?

      I would suggest not using a while(true) ... ReadInput() ... break but doing it properly with signals & slots and allowing the Qt event loop to run, and see how it goes.

      U 1 Reply Last reply
      2
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        another example of trying to use traditional programming mechanisms in an event driven architecture. You should not use infinite loops in Qt code. It will amost certainly break the event handler loop that in intrinsic in Qt.

        I light my way forward with the fires of all the bridges I've burned behind me.

        1 Reply Last reply
        2
        • JonBJ JonB

          @User94
          What "timeout"? And how do you know from your code whether it is the serial->write() which is not writing/sending immediately versus your ReadInput() (whose code you don't show us anyway) not receiving a reply right away?

          I would suggest not using a while(true) ... ReadInput() ... break but doing it properly with signals & slots and allowing the Qt event loop to run, and see how it goes.

          U Offline
          U Offline
          User94
          wrote on last edited by
          #4

          @JonB it's because of the hardware that I know when serial data is sent (TX light blinks when data received)

          JonBJ 1 Reply Last reply
          0
          • U User94

            @JonB it's because of the hardware that I know when serial data is sent (TX light blinks when data received)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @User94

            @JonB said in Serial not writing directly:

            I would suggest not using a while(true) ... ReadInput() ... break but doing it properly with signals & slots and allowing the Qt event loop to run, and see how it goes.

            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