my application doesnt work:( can you help me to understand why?
-
@RuWex What about adding error handling? A proper application should have it.
For example: m_serial->write(data) has a return value with a meaning, you simply ignore it. And there is also https://doc.qt.io/qt-5/qserialport.html#error-prop
Then you're blocking Qt event loop with your QThread::sleep(1), this can also break things.Why do you send these strings character by character instead whole string at once?
-
@RuWex said in my application doesnt work:( can you help me to understand why?:
the camera need to get the command character by character with waiting between
Then you should rework your code. You should use a QTimer and on each timeout send next character. With your current approach you're blocking the event loop.
And don't forget to add error handling... -
@jsulm
I did it,
before I load the code I took away all unrelevant line:void SendCommands::StartToSendCommand( QString fileName)//Ruth { bool ans=additionalTest->CountsTheTimeRemaining(fileName); if (ans) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); QString line; while (!in.atEnd()) { line = in.readLine(); if (line==""||line[0]=="#") continue; for(int i=0; i< line.length(); ++i) { QString letter= (QString)line[i]; MainSendCommand mainSend(letter.toLatin1()); mainSend.ActivationMainWindow(); QThread::sleep(1); } qDebug()<<endl; timer->start(GWaitingTimeBetweenCommand); QApplication::processEvents(); QThread::sleep(2); } } else { return; } }
-
@RuWex said in my application doesnt work:( can you help me to understand why?:
I did it,
What?
QTimer to send characters? No, you did not!
You start a timer after sending all the characters in the for loop... -
@RuWex said in my application doesnt work:( can you help me to understand why?:
QThread::sleep(1); QApplication::processEvents(); QThread::sleep(2);
And in addition to @jsulm none of the above lines should be in your code any longer (when you get the timer right).
-
@RuWex said in my application doesnt work:( can you help me to understand why?:
timer->start(GWaitingTimeBetweenCommand);
I did it in the constructor:
SendCommands::SendCommands() { additionalTest= new DoAdditionalTest(); timer= new QTimer(); }
void SendCommands::StartToSendCommand( QString fileName)//Ruth
{bool ans=additionalTest->CountsTheTimeRemaining(fileName); if (ans) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); QString line; while (!in.atEnd()) { line = in.readLine(); if (line==""||line[0]=="#") continue; for(int i=0; i< line.length(); ++i) { QString letter= (QString)line[i]; MainSendCommand mainSend(letter.toLatin1()); mainSend.ActivationMainWindow(); QThread::sleep(1); } qDebug()<<endl; **~~timer->start(GWaitingTimeBetweenCommand);~~** QApplication::processEvents(); QThread::sleep(2); } } else { return; }
}
-
@RuWex
Please look at the QTimer documentation, and the examples there. This does not work by "inserting a delay" into the code where it is called. Rather, it sets off a timer which keeps running, and you supply a "slot"/"callback" which will be invoked when the timer expires. It is in that function/lambda that you do whatever work you wish to perform each time the repeating or singleshot timer expires, e.g. in your case maybe it sends the next character.