Weired problem with monitor

Hey, i’m facing something super weird.
have a code that controls motors (doesnt matters for now), using monitor with echo and filter on enter doest return to monitor from parseSerialInput() (it does print “started” and Serial.println(params);), but the same exact code opened in Arduino IDE monitor works perfect and returns what i need.

a help please?!

#include <Arduino.h>
#include <math.h>


#define DRIVER_MICROSTEP 8
#define Y_PITCH_DIAMETER 6
#define XZ_PITCH_DIAMETER 4
#define STEPS_PER_ROTATION 200
#define FORWARD HIGH
#define BACKWARDS LOW
#define MOTOR_SPEED 2000 //on/off sequence. microseconds
#define MOTOR_SPEED_FAST_DEVIDER 10 //fast speed devider

bool doneFlag = 1;
int motor_speed = MOTOR_SPEED;
const int biled = LED_BUILTIN;


//parse serial input command
void parseSerialInput(String inputCommand){
    //convert to lowercase
    if ( isDigit( inputCommand.charAt(0) ) == false ) inputCommand.toLowerCase(); 
    //zeroing using switches

    //setting fast speed
    if ( inputCommand == "fast" )
    {
        motor_speed = MOTOR_SPEED / MOTOR_SPEED_FAST_DEVIDER;
        Serial.println(1);
        digitalWrite(LED_BUILTIN, HIGH);
    }
    //setting normal speed
    if ( inputCommand == "normal" ){
        motor_speed = MOTOR_SPEED;
        Serial.println(1);
        digitalWrite(LED_BUILTIN, LOW);
    }
    if ( isDigit(inputCommand.charAt(0)) || inputCommand.charAt(0) == '-' ){//param - time to wait between no signals (if number sent)


        Serial.println(inputCommand);
    }

}

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

    pinMode(LED_BUILTIN, OUTPUT);
    Serial.println("Started");
}

//main loop reading serial
void loop()
{
    if (Serial.available() > 0){
        String params = Serial.readStringUntil('\n');
        if (params.length() > 0){
            parseSerialInput(params);
            Serial.println(params);
                     
        }
    }
}


Thanks in advance!

Can you post your platformio.ini please, to show the monitor parameters. Thanks.

Cheers,
Norm.


[env:328p]
platform = atmelavr
board = nanoatmega328
framework = arduino
monitor_speed = 115200
monitor_echo = yes
monitor_filters = 
	send_on_enter
	colorize
lib_deps = 
	; teemuatlut/TMCStepper @ ^0.7.3
	; paulstoffregen/AltSoftSerial@^1.4
	; slashdevin/NeoSWSerial@^3.0.5
	; janelia-arduino/TMC2209 @ ^9.0.6

[env:esp8266]
platform = espressif8266
board = d1
monitor_speed = 115200
monitor_filters = 
	send_on_enter
	colorize
lib_deps = 
	; janelia-arduino/TMC2209 @ ^9.0.6
	; ; paulstoffregen/AltSoftSerial@^1.4
	; slashdevin/NeoSWSerial@^3.0.5
framework = arduino

[env:wroom32]
platform = espressif32
framework = arduino
board = upesy_wroom
monitor_speed = 112500
monitor_filters = 
	send_on_enter
	colorize
lib_deps = 
	janelia-arduino/TMC2209 @ ^9.0.6
	; ; paulstoffregen/AltSoftSerial@^1.4
	; slashdevin/NeoSWSerial@^3.0.5

I only have access to an Uno board ATM as I’m supposed to be moving house, and all my other boards are in a box somewhere!

TL;DR

Your problem appears to be caused by the combination of readStringUntil('\n') and the fact that the monitor terminates each line with CRLF.

I added monitor_eol = LF to platformio.ini to get the correct execution. Everything that should work in your example code, now appears to work.

Gory Details

I added some Serial.print() lines to see what was going on:

First, in the loop():

