新しいデータを常に探しているスレッドがあり、データがまだシリアルバッファにない場合、データがありReadFile
、GetOverlappedResult
それを読み取ったが、バッファに転送していないことがわかります...
func read()
{
if(state == 0)
{
memset(bytes, '\0', sizeof(amount_to_read));
readreturn = ReadFile(h, bytes, amount_to_read,NULL, osReader);
if(readreturn <= 0)
{
errorcode = GetLastError();
if(errorcode != ERROR_IO_PENDING)
{
SetEAIError(ERROR_INTERNALERROR);
return -1;
}
}
}
if (GetOverlappedResult(h, osReader, &dwRead, FALSE) == false)
{
errorcode = GetLastError();
if (errorcode == ERROR_IO_INCOMPLETE || errorcode == 0)
{
if(dwRead > 0)
{
return 1;
}
//timeout
SetEAIError(ERROR_EAITIMEOUT);
return -1;
}
else
{
//other error
SetEAIError(ERROR_WIN_ERROR);
return -1;
}
}
else
{
//read succeded, check if we read the amount required
if(dwRead != amount_to_read)
{
if(dwRead == 0)
{
//nothing read, treat as timeout
SetEAIError(ERROR_EAINOREAD);
return -1;
}
else
{
//memcpy_s(bytes, sizeof(bytes), readbuf, dwRead);
SetEAIError(ERROR_PARTIALREAD);
*_bytesRead = dwRead;
return -1;
}
}
else
{
if(strlen((char*)bytes) == 0)
{
//nothing read, treat as timeout
SetEAIError(ERROR_EAINOREAD);
return -1;
}
//memcpy_s(bytes, sizeof(bytes), readbuf, dwRead);
*_bytesRead = dwRead;
return 0;
}
}
}
エラーコードの意味は次のとおりです。
ERROR_TIMEOUT - 再度読み込まれないように状態を 1 に切り替えます
GetOverlappedResult
。INTERNALERROR、ERROR_EANOREAD - 状態を 0 にリセットします
ERROR_PARTIALREAD - 読み取る新しいバイト数で新しい読み取りを開始します
GetOverlappedResult をブロッキングに切り替える (TRUE を渡す) と、毎回動作します。そこにデータがあることがわかっているときにスレッドを読み取り専用に切り替えると、毎回機能します。
しかし、そこにデータがない場合、そこにデータがある場合、データが「失われる」ように見えます。私の読み取り量パラメーターは、読み取らdwRead
れた正しいバイト数を示します (ポートモニターで読み取られたことを確認できます)が、バイトは保存されません私の文字で*。
私は常に ERROR_EANOREAD を受け取ります
私は何を間違っていますか?
私はフラグを使用したくない、私はただ使用したいReadFile
and GetOverlappedResult
、私が持っているコードでこれを達成できるはずです.......私は仮定します