Two Nucleo Serial Communication via TX and RX and vice versa

Hi All,
I am using two Nucleo board (F401RE and F429ZI) to simulate a Serial communication. The HW Connection is as follows
F401RE (TX)—>F429ZI (RX)
F401RE (RX)—>F429ZI (TX)

I want to send a character from board1 to board2 and at the same time from board2 to board 1. but i cann’t do this automatically , could you have some ideas? pls see my Code snip below .

Code for F401RE

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
Serial Receiver(PB_6, PA_10);//F401RE
DigitalOut myled(LED1);
const uint8_t ResStr[9]="ABCDEFGH";

int main ()
{
    pc.baud(115200);
    Receiver.baud(9600);
    pc.printf("Hello Nucleo F402RE!!!\n\r");
    
while (1) {
             
    if(Receiver.readable()) 
        {
            SendData = Receiver.getc();
            Receiver.putc(SendData);
            //Receiver.printf("\n\r");
            pc.printf("%c",SendData);
            
            myled = 1;
        }
        
   if(Receiver.writeable()) //send  data to NucleoF429zi
        {
            int i;
            for(i=0; i<10; i++)
                {
                    Receiver.putc(ResStr[i]);
                    //pc.putc(ResStr[i]);
                    myled = 1;
                    wait_us(100); 
                    myled = 0;
                    wait_us(100);
                }
        } 
  }
}
cod for F429ZI
#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
Serial Sender(PD_5, PD_6);  // F429ZI
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);

int i;
char data;
char str[15]=" 1234567890 ";

int main() {
   pc.baud(115200);  // set baud rate 9600
   Sender.baud(9600);
   pc.printf("Hello From Nucleo F429ZI\r\n");

   while(1) {
 if(Sender.readable())  //read from Nucleo F401re and put data
   {
       data = Sender.getc();
       Sender.putc( data);
       //pc.putc(data);
       pc.printf("%c",data);
       myled1 = 1;
   }
       
 if(Sender.writeable()) {
     
     for(i=0; i<12; i++)
       {
         Sender.putc(str[i]);
         //pc.putc(str[i]);
         }
         myled2 = 1;
         wait_us(100);
         myled2 = 0;
         wait_us(100);
      
      }
    
 }
}

Thanks you very much!
Nahom

Hello Nahom,

Try this:

Code for the NUCLEO-F401RE:

#include "mbed.h"

Serial      pc(USBTX, USBRX);       // tx, rx
Serial      receiver(PB_6, PA_10);  // NUCLEO-F401RE
DigitalOut  myled(LED1);

int main()
{
    pc.baud(115200);
    receiver.baud(9600);
    pc.printf("Starting...\n\r");       // Send to PC's serial terminal
    receiver.putc('x');                 // Send to NUCLEO-F429ZI (start the ping-pong)
    while (1) {
        while (receiver.readable()) {
            char c = receiver.getc();   // Receive from NUCLEO-F429ZI
            pc.putc(c);                 // Send to PC's serial terminal
            //wait_us(1000 * 1000);     // To slow it down
            receiver.putc(c);           // Send back to NUCLEO-F429ZI
            myled = !myled;
        }
    }
}

Code for the NUCLEO-F429ZI:

#include "mbed.h"

Serial      pc(USBTX, USBRX);       // tx, rx
Serial      receiver(PB_6, PA_10);  // NUCLEO-F429ZI
DigitalOut  myled(LED1);

int main()
{
    pc.baud(115200);
    receiver.baud(9600);
    pc.printf("Starting...\n\r");       // Send to PC's serial terminal
    while (1) {
        while (receiver.readable()) {
            char c = receiver.getc();   // Receive from NUCLEO-F401RE
            pc.putc(c);                 // Send to PC's serial terminal
            receiver.putc(c);           // Send back to NUCLEO-F401RE
            myled = !myled;
        }
    }
}

Hello Hudakz,

Thank you very much! It works.

But when I try to send the below Code I got only one character. Do you have any Suggestion that I send and receive both direction the full message(byte)?

char str[11]=" 1234567890 ";// send data from F429ZI
char ResStr[9]="ABCDEFGH";// send data from F401RE

Thank you very much for your time and help!

Nahom

I’m afraid I either do not fully understand what you want to do or you want something what is impossible to achieve.
If the F401 sends ABCDEFGH to the F429ZI then the F429ZI can either send that string back to the F401 or to send the 1234567890 string to the F401. It cannot send both strings at the same time to the F401.

Thanks Hudakz, you are right it cannot send F401 both Strings at the same time.

But I wannt to send continousely ABCDEFGH from F401to the F429ZI then the F429ZI reply as conformation and send the 1234567890 string to the F401.

This ist what I want to achieve.

Thanks.

Nahom

Hello Nahom,

Try the following.

Code for the NUCLEO-F401RE:

#include "mbed.h"

Serial      pc(USBTX, USBRX);           // tx, rx
Serial      receiver(PB_6, PA_10);      // NUCLEO-F401RE
DigitalOut  myled(LED1);
const char  resStr[] = "ABCDEFGH\r\n";

int main()
{
    pc.baud(115200);
    receiver.baud(9600);
    pc.printf("Starting...\r\n");           // Send to PC's serial terminal
    receiver.printf(resStr);                // Send to NUCLEO-F429ZI
    while (1) {
        while (receiver.readable()) {
            char    c = receiver.getc();    // Receive from NUCLEO-F429ZI

            pc.putc(c);                     // Send to PC's serial terminal
            if (c == '\n') {                // A complete message (line) received from the NUCLEO-F429ZI
                //wait_us(1000 * 1000);     // To slow it down
                receiver.printf(resStr);    // Send message to NUCLEO-F429ZI
            }
            myled = !myled;
        }
    }
}

Code for the NUCLEO-F429ZI is identical but :

...
const char  resStr[] = "1234567890\r\n";

...

Thankyou very much Hudakz! It works.

I would recommend to read the serial data first into a 64 byte receive buffer until the buffer is full or readable is false. Then write the bytes to the other serial, between each byte write check for additional incoming data.

For high speed serial communication you may miss receive characters.

There is as well a BufferedSerial library available here on Mbed, however with faster speeds, e.g. 115200 it misses characters or has buffer overwrites.

I finally switch to DMA serial transfers, which is quite a big development tasks and it works only with my STM32L4 chip.

Regards
Helmut