从串行

时间:2019-04-03 14:00:50

标签: arduino serial-port hex

我在arduino上编码a,并且正在与HEX中的其他设备通信。我想知道如何读取他发送给我的数据。

我正在发送一个HEX数据包(这里一切正常,没问题)

//Ask for Data
Serial.write(askData, sizeof(askData));

此后,我将接收数据(十六进制)。我需要存储所有这些以便以后使用。我唯一知道的是它将以“ 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

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

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++;
  }
}