アリスです。
1.BeginReadの前に、データが来ているかチェック
if( ns.DataAvailable)
{
IAsyncResult ar = ns.BeginRead(readBytes, 0, readBytes.Length, null, null);
}
この方法だと、普通に実行した場合、データがまだきておらず、読み込みができなかった。
ステップ実行だと、ゆっくりなので必ずif分のなかにはいるのだが。。
2.読み込みサイズが256バイト以下だったら、読み込み停止
int readSize = ns.EndRead(ar);
if (readSize < 256)
{
Break;
}この方法は駄目。複数回受信するコマンドの場合に駄目。最初の1回目が33バイトで、2回目が256バイトとなるパターンがあった。
これもステップ実行だと、ちゃんとうまくいくのだが・・
3.無限ループにした
byte[] readBytes = new byte[256];
while (true)
{
IAsyncResult ar = ns.BeginRead(readBytes, 0, readBytes.Length, null, null);
if (ar.AsyncWaitHandle.WaitOne(timeout) == false)
{
break;
}
int readSize = ns.EndRead(ar);
ms.Write(readBytes, 0, readSize);
}
