0

为了衡量 Nano 33 BLE Sense 的 USB CDC 虚拟串行 COM 的字节/秒数据传输能力,我试图从Arduino 论坛上的一篇较早的帖子中运行此代码,该帖子最初用于 Leonardo(它也有原生 USB,如纳米):

const unsigned long NUMERATOR = 1000000000;

void setup(){
  Serial.begin(115200); //Does nothing on the nano 33 ble sense.
  while (!Serial); //Wait for serial port to connect. Needed for native USB on nano 33 ble sense.
  Serial.println("Begin printing.");
}
  
void loop(){
  Serial.println("Enter loop.");
  
  unsigned long startClock = micros();
  for (int i = 1000; i > 0; i--) {
    Serial.write('.'); //Non-blocking so wait until complete using Serial.flush() directly after...
    Serial.flush(); //Blocks forever on nano 33 ble sense.
  }
  unsigned long endClock = micros();
  
  uint32_t bytesPerSecond = NUMERATOR / (endClock-startClock);
  Serial.println("");
  Serial.print(bytesPerSecond);
  Serial.println(" bytes/second");

  while(1);
}

顺便说一句:当这段代码在 Leonardo 上运行时,据报道他们测量到 39258 字节/秒。我什么也没得到,因为我永远无法逃脱 for 循环!

我认为问题在于用于等待 Serial.write('.') 完成的 Serial.flush() 行。它似乎永远阻塞而不是短时间阻塞。任何人都知道我可以在 Nano 33 BLE Sense 上使用的 Serial.flush() 替代方法或其他测试数据传输速率的策略吗?

4

0 回答 0