10 years, 1 month ago.

Communication Problem

I am having a big problem at the moment when I try to communicate over serial.

the error is when I start up the mbeds, i need to start them at the same time or they will not communicate with each other. the code below is trying to detect a connection between them by sending a '#' using a master slave relationship. Mbed 1 listens for a # and when it receives it sends one back. Mbed 2 sends it until a # is received then stops (yes i know it doesn't stop as of yet but im trying to get them to speak).

Depending on which mbed starts first will decide if mbed 1 revives a # and mbed 2 doesn't get one back OR nothing is ouputted to the console. The clincher is if i set up a basic program to take char from pc and send it to over the serial xbee, reflect it back and read it into the pc it works without problems.

Mbed 1:

//Declerations - General
Serial pc(USBTX, USBRX); // tx, rx
Serial xbee(p13,p14); //Initalise xbee_lib#
DigitalOut rst(p12);

//Variables
bool linked = false;

//Functions
//Make Link To Controller
void makeLink(){
    char c;
    while(linked == false){
        if(xbee.readable()){
            pc.printf("read");
            c = xbee.getc();
            if(c == '#'){
                pc.printf("#");
            }
        }
    }
}

//Init Function
void init(){
    pc.baud(9600);
    pc.printf(">Program Starting\r\n");
    rst = 0;
    wait_ms(1); 
    rst = 1;
    wait_ms(1);
    wait(1);
    pc.printf(">Communication Ready\r\n");
    
    makeLink();
}

//Main Function
int main() {
    init();
}

Mbed 2:

//Declerations - General
Serial pc(USBTX, USBRX); // tx, rx
Serial xbee(p9,p10); //Initalise xbee_lib
DigitalOut rst(p8);

//Variables
bool linked = false;

//Functions
//Make Link To Controller
void makeLink(){
    while(linked == false){
        xbee.putc('#');
        pc.printf("run");
        wait(1);
        char c;
        if(xbee.readable()){
            pc.printf("read");
            c = xbee.getc();
            if(c == '#'){
                pc.printf("#");
            }
        }
    }
}

//Init Function
void init(){
    pc.baud(9600);
    pc.printf(">Program Starting\r\n");
    rst = 0;
    wait_ms(1); 
    rst = 1;
    wait_ms(1);
    wait(1);
    pc.printf(">Communication Ready\r\n");
    
    makeLink();
}

//Main Function
int main() {
    init();
}

I hope this is clear but if it isnt ill try to explain better.

Thanks, Nick

1 Answer

10 years, 1 month ago.

Mbed 1 is only receiving. It waits for a #, prints it on the PC and waits for the next #.

Mbed 2 is first sending a #, then checks to see if anything has been received, checks that for a #, prints it, sends the next #.

Problem is that Mbed 1 never sends a #...