0

J'essaie d'envoyer deux valeurs décimales différentes en série à l'Arduino. Les valeurs qui sont envoyées à l'Arduino sont séparées par une virgule (,):Comment envoyer en série plusieurs valeurs flottantes ou décimales différentes à Arduino?

Par exemple. 1.23,4.56

Mon problème est que lorsque les valeurs sont reçues par le micro-contrôleur Arduino, le code ne semble pas afficher le résultat souhaité.

deux la commande Serial.println vu dans le code ci-dessous les sorties suivantes pour les variables valeur_1 et value_2:

1,20

0,00

4,50

0,00

Alors Ce que je ne comprends pas, c'est pourquoi il y a une valeur '0.00' supplémentaire dans les deux variables.

Merci d'avance.

const int MaxChars = 3; // an int string contains up to 3 digits (3 s.f.) and 
         // is terminated by a 0 to indicate end of string 
char strValue_1[MaxChars+1]; // must be big enough for digits and terminating null 
char strValue_2[MaxChars+1]; // must be big enough for digits and terminating null 
int index_1 = 0;   // the index into the array storing the received digits 
int index_2 = 0;   // the index into the array storing the received digits 
double value_1; 
double value_2; 

void setup() 
{ 
    Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud 
} 

void loop() 
{ 
if(Serial.available()) 
{ 
char ch = Serial.read(); 
if(index_1 < MaxChars && ch >= '.' && ch <= '9') 
{ 
    strValue_1[index_1++] = ch; // add the ASCII character to the array; 

} 
else if (ch == ',') 
{ 
    if(index_2 < MaxChars && ch >= '.' && ch <= '9') 
    { 
     strValue_2[index_2++] = ch; // add the ASCII character to the array; 
    } 
} 
else 
{ 
    // here when buffer full or on the first non digit 
    strValue_1[index_1] = 0;  // terminate the string with a 0 
    strValue_2[index_2] = 0;  // terminate the string with a 0 
    value_1 = atof(strValue_1);  // use atof to convert the string to an float 
    value_2 = atof(strValue_2);  // use atof to convert the string to an float 
    Serial.println(value_1); 
    Serial.println(value_2); 
    index_1 = 0; 
    index_2 = 0; 
} 
} 
} 

Voici la dernière version modifiée du code tel que suggéré par @mactro et @aksonlyaks, je suis toujours incapable d'obtenir les résultats souhaités, d'où je suis ouvert à d'autres suggestions.

À partir de la sortie de courant I reçu pour l'entrée spécifique de 1.23,4.56 pour les variables suivantes sont:

strValue [0]:

1,2

strValue [1]:

1,2

4,5

Valu e_1:

1,20

0,00

valeur_2:

1,20

4,50

Merci à l'avance.

Voici la version la plus récente du Code:

const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '\0' and 
        // is terminated by a 0 to indicate end of string 

const int numberOfFields = 2; //Amount of Data to be stored 
char strValue[numberOfFields][MaxChars+1]; // must be big enough for digits and terminating null 

int index_1 = 0;   // the index into the array storing the received digits 

double value_1; 
double value_2; 

int arrayVal = 0; 

void setup() 
{ 
    Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud 
} 

void loop() 
{ 

if(Serial.available()) 
{ 
    char ch = Serial.read(); 

if (ch == ',') 
{ 
    arrayVal = 1; 

    if(index_1 < MaxChars-1 && ch >= '.' && ch <= '9') 
    { 
     strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array; 
    } 
    if(index_1 == MaxChars - 1) 
    { 
     strValue[arrayVal][index_1++] = '\0'; 
    } 

} 
else if(index_1 < MaxChars-1 && ch >= '.' && ch <= '9') 
{ 
    strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array; 

if(index_1 == MaxChars - 1) 
{ 
    strValue[arrayVal][index_1++] = '\0'; 
} 

} 

else 
{ 

    value_1 = atof(strValue[0]);  // use atof to convert the string to an float 
    value_2 = atof(strValue[1]);  // use atof to convert the string to an float 
    Serial.println(value_1); 
    Serial.println(value_2); 
    index_1 = 0; 
    arrayVal = 0; 
} 
} 
} 

