Serial communication between 2 STM32L432KC through mbed studio

I’m trying to use the following codes to establish communication between two boards. However the results are “Not readable” and “Not writeable.” What could be the potential problem?(mbed-os-5.15)

#include “mbed.h”
Serial uart(D1,D0);
int main()
{
char data={‘A’,‘B’};
while(1)
{
if(uart.writeable())
uart.puts(data);
else
printf(“Not writeable.\n”);
}
}

#include “mbed.h”
Serial uart(D1,D0);
int main()
{
char data[100];
while(1)
{
if(uart.readable())
{
uart.gets(data,2);
printf(“%s\n”,data);
}
else
printf(“Not readable.\n”);
}
}

Hello,

please use these 3 symbols ``` before and after your code example. The code will be properly formatted like below.

#include “mbed.h”
Serial uart(D1,D0);
int main()
{
   char data={‘A’,‘B’};
   while(1)
   {
      if(uart.writeable())
         uart.puts(data);
      else
         printf(“Not writeable.\n”);
   }
}

basic mistake what people usually do is wiring. The Wires have to be crossed, TX pin has to be connected to RX and wise versa.

BR, Jan

I’ve checked wiring before . TX-RX,RX-TX,GND-GND.

Try the code for write just simply

#include “mbed.h”
Serial uart(D1,D0);
int main()
{
   char data={‘A’,‘B’};
   while(1)
   {
      uart.puts(data)
      wait(3);
   }
}

I’ve found the problem. The interior of one line is broken… What a joke… But new problem occurs. I change the data to
char data={‘A’,‘B’,‘C’,‘D’,‘E’,‘F’};
The reciever is tested under different parameters.
For: uart.gets(data,6); The result is EABCD. I guess the problem is related to ‘\0’
For: uart.gets(data,7);The result is FABCDE. Still wrong about the order.
How do I fix it?

Is good to have a starting and/or ending delimiter (example !mystring\n) and make a logic around. Instead of gets use getc in a loop, colect byte per byte into a buffer and check these delimiters until the right one comes.
I usually use getc() until receive \n, then know this is end of message.

BR, Jan