Ok, maybe. But how to rewrite the next algorithm with signals and slots?
gui thread:
startDataExchange() {
// create protocol, using customIODevice
AsciiProtocol protocol(customIODevice);
// Ascii protocol's method sending some data into QIODevive
protocol.sendCmd("cmd1;");
// wait for answer
if(!waitForCmd())
errorHandler();
if(protocol.getCmd() == "someCommand") {
.. some actions ..
} else if ..... {
} else {
}
protocol.sendCmd("cmd2;");
... etc..
}
startDataExchange() {
// create protocol, using customIODevice
AsciiProtocol protocol(customIODevice);
// Ascii protocol's method sending some data into QIODevive
protocol.sendCmd("cmd1;");
// wait for answer
if(!waitForCmd())
errorHandler();
if(protocol.getCmd() == "someCommand") {
.. some actions ..
} else if ..... {
} else {
}
protocol.sendCmd("cmd2;");
... etc..
}
To copy to clipboard, switch view to plain text mode
If I understood right, all I need is to do something following:
init() {
// attach protocol to customIODevice
protocol->attach(customIODevice);
}
startDataExchange() {
connect(protocol, SIGNAL(cmdReceived()), this, SLOT(handleCmd1()));
// Ascii protocol's method sending some data into QIODevive
protocol.sendCmd("cmd1;");
}
handleCmd1() {
// cmd1 received ?
if(protocol->cmd() == "cmd1") {
disconnect(protocol, SIGNAL(cmdReceived()), this, SLOT(handleCmd1()));
connect(protocol, SIGNAL(cmdReceived()), this, SLOT(handleCmd2()));
protocol->sendCmd("cmd2");
}
}
handleCmd2() {
... etc
}
init() {
// attach protocol to customIODevice
protocol->attach(customIODevice);
}
startDataExchange() {
connect(protocol, SIGNAL(cmdReceived()), this, SLOT(handleCmd1()));
// Ascii protocol's method sending some data into QIODevive
protocol.sendCmd("cmd1;");
}
handleCmd1() {
// cmd1 received ?
if(protocol->cmd() == "cmd1") {
disconnect(protocol, SIGNAL(cmdReceived()), this, SLOT(handleCmd1()));
connect(protocol, SIGNAL(cmdReceived()), this, SLOT(handleCmd2()));
protocol->sendCmd("cmd2");
}
}
handleCmd2() {
... etc
}
To copy to clipboard, switch view to plain text mode
But to my mind it is not such a clear approach, as if all the data exchange related code situated in one function... What will be if data exchange session has 10 data senders and 10 data receivers? And what will be if this number is unknown before session starts ? QApplication :: processEvents worked for me in this way, but it used too much CPU time, so I was need to put Sleep(1) before it (win32 api functuion).. That is why I thought QTimer maybe is more helpful in my case...
Bookmarks