1、上位机发送十六进制 AA 01 DE 下位机回复AC,上位机发送十六进制 AA 02 DE 下位机回复AB。如下图所所示。
2、Arduino 代码如下。
#define ReceiveLen 100 // 接收数据数组长度
byte ReceiveData[ReceiveLen]; // 接收数据数组void loop()
{// 串口接收数据if (Serial.available() > 0){memset(ReceiveData, 0, sizeof(ReceiveData));byte IndexMark = 0;while (Serial.available()){ReceiveData[IndexMark] = Serial.read();IndexMark++;}for (byte i = 0; i < ReceiveLen; i++){switch (ReceiveData[i]){case protocolHeard:if (ReceiveData[i+2] == protocolEnd){if (ReceiveData[i+1] == 0x01){// byte _1=0x11;Serial.write(0xAC);}else{// byte _2=0x22;Serial.write(0xAB);} }else{// byte _3=0x33;Serial.write(0xAC);} break;default:break;}}}
}