In your scenario, a thread is responding to a blocking read/write method, waiting until it detects a keyword or times out using waitForKeyword(). Unfortunately, when readLines() is utilized to test the app, only a small portion of the file is obtained. Additionally, readLines immediately returns false during the second iteration without waiting for timeout.
The issue stems from the design of readLines(), which reads all available data and separates it into lines. When a sequence of commands is being processed, readLines() reads only the first part of the file because it doesn't detect the keyword. Subsequently, when it's called again, readLines() returns false as it has already iterated over the entire available data.
To effectively send a sequence of commands and wait for their responses, consider utilizing a state machine approach. This provides a structured and reliable method for managing the flow of commands and responses, ensuring that the expected response is received before proceeding.
The following snippet demonstrates a state machine implementation using Qt's QStateMachine and related classes:
class Programmer : public StatefulObject { Q_OBJECT AppPipe m_port { nullptr, QIODevice::ReadWrite, this }; State s_boot { &m_mach, "s_boot" }, s_send { &m_mach, "s_send" }; FinalState s_ok { &m_mach, "s_ok" }, s_failed { &m_mach, "s_failed" }; public: Programmer(QObject * parent = 0) : StatefulObject(parent) { connectSignals(); m_mach.setInitialState(&s_boot); send (&s_boot, &m_port, "boot\n"); expect(&s_boot, &m_port, "boot successful", &s_send, 1000, &s_failed); send (&s_send, &m_port, ":HULLOTHERE\n:00000001FF\n"); expect(&s_send, &m_port, "load successful", &s_ok, 1000, &s_failed); } AppPipe & pipe() { return m_port; } };
In this example, Programmer encapsulates the state machine and provides an interface to communicate with the device. State transitions are defined using the send() and expect() functions to handle sending commands and waiting for expected responses, respectively.
Using a state machine has several advantages:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3