Case

If the case has one or two words the value of X (Xpos) is not shown.
When the case has three digits the value of X is shown.
Why does it happen?

G302 X9.088917 Y10.215494 I-0.222468 J0.914225

X= 9.09
Y = 10.22
I = -0.22
J = 0.91

G0 X9.088917 Y10.215494 I-0.222468 J0.914225

X= 0
Y = 10.22
I = -0.22
J = 0.91

G30 X9.088917 Y10.215494 I-0.222468 J0.914225

X= 0
Y = 10.22
I = -0.22
J = 0.91

G002 X9.088917 Y10.215494 I-0.222468 J0.914225

X= 0
Y = 10.22
I = -0.22
J = 0.91

This code work:
void processIncomingLine( char* line, int charNB ) {
int currentIndex = 0;
char buffer[ 64 ]; // Hope that 64 is enough for 1 parameter

while( currentIndex < charNB ) {
switch ( line[ currentIndex++ ] ) { // Select command, if any
case ‘G’:
buffer[0] = line[ currentIndex++ ]; // /!\ Funciona apenas com comandos de 3 dígitos
buffer[1] = line[ currentIndex++ ];
buffer[2] = line[ currentIndex++ ];
buffer[3] = ‘\0’;
switch ( atoi( buffer ) ){
case 300:
case 301:
{
char* indexX = strchr( line+currentIndex, ‘X’ );
float Xpos = atof( indexX + 1);
char* indexY = strchr( line+currentIndex, ‘Y’ );
float Ypos = atof( indexY + 1);
Serial.println( Xpos );
Serial.println( Ypos );
move_to(Xpos, Ypos );
break;
}
case 302:
{
char* indexX = strchr( line+currentIndex, ‘X’ );
float Xpos = atof( indexX + 1);
char* indexY = strchr( line+currentIndex, ‘Y’ );
float Ypos = atof( indexY + 1);
char* indexI = strchr( line+currentIndex, ‘I’ );
float Ipos = atof( indexI + 1);
char* indexJ = strchr( line+currentIndex, ‘J’ );
float Jpos = atof( indexJ + 1);
Serial.println( Xpos );
Serial.println( Ypos );
Serial.println( Ipos );
Serial.println( Jpos );
draw_arc_cw(Xpos, Ypos, Ipos, Jpos );
break;
}
case 303:
{
char* indexX = strchr( line+currentIndex, ‘X’ );
float Xpos = atof( indexX + 1);
char* indexY = strchr( line+currentIndex, ‘Y’ );
float Ypos = atof( indexY + 1);
char* indexI = strchr( line+currentIndex, ‘I’ );
float Ipos = atof( indexI + 1);
char* indexJ = strchr( line+currentIndex, ‘J’ );
float Jpos = atof( indexJ + 1);
//Serial.println( Xpos );
//Serial.println( Ypos );
//Serial.println( Ipos );
//Serial.println( Jpos );
draw_arc_ccw(Xpos, Ypos, Ipos, Jpos );
break;
}
}
case ‘M’:
buffer[0] = line[ currentIndex++ ]; // /!\ Funciona apenas com comandos de 3 dígitos
buffer[1] = line[ currentIndex++ ];
buffer[2] = line[ currentIndex++ ];
buffer[3] = ‘\0’;
switch ( atoi( buffer ) ){
case 2:
{
MotoresOFF();
break;
}
case 3:
{
pen_down();
break;
}
case 5:
{
pen_up();
break;
}

}
}
}
}

If the case has one or two words the value of X (Xpos) is not shown.
When the case has three digits the value of X is shown.
Why does it happen?

Why don't you print the string in buffer, and make sure that it holds what you think it holds?

Print the rest of the string, and make sure that it holds what you think it does.

What happens when the data starts with "G302 X"? You collect the '3', the '0', and the '2', and currentIndex points to the X. So, the X is present.

What happens when the data starts with "G30 X"? You collect the '3', the '0', and the ' ', and currentIndex points to the character after the X. So, the X is missing.

What happens when the data starts with "G0 X"? You collect the '0', the ' ', and the 'X', and currentIndex points to the 2nd character after X. So, the X is missing.

You need a much more robust parsing algorithm. You must NOT expect three characters after the G or M.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.