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. Error in the value read from serialPort->bytesAvailable()
QtWS25 Last Chance

Error in the value read from serialPort->bytesAvailable()

Scheduled Pinned Locked Moved General and Desktop
serialport-byte
2 Posts 2 Posters 774 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.
  • S Offline
    S Offline
    sdcr
    wrote on last edited by
    #1

    The value read from sp->bytesAvailable() change when I change the while loop condition. Examples:

    @@ CONDITION 1 [ == ]

    ** CODE
    // write in serial Port
    sp->clear();
    sp->write(...);
    sp->waitForBytesWritten(100);

    // read in serial port
    qint64 bites = 0;
    spBill->waitForReadyRead(100);

    while (((bites = sp->bytesAvailable()) == 0) && (ind < _RETRY))
    {
    std::cout << "bytes = " << bites << std::endl;
    debugShow(QString("Esperando respuesta desde puerto serie. retry %1 de %2..").arg(ind).arg(_RETRY));
    sp->waitForReadyRead(100);
    ind++;
    }

    ** CONSOLE OUTPUT
    bytes = 0
    Esperando respuesta desde puerto serie. retry 0 de 100..
    bytes = 0
    Esperando respuesta desde puerto serie. retry 1 de 100..
    bytes = 0
    Esperando respuesta desde puerto serie. retry 2 de 100..
    bytes = 0
    Esperando respuesta desde puerto serie. retry 3 de 100..
    ..
    ..
    bytes = 0
    Esperando respuesta desde puerto serie. retry 100 de 100..

    @@ CONDITION 2 [ != 0 ]

    ** CODE
    // write in serial Port
    sp->clear();
    sp->write(...);
    sp->waitForBytesWritten(100);

    // read in serial port
    qint64 bites = 0;
    spBill->waitForReadyRead(100);

    while (((bites = sp->bytesAvailable()) != 0) && (ind < _RETRY))
    {
    std::cout << "bytes = " << bites << std::endl;
    debugShow(QString("Esperando respuesta desde puerto serie. retry %1 de %2..").arg(ind).arg(_RETRY));
    sp->waitForReadyRead(100);
    ind++;
    }

    ** CONSOLE OUTPUT
    bytes = 1
    Esperando respuesta desde puerto serie. retry 0 de 100..
    bytes = 2
    Esperando respuesta desde puerto serie. retry 1 de 100..
    bytes = 3
    Esperando respuesta desde puerto serie. retry 2 de 100..
    ..
    ..
    bytes = 5
    Esperando respuesta desde puerto serie. retry 100 de 100..

    Test are made on a device connected to serial port through a serial port adapter on ubuntu.

    The value of sp->bytesAvailable() is modified to evaluated the while loop condition always true.. What would be the problem in the code?

    thanks

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jeroen3
      wrote on last edited by
      #2

      @sdcr said:

      (bites = sp->bytesAvailable())

      Although it should return a reference to bites, it is not good coding practice imho.
      http://en.cppreference.com/w/cpp/language/operator_assignment

      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