Serial test sketch

This is a little sketch that I find useful for testing the serial communication on an Arduino-like board. It not only sends data of its own accord (millis() every second), but also echoes back what it receives. That way it will test both transmission and reception, so if one is failing you can easily see.

void setup() {
    Serial.begin(115200);
}

void loop() {
    static uint32_t ts = 0;
    if (millis() - ts >= 1000) {
        ts = millis();
        Serial.println(millis());
    }

    if (Serial.available()) {
        char c = Serial.read();
        Serial.print("Recv: 0x");
        Serial.print((int)c, HEX);
        Serial.print(" ");
        Serial.println(c);
    }
}