我在arduino上编码,我正在与HEX中的其他设备通信。我想知道如何阅读他发给我的数据。
我正在发送一个十六进制包(这里一切都很好,没问题)
//Ask for Data
Serial.write(askData, sizeof(askData));
在此之后,我将收到数据(在HEX中)。我需要将它全部存储起来以便以后使用它。我唯一知道的是它会以“16”结尾。我事先不知道数据包的长度。以下是我可以了解的示例或数据包:
68 4E 4E 68 08 09 72 90 90 85 45 68 50 49 06 19 00 00 00
0C 14 02 00 00 00 8C 10 12 35 02 00 00 0B 3B 00 00 00 8C
20 14 02 00 00 00 8C 30 14 00 00 00 00 04 6D 2F 09 61 24
4C 14 02 00 00 00 42 6C 5F 2C 42 EC 7E 7F 2C 0A 92 2A 00
10 0A 92 2B 00 10 39 16
有谁可以帮助我吗 ?
This arduino示例略有修改:
/* reserve 200 bytes for the inputString:
* assuming a maximum of 200 bytes
*/
uint8_t inputString[200]; // a String to hold incoming data
int countInput = 0;
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(115200);
}
void loop() {
// print the string when 0x16 arrives:
if (stringComplete) {
for (int i=0; i<countInput; i++) {
Serial.print(inputString[i], HEX);
Serial.print(" ");
}
// clear the string:
countInput = 0;
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
inputString[countInput] = (uint8_t)Serial.read();
// if the incoming character is '0x16', set a flag so the main loop can
// do something about it:
if (inputString[countInput] == 0x16) {
stringComplete = true;
}
countInput++;
}
}