Bonjour la communauté, j'essaie de convertir simplement une str en tableau de char à l'aide de toCharArray(), mais il y a un probleme et je le comprend pas.
En résultat j'ai :
Exemple si j'envoie en Serial ZUT< :
nombre de caractere; 4
Z
U
T
<
si l'on recommence str = ZUT<
nombre de caractere; 6
ZUT<
vide
vide
Z
U
T
<
Ma question : Pourquoi 6 alors que j'ai 4 caracteres, cela est correct sur 1 itéraction mais après toujours 6.
Les caracteres sont décalé. Comprenez vous le probleme?
Je vois pas le problème, j'ai cherché dans tous les sens..
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 String inputString = ""; // a String to hold incoming data bool stringComplete = false; // whether the string is complete char tabCommande[10]=""; int i; void setup() { // initialize serial: Serial.begin(9600); inputString.reserve(10); } void loop() { // print the string when a newline arrives: if (stringComplete) { //Affichge str Serial.print("Nombre de caractere:"); Serial.println(inputString.length()); Serial.print("str : "); Serial.println(inputString); //Affiche tableau for(i=0; i<inputString.length(); i++) { Serial.print("case : "); Serial.println(tabCommande[i]); tabCommande[i] = 0; //clear } // clear the string: inputString = ""; stringComplete = false; } } /* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. Multiple bytes of data may be available. */ void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag so the main loop can // fin de caractere if (inChar == '<') { stringComplete = true; //convert in array inputString.toCharArray(tabCommande,inputString.length()+1); } } }
Partager