0
블루투스 스트림을 안정적으로 읽는 데 문제가 있습니다. 블루투스 연결로 작업 한 것은 처음입니다. 응용 프로그램은 블루투스 모듈을 통해 Arduino와 통신합니다. 명령을 보내고 응답을받습니다. 예를 들어
는 응답은 다음과 비슷한 모습이 될 것
1
을 내가 (이 완전히 다른 보일 것 또한 경우) 다른 응답을받을 경우 내가 응답의 나머지를 얻을 :
100,200,300,400
이 내가 무엇을 얻을 이전에 요청한 경우 :
00,200,300,400
가끔은 응답이 없습니다. 여기 C# 블루투스 스트림을 안정적으로 읽음 (InTheHand 32feet)
void BluetoothClientConnectCallback(IAsyncResult result)
{
try
{
BluetoothClient client = (BluetoothClient)result.AsyncState;
client.EndConnect(result);
NetworkStream stream = client.GetStream();
stream.ReadTimeout = 1000;
_frm.printMsg("Connected!", false);
byte[] receive = new byte[1024];
while (true)
{
while (!ready) ;
if (stopConnection == true)
{
return;
}
try
{
stream.Write(message, 0, message.Length);
}
catch
{
_frm.printMsg("Error occurred while writing stream.", true);
}
try
{
if (Commands.awaitresponse == true)
{
Array.Clear(receive, 0, receive.Length);
readMessage = "";
do
{
stream.Read(receive, 0, receive.Length);
readMessage += Encoding.ASCII.GetString(receive);
}
while (stream.DataAvailable);
_frm.printMsg("Received: " + readMessage, false);
Commands.awaitresponse = false;
}
}
catch
{
_frm.printMsg("Error occurred while receiving stream.", true);
}
ready = false;
}
}
catch
{
_frm.printMsg("Error occurred while connecting.", true);
}
}
하지만 해결책에 올 수 없었다.