webusb / arduino

WebUSB demos running on Arduino (and compatible) hardware.
566 stars 152 forks source link

transferIn not triggered until webpage send something #24

Closed DeqingSun closed 7 years ago

DeqingSun commented 7 years ago

Hi, I was testing webUSB but I got no success in getting data from Arduino. So I made a simple test with console demo. I let Arduino print ok every second. However, the transferIn in web does not trigger until I type something in console.

Tested on Mac OS 10.10.5

void loop() {
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 1000) {
    previousMillis = currentMillis;
    Serial.println("OK");
  }
  if (Serial && Serial.available()) {
    int byte = Serial.read();
    Serial.write(byte);
    if (byte == 'H') {
      Serial.write("\r\nTurning LED on.");
      digitalWrite(ledPin, HIGH);
    } else if (byte == 'L') {
      Serial.write("\r\nTurning LED off.");
      digitalWrite(ledPin, LOW);
    }
    Serial.write("\r\n> ");
    Serial.flush();
  }
}
DeqingSun commented 7 years ago
Serial.println("OK");
Serial.flush();

Flush is required to make the library work properly.