는 일련의 명령을 효과적으로 보내고 응답을 기다리려면 상태 기계 접근 방식을 활용하는 것을 고려하십시오. 이것은 명령 및 응답의 흐름을 관리하기위한 구조적이고 신뢰할 수있는 방법을 제공하여 진행하기 전에 예상 응답을 받도록 보장합니다.
다음 스 니펫은 QT의 QSTATEMACHINE 및 관련 클래스를 사용하여 상태 기계 구현을 보여줍니다. 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"}; 공공의: 프로그래머 (qobject * parent = 0) : statefulobject (부모) { ConnectSignals (); m_mach.setinitialstate (& s_boot); send (& s_boot, & m_port, "boot \ n"); 기대 (& s_boot, & m_port, "boot success", & s_send, 1000, & s_failed); send (& s_send, & m_port, ": Hullothere \ n : 00000001ff \ n"); expect (& s_send, & m_port, "로드 성공", & s_ok, 1000, & s_failed); } AppPipe & Pipe () {return m_port; } };
시간 초과 처리
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; } };
비동기 특성
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3