Arduino: Comment stocker des valeurs dans un tableau de char dans une ligne claire/vide un char tableau?

J'essaie d'écrire le code qui va stocker le texte de la commande entre { et } à partir du terminal série. Pour l'instant j'ai:

byte index = 0; //Index into array; where to store the character
char cmdArr[10];

boolean startOfLine = false;
boolean endOfLine = false;

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

void serialEvent()
{
  while (Serial.available())
  {
    char cmd = (char)Serial.read();

    if (cmd == '{')
    { 
      startOfLine = true;
    }

    if (cmd == '}')
    { 
      endOfLine = true;
      //cmdArr[index] = '
byte index = 0; //Index into array; where to store the character
char cmdArr[10];
boolean startOfLine = false;
boolean endOfLine = false;
void setup()
{
Serial.begin(38400);
}
void serialEvent()
{
while (Serial.available())
{
char cmd = (char)Serial.read();
if (cmd == '{')
{ 
startOfLine = true;
}
if (cmd == '}')
{ 
endOfLine = true;
//cmdArr[index] = '\0'; //null terminate the C string; I'm not sure if this is needed
}
if (startOfLine && cmd != '{'  && cmd != '}')
{
cmdArr[index++] = cmd;
}
if (startOfLine && endOfLine)
{
int i;
for (i = 0; i < 10; i++)
{
//Do something with the command
}
startOfLine = false;
endOfLine = false;
break;
}
}
}
void loop()
{ 
}
'; //null terminate the C string; I'm not sure if this is needed } if (startOfLine && cmd != '{' && cmd != '}') { cmdArr[index++] = cmd; } if (startOfLine && endOfLine) { int i; for (i = 0; i < 10; i++) { //Do something with the command } startOfLine = false; endOfLine = false; break; } } } void loop() { }

Je suis capable de parcourir cmdArr imprimer le tableau de valeurs. Actuellement, par exemple, la commande est prise en stockées comme ceci: char cmdArr[10] = {'p', 'h', 'c', '\0'}; Est-il possible de stocker la commande de type char cmdArr[10] = {"ssp"};? Aussi, comment pourrais-je effacer/vide qui commande de se préparer pour la prochaine commande?

Nouveau code qui semble être au travail. J'ai eu recours à l'aide d'une Chaîne, car il semblait que l'approche la plus simple:

String cmdData; //Store the complete command on one line to send to sensor board.

boolean startOfLine = false;
boolean endOfLine = false;

void setup()
{
  Serial.begin(38400);
  Serial3.begin(38400);
}

void serialEvent()
{
  while (Serial.available())
  {
    char cmd = (char)Serial.read();

    if (cmd == '{')
    { 
      startOfLine = true;
    }

    if (cmd == '}')
    { 
      endOfLine = true;
    }

    if (startOfLine && cmd != '{'  && cmd != '}')
    {
      //Serial.print("Send command");
      cmdData += cmd;
    }

    if (startOfLine && endOfLine)
    { 
      startOfLine = false;
      endOfLine = false;

      cmdData += '\r';

      Serial3.print(cmdData);

      cmdData = "";
    }
  }
}

void serialEvent3()
{
  char cmd3 = (char)Serial3.read();
  Serial.print(cmd3);
}

void loop()
{ 
}
  • cmdArr[10] = "phc\0" peut travailler. En outre, cmdArr peut être utilisée de la même manière que &(cmdArr[0]); les deux sont de type char* qui est aussi proche de "réel", les chaînes que vous aurez à C. de travail sur les Opérations sur des chaînes de caractères, comme strcmp(), faudra que char* que l'entrée représentant un 'string' variable.
InformationsquelleAutor dottedquad | 2012-06-04