Split string with comma as delimiting in Arduino’s Serial.read()

Asked

Viewed 450 times

1

I’m developing an application in C# with Visual Studio that sends a character to the Arduino only now I want to send a string with this structure character, number as in the example below:

A,0 or R,30 or L,15

I have to perform the split and take the separate values, a Character and another Whole.

This is the Arduino code I have so far:

void loop() {

  if (Serial.available()) 
  {  
    caractere = Serial.read();  
  }

  /* AQUI REALIZO O SPLIT */

  if( caractere == 'R')
  {
    Serial.println(caractere);
    giraHorario(VALOR INTEIRO AQUI);
  }
  else if( caractere == 'L')
  {
    Serial.println(caractere);
    giraAntiHorario(VALOR INTEIRO AQUI);
  }
  else if(caractere == 'A')
  {
    Serial.println(caractere);
    giraAlternado(VALOR INTEIRO AQUI);
  }
}

1 answer

1

You can use the function Serial.parseInt and then get into it:

void gira(char direcao, long valor) {
  if (caractere == 'R') {
    giraHorario(valor);
  } else if (caractere == 'L') {
    giraAntiHorario(valor);
  } else if (caractere == 'A') {
    giraAlternado(valor);
  }
}

void loop() {
  if (Serial.available()) {  
    char caractere = Serial.read();
    Serial.read(); /* Para ler a vírgula. */
    long valor = Serial.parseInt();
    Serial.println(caractere);
    Serial.println(valor);
    gira(caractere, valor);
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.