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



  • 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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.