我收到一个像 0xFA5D0D01 这样的数据包。现在我想把它像
FA 是 Header1 5D 是 Header2 0D 是长度,01 是校验和。常量 int data_available = Serial.available();
我能够写入串行端口,但不能像收到 FA 那样将其打包,然后打印收到的 Header1
const int data_availabe = Serial.available();
if (data_availabe <= 0)
{
return;
}
const int c = Serial.read();
Serial.print("Receive Status: ");
Serial.println(STATE_NAME[receiveState]);
Serial.print(c, HEX);
Serial.print(" ");
if (isprint(c)) //isprint checks whether it is printable character or not (e.g non printable char = \t)
{
Serial.write(c);
}
Serial.println();
Serial.println(receiveState);
switch (receiveState)
{
case WAITING_FOR_HEADER1:
if (c == HEADER1)
{
receiveState = WAITING_FOR_HEADER2;
}
break;
case WAITING_FOR_HEADER2:
if (c == HEADER2)
{
receiveState = WAITING_FOR_LENGTH;
}
break;
}
当我们获得预期的数据时,receiveState 正在发生变化。