9 years, 2 months ago.

printf() and scanf() wireless communication

i have used two xbee module to transmit the data, while the receiver will only get the data in the first second, and after that it only store the number after the decimal point, can anybody help me. In this example. the receiver will get 100.612000 at first, and then it will always only store the 0.612000.

transmitter

#include "mbed.h"
#include "C12832.h"

C12832 lcd(p5, p7, p6, p8, p11);
Serial pc(USBTX, USBRX);
Serial xbee1(p9,p10);
 DigitalOut rst1(p30); //Digital reset for the XBee, 200ns for reset
 DigitalOut myled(LED3);//Create variable for Led 3 on the mbed
 
int main() { 
 
    rst1 = 0; //Set reset pin to 0
    myled = 0;//Set LED3 to 0
    wait_ms(1);//Wait at least one millisecond
    rst1 = 1;//Set reset pin to 1
    wait_ms(1);//Wait another millisecond

     
    while (1) {//Neverending Loop
        
        myled = 1; //Turn Led 3 Off
        float a;
      
        a=100.612;
       
        xbee1.printf("%f",a); //XBee write whatever the PC is sending
       // xbee1.putc(b);
            wait(0.5);
            myled = 0; //Turn Led 3 on for succcessfull communication
            wait(0.5);
            
          
    lcd.cls();
    lcd.locate(0,3);
    lcd.printf("%f",a);
         system("pause"); 
        }
    }

receiver

#include "mbed.h"
#include "C12832.h"

C12832 lcd(p5, p7, p6, p8, p11);
 Serial xbee(p9, p10);
 Serial pc(USBTX, USBRX);
 
 DigitalOut rst1(p30);

 DigitalOut myled2(LED2);

 int main() {
 
     rst1 = 0;   //Set reset pin to 0
     myled2=0;
     wait_ms(1);
     rst1 = 1;   //Set reset pin to 1
     wait_ms(1);
     float X; 

     while (1) {

        if(xbee.readable()){
             
           xbee.scanf("%f", &X);
          //  Y = xbee.getc();
            // wait(1);
             myled2 = 1;   
                
              wait(0.5);
              myled2 = 0;
              wait(0.5);
    lcd.cls();
    lcd.locate(0,3);
    lcd.printf("%f",X);      
    system("pause");
    
      
           }      
         } 
     }

1 Answer

9 years, 1 month ago.

I have never been to involved with what scanf accepts exactly, but I would start by adding a space or '\n' in your printf functions to make it seperate the different values.

what do you mean, can you show me how to correct printf function

posted by qi mao 19 Mar 2015

i tried to add a \n in the printf()function in the first minutes, the data received always changes and finally it is the same.

posted by qi mao 19 Mar 2015