CmdArduino icon indicating copy to clipboard operation
CmdArduino copied to clipboard

Sending inadvertent '\r\n' (Carriage Return & Line Feed) breaks CmdArduino.

Open DarrellStuff opened this issue 9 years ago • 1 comments

Hello,

I was very happy to find a simple serial command line interface for my Arduino sketches. Thank you for writing this!

Initially I was unable to get "cmd_line_ex1_hello.pde" working until I figured out that I was terminating the command strings sent through the terminal program with \r\n instead of just \r.

I don't recall seeing any notes about using only \r for string terminations in the comments, and I didn't realize that \r\n was corrupting the input buffer for every command sent after the first command sent. I found the problem while reviewing the "switch (c)" code of Cmd.cpp and verifying that your screen capture example showing the use of "Carriage return" terminations as see on flicker at:

https://www.flickr.com/photos/22874071@N05/10473274214/in/photostream/

To prevent an accidental \r\n from breaking the input routine, I added a filter to remove all incoming \n characters. I don't know how often \n might be a valid input character that is being incorrectly filtered out, but the likelihood of \r\n being sent accidentally as I move between different terminal programs highly likely. Someone needing the \n character could just comment out the filtering case.

I implemented the \n filter by making a copy of the case '\b' functionality for '\n' instead of '\b', and not echoing the \n character back to the terminal. Below is the full modified switch statement with the additional case statement added.

I am not familiar with pulling/pushing code changes through GitHub directly, so I opened this as an issue instead.

Regards,

Darrell


switch (c)
{
case '\r':
    // terminate the msg and reset the msg ptr. then send
    // it to the handler for processing.
    *msg_ptr = '\0';
    Serial.print("\r\n");
    cmd_parse((char *)msg);
    msg_ptr = msg;
    break;

case '\b':
    // backspace 
    Serial.print(c);
    if (msg_ptr > msg)
    {
        msg_ptr--;
    }
    break;

case '\n':    // This case added by d. on 6/5/2015. 
    // remove linefeeds so receiving \r\n won't corrupt buffer for next command 
    // Serial.print(c);     // Do not echo \n back to terminal 
    if (msg_ptr > msg)
    {
        msg_ptr--;
    }
    break;

default:
    // normal character entered. add it to the buffer
    Serial.print(c);
    *msg_ptr++ = c;
    break;
}

DarrellStuff avatar Jun 06 '15 04:06 DarrellStuff

Thank that was very useful. Please update the library with the suggestion :-)

samuelcarreira avatar Apr 10 '20 17:04 samuelcarreira