will timer interrupts happen in middle of Serial.read()?

I'm controlling steppers, so I need the timing on those to be reliable. It'd be convenient for the rest of my code to use Serial.readStringUntil('\n') - will timer interrupts interrupt this sometimes-very-slow (like when \n hasn't been sent yet) function?

Yes. Serial should not delay your interrupts any significant amount.

Note: SoftwareSerial turns off interrupts during the sending and receiving of individual characters. For slow baud rates this causes jitter in Servo outputs.

The serial input basics tutorial shows how to read data as it arrives in a non blocking fashion and it does not use Strings which can cause memory problems.

@johnwasser Thank you!

@groundFungus Memory issues? Do you mean, because they don’t allocate a specific amount of RAM at compile-time? I’ve got quite a bit of free RAM, and my Strings are easily under 50 characters in total at any given time.

Global variables use 698 bytes (34%) of dynamic memory, leav
les. Maximum is 2048 bytes.

void serial_receive() {
    String msg;

    if(Serial.available()) {
        msg = Serial.readStringUntil('\n');
    }

    // Validate and remove opening and closing chars <>
    if(! (msg.startsWith("<") or msg.endsWith(">")) ) {
        return;
    }
    msg = msg.substring(1, msg.length()-2);

    int pos = msg.indexOf(",");
    if(pos == -1) {
        return;
    }
    String key = msg.substring(0, pos);
    String value = msg.substring(pos+1);

    serial_cmd(key, value);
}

Read this page: Evils of Arduino Strings to see why to not use the String class and some advice on using c_stings instead.

Ok, I see, thank you!

It was easier than expected to modify, thanks to that page.

void serial_receive() {
    static char key[8];
    static char value[8];
    static int key_i = -1;
    static int value_i = -1;
    char ch;

    while(Serial.available()) {
        ch = Serial.read();
        if(ch == '<') {
            memset(key, 0, sizeof(key));
            memset(value, 0, sizeof(value));
            key_i = 0;
            value_i = 0;
        } else if(key_i != -1) {
            if(ch == ',') {
                key_i = -1;
            } else {
                key[key_i] = ch;
                key_i++;
            }
        } else if(value_i != -1) {
            if(ch == '>') {
                key_i = -1;
                value_i = -1;
                serial_cmd(key, value);
                return;
            } else {
                value[value_i] = ch;
                value_i++;
            }
        }
    }
}