'Wire Slave Receiver' Arduino-Code 中的receiveEvent 是否只调用了一次?

Is the receiveEvent in the 'Wire Slave Receiver' Arduino-Code only called once?

我正在尝试通过读取其 i2c 接口并将其连接到我的 Arduino ATMega2560 来从微控制器读取数据。如果收到消息,它们将打印到串行(Arduino 在我的 PC-COM 端口上)

我的问题是,即使 i2c 正在循环发送数据,我的串行接口上​​只显示第一条消息(已正确接收!),所有以后的消息都不会从 Arduino 发送。我还让 Arduino 将消息循环发送到我的文件,这很有效。所以我想它一定是这个 receiveEvent() 的东西,在 Arduino 代码中用于这个目的。

代码来自Arduino 'slave_receiver'-示例,我只是更改了地址。

// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(0x04);             // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(115200);           // start serial for output
}

void loop() {
  delay(1);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
}

好的,显然我并没有真正使用 arduino-library 中的示例。这里重要的是在 while (1 < Wire.available()) 退出后再读取一个字节。然后消息从 i2c 缓冲区“完全清空”,receiveEvent 可以再次调用。

我认为

while (Wire.available()) 
  { 
    // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }

本来是一个更好的例子,这就是我现在实现它的方式,它做了它应该做的事情。