单片机通过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;
}
}