//main loop reading serial
void loop()
{
    if (Serial.available() > 0){
        Serial.println("Serial has input...");          // ND
        String params = Serial.readStringUntil('\n');
        ...

Also, in the parseSerialInput() function:

void parseSerialInput(String inputCommand){
    Serial.print("Parsing inputCommand: '");          // ND
    Serial.print(inputCommand);                       // ND
    Serial.println("'");                              // ND

    //convert to lowercase
    ...

Then I uploaded the [328p] environment to my Uno and, interestingly, I see it is dropping characters:

--- Terminal on /dev/ttyUSB0 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
Started
fred
Serial has input...
'arsing inputCommand: 'fred
fred

It seems to have dropped the ‘P’ from “Parsing” and the trailing single quote from the passed inoutCommand. If I change Serial.readStringUntil() to Serial.readString() the output is correct, but the inputCommand ends up with a trailing “\r\n” (You do know that the default for the monitor is to send CRLF end of lines?)

To fix that problem, I added monitor_eol = LF to the platformio.ini file. Now when I run the code, I see the correct prompts and the code appears to be working correctly now. The builtin LED also comes on for fast mode, and goes off for normal mode.

Fast
Serial has input...
Parsing inputCommand: 'Fast'
1
Fast
Normal
Serial has input...
Parsing inputCommand: 'Normal'
1
Normal
This won't parse
Serial has input...
Parsing inputCommand: 'This won't parse'
This won't parse

I took the liberty of slightly amending your platformio.ini file to take out all the common elements of each different environment that you have configured. This is handy if you ever have to amend the settings as you only need to change it in one place. :wink:

Your platformio.ini looks like this now:

; All the stuff common to all other environments go here.
[env]
framework = arduino
monitor_speed = 115200
monitor_echo = yes
monitor_eol = LF
monitor_filters = 
	send_on_enter
	colorize
    

[env:328p]
platform = atmelavr
;board = nanoatmega328
board = uno
;lib_deps = 
	; teemuatlut/TMCStepper @ ^0.7.3
	; paulstoffregen/AltSoftSerial@^1.4
	; slashdevin/NeoSWSerial@^3.0.5
	; janelia-arduino/TMC2209 @ ^9.0.6

[env:esp8266]
platform = espressif8266
board = d1
;lib_deps = 
	; janelia-arduino/TMC2209 @ ^9.0.6
	; ; paulstoffregen/AltSoftSerial@^1.4
	; slashdevin/NeoSWSerial@^3.0.5

[env:wroom32]
platform = espressif32
board = upesy_wroom
lib_deps = 
	janelia-arduino/TMC2209 @ ^9.0.6
	; ; paulstoffregen/AltSoftSerial@^1.4
	; slashdevin/NeoSWSerial@^3.0.5

And your example code is like this with my changes left in, but marked with my initials for ease of finding to remove:

// Helping out on a problem at:
// https://community.platformio.org/t/weired-problem-with-monitor/36442

#include <Arduino.h>
#include <math.h>


#define DRIVER_MICROSTEP 8
#define Y_PITCH_DIAMETER 6
#define XZ_PITCH_DIAMETER 4
#define STEPS_PER_ROTATION 200
#define FORWARD HIGH
#define BACKWARDS LOW
#define MOTOR_SPEED 2000 //on/off sequence. microseconds
#define MOTOR_SPEED_FAST_DEVIDER 10 //fast speed devider

bool doneFlag = 1;
int motor_speed = MOTOR_SPEED;
const int biled = LED_BUILTIN;


//parse serial input command
void parseSerialInput(String inputCommand){
    Serial.print("Parsing inputCommand: '");          // ND
    Serial.print(inputCommand);                       // ND
    Serial.println("'");                              // ND

    //convert to lowercase
    if ( isDigit( inputCommand.charAt(0) ) == false ) inputCommand.toLowerCase(); 
    //zeroing using switches

    //setting fast speed
    if ( inputCommand == "fast" )
    {
        motor_speed = MOTOR_SPEED / MOTOR_SPEED_FAST_DEVIDER;
        Serial.println(1);
        digitalWrite(LED_BUILTIN, HIGH);
    }
    //setting normal speed
    if ( inputCommand == "normal" ){
        motor_speed = MOTOR_SPEED;
        Serial.println(1);
        digitalWrite(LED_BUILTIN, LOW);
    }
    if ( isDigit(inputCommand.charAt(0)) || inputCommand.charAt(0) == '-' ){//param - time to wait between no signals (if number sent)


        Serial.println(inputCommand);
    }

}

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

    pinMode(LED_BUILTIN, OUTPUT);
    Serial.println("Started");
}

//main loop reading serial
void loop()
{
    if (Serial.available() > 0){
        Serial.println("Serial has input...");          // ND
        String params = Serial.readStringUntil('\n');
        if (params.length() > 0){
            parseSerialInput(params);
            Serial.println(params);                     
        }
    }
}

HTH

Cheers,
Norm.

1 Like

Hey norm, thanks for such deep dive. checking if this works and letting you know.

1 Like