Nucleo to nucleo serial Communication via TX and RX pins

Hi All,
I am using two Nucleo board (F746ZG and F746ZG) both are same to simulate a Serial communication. The HW Connection is as follows

MAIN-NUCLEO (STM32-F746ZG) and SUB-NUCLEO(STM32-F746ZG)
F746ZG pin-PC_12(TX) —>F746ZG pin-PG_9 (RX)
F746ZG pin-PD_2(RX) —>F746ZG pin-PG_14(TX)

I want to send a set of character(String) from main nucleo to sub nucleo and After sent the Full String some done Message will send from sub nucleo to main nucleo. pls see my Codes.

// main Nucleo 
#include "mbed.h"
#include"string.h"

Serial pc(USBTX, USBRX);           // tx, rx
Serial receiver(PC_12,PD_2);       // NUCLEO-F746zg
char resStr[] = "ABCDEFGH#";


int main()
{
    pc.baud(115200);
    receiver.baud(115200);
    pc.printf("Starting...\r\n");           // Send to PC's serial terminal
    int ch;
    int i=0;
    while (1) 
    {
        pc.printf("\n Select The case :"); 
        scanf("%d",&ch);
        switch(ch)
            {
                case 1:
                {
                    printf("\n nucleo to nucleo Communication");
                    while(resStr[i] != '#' )
                        { 
                            if(receiver.writeable())
                                {
                                    receiver.putc(resStr[i]);         //sending to Sub Nucleo F746ZG
                                    printf("\n send=%c",resStr[i]);
                                    wait_ms(100);
                                }    
                            i++;
                        } 
                    printf("\nsending done");
                }
                break;
           }
   }
}
// Sub Nucleo
#include "mbed.h"
#include"string.h"

Serial pc(USBTX, USBRX); // tx, rx
Serial receiver(PG_14,PG_9); // NUCLEO-F746ZG

char received[50] = "\0"; // buffer with a size with Null
char c='\0'; 
int main()
{
    pc.baud(115200);
    receiver.baud(115200);
    pc.printf("Ready\n\r");
    int i=0;
    while(1)
        {
            //while(receiver.readable() < '\0')
           while(receiver.readable() )
                {
                    c = receiver.getc(); // Receive from main NUCLEO-F746zg
                    received[i]=c;
                    printf("Received=%c",received[i]);
                    i++;
                    c='\0'; // make it c null for ready to receive the next Char 
                    wait_ms(100);
                }

            for(int j=0;j!=50;j++) 
                {
                    received[j] = '\0';   // make it Received String Null for Next Receive Data
                    wait_ms(100);
                }   
               
        }
        
    }

Hello,

thank you for code formatting but not understand why new topic again…

  • pc.printf and printf have same efect would be good to use just one.
  • scanf is also on same uart
  • variable i has to be increase after sucesful send and not in the loop because the loop can be much faster than UART will be available again send something and that can cause zou will send nothing.
// main Nucleo 
#include "mbed.h"
#include"string.h"

Serial pc(USBTX, USBRX);           // tx, rx
Serial receiver(PC_12,PD_2);       // NUCLEO-F746zg
char resStr[] = "ABCDEFGH#";
DigitalOut led(LED1);


int main()
{
    pc.baud(115200);
    receiver.baud(115200);
    pc.printf("Starting...\r\n");           // Send to PC's serial terminal
    int ch;
    int i=0;
    while (1) 
    {
        pc.printf("Select The case :\n"); 
        pc.scanf("%d",&ch);
        pc.printf("Perform command %d \n", ch); 
        switch(ch)
        {
            case 1:
            {
                pc.printf("nucleo to nucleo Communication\n");
                while(resStr[i] != '#' )
                { 
                    if(receiver.writeable())
                    {
                        led = !led;
                        receiver.putc(resStr[i++]);         //sending to Sub Nucleo F746ZG
                        wait_ms(100);
                    }    
                } 
                pc.printf("send=%s\n",resStr);
                pc.printf("sending done\n");
            }
            break;
        }
    }
}
// Sub Nucleo
#include "mbed.h"
#include"string.h"

Serial pc(USBTX, USBRX); // tx, rx
Serial receiver(PG_14,PG_9); // NUCLEO-F746ZG
DigitalOut led(LED1);

char received[50] = "\0"; // buffer with a size with Null
char c='\0'; 

bool msgComplete = false;
int main()
{
    pc.baud(115200);
    receiver.baud(115200);
    pc.printf("Ready\n\r");
    int i=0;
    while(1)
    {
        //while(receiver.readable() < '\0')
        while(receiver.readable())
        {
            led = !led; //signal for something is readed
            c = receiver.getc(); // Receive from main NUCLEO-F746zg
            received[i++]=c; // place char to buffer and increase counter 
            if(c == '#') //check msg is complete
            {
                received[i]='\0'; // this will help to mark end of string because buffer is much bigger than a single msg and is not necessary to clean whole buffer
                msgComplete = true; //flag msg is complete
            }
        }
        if(msgComplete)
        {
            pc.printf("Received=%s\n",received);   // printf everithing until \0
            msgComplete  = false;  // clean flag        
        }   
        wait_ms(10);
    }
}

Please do not write “it is not working”, rather write console output from both sides and check Led indication.

BR, Jan

working fine i add some delay before variable increment, thanks very much to johnnyk…