Bonjour à tous,
Je travaille actuellement avec 2 XBEE : master et slave.
J'envoie une trame avec le master que le slave devra lire et afficher tout simplement.
La trame envoyée avec le master est construite avec XCTU.
Vous pouvez trouver le détail de cette construction dans le fichier joint : "Frame_XCTU".
Dans cette trame j'envoie donc un "Strt" qui doit être détecté par le slave.
Pour cela je stocke la trame reçue au fur et à mesure dans un String.
Puis je parcours ce String en cherchant "Strt" avec la fonction indexOf.
Quand j'affiche la trame reçue et stockée, je retrouve bien mon "Strt".
En revanche en utilisant indexOf, la fonction me retourne "-1", elle ne trouve pas le mot dans la trame.
Vous pouvez également trouver mes résultats affichés dans la pièce jointe : "Serial_Results".
J'ai l'impression que cela est lié à la partie RF Data dans la construction de ma trame (voir Frame_XCTU).
Est-ce que ce serait lié au format ? Une conversion à faire ?
Voici mon code :
#include <SoftwareSerial.h>
#define RXPIN 0 // Arduino Pin for Reception
#define TXPIN 1 // Arduino Pin for Transmission
SoftwareSerial xbee(RXPIN,TXPIN); // link virtual serial between xbee and arduino
/* Communication */
int i,found ;
String frame = ""; //sent to the other XBEE
String state_start=""; //for start
String received=""; //received from the other XBEE
char c; //byte received
void setup() {
Serial.begin(9600); //opening serial with 9600 baud
/* XBee Initialisation */
while(!Serial)
{ ; } //waiting for Serial Port
xbee.begin(9600); // opening serial xbee with 9600 baud
Serial.println("Arduino is ready"); //serial link activated
}
void loop() {
//Waiting for the Start of Pi
if(state_start=="Strt"){
Serial.println("Start received ! ");
Serial.println("Data ready to be sent !");
}
else {
state_start="";
received="";
xbee.flush(); //clear out
c=xbee.read();
//Start Delimiter Detected
if (c==0x7E) {
received.concat(c);
for (i=0;i<16;i++) {
c=xbee.read();
Serial.println(c);
Serial.println(c,HEX);
received.concat(c);
}
Serial.println(received);
found = received.indexOf("Strt"); //detect "Send" and return index
Serial.print("With indexOf : ");
Serial.println(found);
if (found != -1) {
state_start="Strt"; }
}
} }