Serial not writing directly
-
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++; }
-
@User94
What "timeout"? And how do you know from your code whether it is theserial->write()
which is not writing/sending immediately versus yourReadInput()
(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. -
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.