Répondre

0

J'ai fait quelques modifications à votre code et maintenant il imprime ce que vous désirez. Le code modifié est le suivant:

const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '\0' and 
        // is terminated by a 0 to indicate end of string 

const int numberOfFields = 2; //Amount of Data to be stored 
char strValue[numberOfFields][MaxChars+1]; // must be big enough for digits and terminating null 

int index_1 = 0;   // the index into the array storing the received digits 

double value_1; 
double value_2; 

int arrayVal = 0; 

void setup() 
{ 
    Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud 
} 

void loop() 
{ 

    if(Serial.available()) 
    { 
     char ch = Serial.read(); 

     if (ch == ',') 
     { 
      arrayVal = 1; 
      index_1 = 0; // Initialise this to zero for the float value received after ',' 
/* 
      if(index_1 < MaxChars-1 && ch >= '.' && ch <= '9') 
      { 
       strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array; 
      } 
      if(index_1 == MaxChars - 1) 
      { 
       strValue[arrayVal][index_1++] = '\0'; 
      } 
*/ 
     } 
     else if((index_1 < MaxChars + 1) && (ch >= '.' && ch <= '9')) // one float value size including null character is 5 (1.23 size 4) 
     { 
      strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array; 
      if(index_1 == MaxChars)    // When we have recevied the 4 character of float value add NULL character 
      { 
       strValue[arrayVal][index_1++] = '\0'; 
      } 
     }else 
     { 
      value_1 = atof(strValue[0]);  // use atof to convert the string to an float 
      value_2 = atof(strValue[1]);  // use atof to convert the string to an float 
      Serial.println(value_1); 
      Serial.println(value_2); 
      index_1 = 0; 
      arrayVal = 0; 
     } 
    } 
} 

De plus, j'ai fait l'indentation appropriée pour votre code. Faites-moi savoir si cela aide.

Cordialement

+0

Oui ses activités d'impression des valeurs comme on le souhaite maintenant, je vous remercie pour toute l'aide. –

0

Vous n'ajouter quoi que ce soit à strValue_2, parce que

if(index_2 < MaxChars && ch >= '.' && ch <= '9') 
{ 
    strValue_2[index_2++] = ch; // add the ASCII character to the array; 
} 

est exécutée que lorsque ch==','. Lorsque vous recevez une virgule, vous devez définir un indicateur qui signale à un code d'écrire d'autres caractères au strValue_2 au lieu de strValue_1.Ou vous pourriez avoir un tableau de chaînes comme char strValues[2][MaxChars+1] et le changement et l'index d'un élément auquel vous écrivez strValues[stringNumber][index++].

+0

je lui ai donné votre méthode d'utiliser un tableau de chaînes d'essayer et je l'ai ajouté le code à ma question que vous pouvez voir ci-dessus. Cependant, je suis toujours incapable de résoudre le problème, pourriez-vous me donner plus d'indices sur ce que je fais mal cette fois avec le nouveau code. Je pense que la question est de mettre le drapeau, peut-être pourriez-vous proposer un moyen de mieux faire. Merci beaucoup pour vos commentaires. –

+0

@bat_wave Quels résultats obtenez-vous maintenant? Essayez 'Serial.println (strValue [0])', vous saurez ainsi ce qu'il y a dans le tampon. – mactro

+0

J'ai essayé 'Serial.println (strValue [0])'. Je saisis les valeurs: 1.23,4.56. Et la valeur dont la sortie est: 1.2 1.2 Je ne suis pas sûr de ce qui cause la duplication. Et quand je fais la même chose mais avec 'Serial.println (strValue [1])' pour la même entrée de 1.23,4.56. Je reçois la sortie: 4.5 Après la conversion de atof, valeur_1 donne: 1,20 1,20 –