單片機通過wifi將采集到傳感器的數據傳至上位機,單片機是每一秒發送一組數據,如“0xaa,0x55,‘采集到的數據1’,‘采集到的數據2’”
問:怎樣才能接收數據並判斷第一個是“0xaa“並且第二位是”0x55“時候將”采集到的數據1“和”采集到的數據2“拿出來並整合合,下一秒也是這樣循環下去,我現在只能做到接收數據。
private byte[] m_receiveBuffer = new byte[1024];
sock.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
private void ReceiveCallBack(IAsyncResult ar)
{
try
{
int REnd = sock.EndReceive(ar);
string strReceiveData = m_receiveBuffer[0].ToString("X2");
this.HandleMessage(strReceiveData);
sock.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void HandleMessage(string message)
{
message = message.Replace("\0", "");
if (!string.IsNullOrEmpty(message))
{
label6.Text = message;
}
}
已解決,不過還需要優化,比如延時問題