my application doesnt work:( can you help me to understand why?
-
I write application that send commands t camera by USB
the terminal work well and everithing amazing:)
but--
in my application there is option also to choose file with commands and my appication should read the command and send them to the usb- to the camera.
and then I read them char by char and use my function I uses always to send to the terminal, but now its not work.
I will show you my file, and be happy for your help:)SendCommand.cpp:
SendCommands::SendCommands() { additionalTest= new DoAdditionalTest(); timer= new QTimer(); } void SendCommands::StartToSendCommand(/*QPlainTextEdit *textEdit,*/ 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); } timer->start(GWaitingTimeBetweenCommand); QApplication::processEvents(); } } else { return; } }
mainSendCommand.cpp:
MainSendCommand::MainSendCommand(/* QPlainTextEdit *textEdit,*/QString fileName) { sendCommand= new SendCommands(); mainwind= new MainWindow(); /*this->textEdit= textEdit;*/ this->fileName= fileName; } MainSendCommand::MainSendCommand(QByteArray file) { sendCommand= new SendCommands(); mainwind= new MainWindow(); this->file= file; } MainSendCommand::MainSendCommand() { sendCommand= new SendCommands(); mainwind= new MainWindow(); } void MainSendCommand::ActivationSendCommand() { sendCommand->StartToSendCommand(/*textEdit,*/fileName ); // mainwind->ChangeStatus("Send Commands"); } void MainSendCommand::ActivationMainWindow() { mainwind->writeData(file); // mainwind->ChangeStatus("Send Commands"); }
mainWindow.cpp:
void MainWindow::writeData(const QByteArray &data) { DBG; m_serial->write(data); }
??
do tou have any idea?
thank:) -
@RuWex said in my application doesnt work:( can you help me to understand why?:
do tou have any idea?
This is quite messed up code to be honest.
What is MainWindow? If it is QMainWindow - why do you create one each time you call MainSendCommand::MainSendCommand() or MainSendCommand::MainSendCommand(QByteArray file)?! And are you aware that you have memory leaks all over the place?You also forgot to say which part exactly does not work...
-
@jsulm
I did it because of a problem I had in my application- its Unfortunately, consumed
this line in sendCommand.cpp doesnt work:MainSendCommand mainSend(letter.toLatin1()); mainSend.ActivationMainWindow();
I just want to remind what I have in mainSendCommand.cpp:
MainSendCommand::MainSendCommand(QByteArray file) { sendCommand= new SendCommands(); mainwind= new MainWindow(); this->file= file; } void MainSendCommand::ActivationMainWindow() { mainwind->writeData(file); }
-
@RuWex said in my application doesnt work:( can you help me to understand why?:
this line in sendCommand.cpp doesnt work
There are two lines.
And each of this lines involves other code. Please tell us what exactly does not work, I'm not a debugger... -
@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.