5

Linux ボックスに接続された 2 つの USB マウスからデータを読み取ろうとしています (このデータは、ロボットのオドメトリ/位置特定に使用されます)。そのため、各マウスからどれだけ移動したかを継続的に読み取る必要があります。問題は、マウスが動いていないときはデータを送信しないため、データを取得するファイル ストリームが実行をブロックし、プログラムがオドメトリ計算 (速度の時間測定を含む) を実行できないことです。 .

入力ストリームにタイムアウトを設定する方法はありますか (C++ で ifstream を使用し、/dev/input/mouse から読み取ります)、待機する代わりに、マウスが動かないときを知ることができます。イベントを受け取るには?それとも、スレッドを台無しにする必要がありますか (arggh...)? 他の提案は大歓迎です!

前もって感謝します!

4

4 に答える 4

7

A common way to read from multiple file descriptors in linux is to use select(). I suggest starting with the manpage. The basic system flow is as follows:

1) Initialize devices
2) Obtain list of device file descriptors
3) Setup the time out
4) Call select with file descriptors and timeout as parameters - it will block until there is data on one of the file descriptors or the time out is reached
5) Determine why select returned and act accordingly (i.e. call read() on the file descriptor that has data). You may need to internally buffer the result of read until an entire data gram is obtained.
6) loop back to 4.

This can become your programs main loop. If you already have a different main loop you, can run the above without looping, but your will need to insure that the function is called frequently enough such that you do not lose data on the serial ports. You should also insure that your update rate (i.e. 1/timeout) is fast enough for your primary task.

Select can operate on any file descriptor such network sockets and anything else that exposes an interface through a file descriptor.

于 2009-02-22T16:31:59.017 に答える
1

あなたが探しているのは、ソケット通信のように、ifstream から読み取る非同期の方法です。役立つ唯一のものはreadsome関数です。データが利用できない場合に返される可能性がありますが、これが役立つとは思えません。

これを処理するには、スレッドを使用するのが最善の方法です。

于 2009-02-22T15:36:31.847 に答える
0

Take a look at the boost Asio library. This might help you deal with the threading suggested by schnaeder.

于 2009-02-22T16:33:21.617 に答える
-1

いいえ、そのような方法はありません。イベントを待つか、カスタム Timer クラスを作成して再ポーリングのタイムアウトを待つか、スレッドを使用する必要があります。

于 2009-02-22T15:34:22.493 に答える