2

Windows で QextSerialPort を使用して、Qt/C++ アプリケーションがシリアル ポートとの間で読み書きできるようにしています。

読み取るバイトがある場合にのみ、シリアル ポートからバイトを読み取りたい。

まず、QextSerialPort::readyRead()シグナルをメイン クラスのスロットに接続しようとしましたが、アプリケーションがハングすることに気付きました。

次に、読み取ったバイト数を返す を使用してみました。その後、別の失敗した組み合わせをQextSerialPort::read(char *, uint64)試み、それがアプリケーションをブロックしないようにするのに役立つかどうかを確認しました。QextSerialPort::bytesAvailable()QextSerialPort::read(char *, uint64)

ただし、アプリケーションはマウスまたはキーボードのイベントに応答しないため、常にブロックされ、強制終了する必要があります。読み取りルーチンがブロックされるため、アプリケーションがフリーズすると思います。

QextSerialPort でノンブロッキング読み取りを実行する方法はありますか?

そうでない場合、Windows のシリアル ポートからノンブロッキング読み取り機能を使用するには、どのライブラリを使用すればよいですか?

更新: orQextSerialPort::atEnd()を使用する代わりに読み取るバイトがあるかどうかを確認するために 使用しようとしましたが、常に false を返します。QextSerialPort::bytesAvailable()QextSerialPort::size()

4

1 に答える 1

0

クエリモードをポーリングに設定していない限り、読み取りはノンブロッキングである必要があります

    inline QueryMode queryMode() const { return _queryMode; }

    /*!
     * Set desired serial communication handling style. You may choose from polling
     * or event driven approach. This function does nothing when port is open; to
     * apply changes port must be reopened.
     *
     * In event driven approach read() and write() functions are acting
     * asynchronously. They return immediately and the operation is performed in
     * the background, so they doesn't freeze the calling thread.
     * To determine when operation is finished, QextSerialPort runs separate thread
     * and monitors serial port events. Whenever the event occurs, adequate signal
     * is emitted.
     *
     * When polling is set, read() and write() are acting synchronously. Signals are
     * not working in this mode and some functions may not be available. The advantage
     * of polling is that it generates less overhead due to lack of signals emissions
     * and it doesn't start separate thread to monitor events.
     *
     * Generally event driven approach is more capable and friendly, although some
     * applications may need as low overhead as possible and then polling comes.
于 2011-03-22T18:29:55.867 に答える