Split string with comma as delimitation in Arduino Serial.read ()

1

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

A,0 or R,30 or L,15

I have to split and get separate values, one Character and another Integer .

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);
  }
}
    
asked by anonymous 26.02.2018 / 01:44

1 answer

0

You can use the Serial.parseInt function and then get to 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);
  }
}
    
26.02.2018 / 04:59