私はシリアルポートに読み書きするマルチスレッドプログラムで作業しています。私がパテを使ってアプリケーションをテストすると、すべてが正常です。しかし、作成した.exeファイルでテストすると、動作しません。 (私はVS2017でプログラムを起動し、次に.exeファイルを起動します)シリアルポートReadFileは繰り返しデータを受信します
例:私の入力: "Test"、他のウィンドウの出力: "Teeeeeeeeeeeessssssssssttttttt"。アレイの「ライン」私は、ユーザからの入力を持って
void SendDataToPort()
{
for (size_t i = 0; i <= strlen(line); i++) // loop through the array until
every letter has been sent
{
try
{
if (i == strlen(line)) // If ever letter has been sent
{ // end it with a new line
c = '\n'; // a new line
}
else
{
c = line[i];
}
WriteFile(serialHandle, &c, 1, &dwBytesWrite, NULL); // Send letter to serial port
}
catch (const exception&)
{
cout << "Error while writing.";
}
}
cout << endl << "Sent." << endl;
}
:データを送信するために
私のコード。私は印刷(出力)機能付きダウンデータを書き込む。その後
int newLineCounter = 0;
unsigned char tmp;
while (!endCurrentRead)
{
ReadFile(hSerial, &tmp, 1, &bytesRead, NULL); // Get new letter
if (tmp != '\n')
{
newLineCounter = 0;
if (tmp >= 32 && tmp <= 126)
{
output += tmp; // Add current letter to the output
}
}
else
{
newLineCounter++;
if (newLineCounter < 2) // If there are more than 2 '\n' it doesn't write it down
{
output += tmp;
}
else if (newLineCounter == 2)
{
Print(output);
output = receiverName;
endCurrentRead = true;
}
}
}
:データを読み込むため
私のコード
:私はハンドルファイルを作成する方法cout << output << endl;
serialHandle = CreateFile(LcomPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
なぜこれが起こっているのですか?これは、パテではなく.exeファイルでテストするとどうしてですか?
あなたはどちらかReadFile'または '' bytesRead'の戻り値をチェックしません。 –
とDavisが言っていたのは、あなたが戻り値を得ることができるため、何も読まれていない**(受信バッファが空で新しいデータを待たずに)通知とそれは前と同じデータを持っていると仮定すると、ゆっくりと返されます。 – quetzalcoatl
@DavidSchwartz戻り値を確認するにはどうすればよいですか? – User987123