本帖最后由 tanks22 于 2014-10-22 08:48 编辑
從客戶端發送一個指令給智能插座,然後讀取智能插座返回的指令。現在的情況是,在點擊第一個按鈕后,Receive的數據可能沒有讀取完,
而在點擊第二個按鈕后,得到的字符串包含第一個按鈕產生的數據。
怎麼才能讓第一個的數據接收完?
對通信協議不是很懂,希望大家指點指點,謝謝啊!
IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);
IPEndPoint localPoint = new IPEndPoint(localIP, localPort);
UdpClient udpclt = new UdpClient(localPoint);
byte[] send1 = new byte[] { 0x05, 0x00, 0x00, 0xFF, 0x00 };
byte[] send2 = new byte[] { 0x05, 0x00, 0x00, 0x00, 0x00 };
private void button1_Click(object sender, EventArgs e)
{
udpclt.Send(send1, send1.Length, remotePoint);
Thread.Sleep(100);
string str1 = getReceive();
}
private void button2_Click(object sender, EventArgs e)
{
udpclt.Send(send2, send2.Length, remotePoint);
Thread.Sleep(100);
string str2 = getReceive();
}
public void getReceive()
{
string sMsg = string.Empty;
bool isStop = false;
while (!isStop)
{
Byte[] receiveData = new byte[100];
while (udpclt.Available > 0)
{
receiveData = udpclt.Receive(ref remotePoint);//接收數據
sMsg = byteArrayToString(receiveData);
}
isStop = true;
}
return sMsg;
}
|