Sending a string over serial connection

I am trying to send a string over the serial port. I am very close but for some reason I get extra junk attached to front of the string. I could chop it off with a substring() but I would rather understand what it is and why its there. Thanks for any help.

const byte MAXIMUM_INPUT_LENGTH = 15;
const char PKG_START = '<';
const char PKG_END = '>';

String input = "";
char currentInput = 0;
byte currentIndex = 0;

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

void loop() { 
 input = "";
 currentIndex = 0;
 if (Serial.available())
 {
  currentInput = Serial.read();
  
  if (currentInput != PKG_START)
  {
     Serial.println("");
     Serial.println("<2>");
     Serial.println("Started reading mid package, waiting for new start");
     while(currentInput != PKG_START)
     {
      currentInput = char(Serial.read());
     }
  }

  currentInput = char(Serial.read());
  
  while (currentInput != PKG_END and currentIndex < MAXIMUM_INPUT_LENGTH )
  
  {
    currentIndex += 1;
    input += currentInput;
    currentInput = char(Serial.read());

    Serial.print("current input is:");
    Serial.println(currentInput);
  }

  Serial.print("full input is:");
  Serial.println(input);
 }
}

expected and actual input output:

input: <abc>

expected output:

current input is:a
current input is:b
current input is:c
full input is:abc

actual output:

current input is:⸮
current input is:⸮
current input is:⸮
current input is:a
current input is:b
current input is:c
current input is:>
full input is:⸮⸮⸮⸮⸮abc

you are converting your ascii input into ascii input.

currentInput = char(Serial.read());

should be like your first read throughout all your code

currentInput = Serial.read();

The value of Serial.read() is already an ascii character.

blh64:
you are converting your ascii input into ascii input.

currentInput = char(Serial.read());

should be like your first read throughout all your code

currentInput = Serial.read();

The value of Serial.read() is already an ascii character.

Thanks, I tried removing the char()'s but I'm still getting the same output

I get extra junk attached to front of the string. I could chop it off with a substring() but I would rather understand what it is and why its there.

The Arduino is fast, and serial input is slow. You are iterating through the while loop (3 times) with nothing to read and are adding a non printable character to the String. Use Serial.begin(115200) and there is less garbage. Add a small delay() to the while loop to allow the characters to arrive, and you see what you expect.

while (currentInput != PKG_END and currentIndex < MAXIMUM_INPUT_LENGTH )
  {
    delay(5);

Your entire approach can be improved. Don't use the String object , but rather a null terminated character array.

See Robin2's excellent tutorial on methods for receiving Serial data. There is an explicit example for the <> start and end characters.

Serial Input Basics

@OP

You are sending the string (< marks the beginning of string, and > marks the end of string) from the InputBox of the Serial Monitor to the Arduino UNO. The UNO receives the string character-by-character and send them back to the OutputBox of the same Serial Monitor. Please, study the following Flow Chart of Fig-1 and try to write the codes without looking much into the codes that I have given in this post in Step-2.

Hopefully, you will be able to understand that the Flow Chart has very well represented the thoughts that you have behind this data exchange. Let us note that Flow Chart is a powerful tool that helps to visualize pictorially/geometrically the logic embedded within a Programming Problem.

1. Solution of your problem in the form of Flow Chart (tested)
uart-2xy.png
Figure-1: Flow Chart describing data exchange between Serial Monitor and UNO

2. Arduino Codes for the Flow Chart of Step-1 (tested)

byte x;
char myArray[20] = "";
int i = 0;
bool flag1 = LOW;

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

void loop()
{
  if (Serial.available() > 0)
  {
    if (flag1 == HIGH) //< received
    {
      x = Serial.read();
      if (x != '>')
      {
        Serial.print("current input is:");
        Serial.println((char)x);
        myArray[i] = x;
        i++;
      }
      else
      {
        Serial.print("full input is:");
        Serial.println(myArray);
      }
    }
    else
    {
      if (Serial.read() == '<')
      {
        flag1 = HIGH;
      }
    }
  }
}

3. Results of the execution of codes of Step-2
sm-5.png
Figure-2:

uart-2xy.png

sm-5.png