8 years ago.

Serial communication with a pc on l152re

Hello,

I'm trying to communicate with a pc through serial port on my l152re. It seems to work well when I send messages from the nucleo to the pc, but not the other way around. It never seems to be readable, and it blocks in getc indefinietly. I tried most of the examples I could find on the site, looked through forums but found nothing. I'm working on windows and I've tried using both putty and teraterm, following the instructions to set them up, got the driver from stm (although the nucleo->pc communication worked even before that). An example of what I tried (including this without line 13):

#include "mbed.h"
 
DigitalOut led1(LED1);
Serial pc(USBTX,USBRX);
 
int main() {
    pc.printf("Started: \n");
    led1 = 0;
    while(1){
        led1 = 1;
        wait(10);
        led1 = 0;
        while(pc.readable())
            pc.putc(pc.getc());
        wait(1);
    } 
}

The led changed as expected, but nothing gets echoed back to the pc.

1 Answer

8 years ago.

Your current code only checks once every 11 seconds for any characters to echo. Are you waiting long enough?

Try this code. It will toggle the LED every time it sees a character come in and echo it back without the random delay. If you don't see the text "Started: " on the terminal then check your port settings on the terminal program.

int main() {
    pc.printf("Started: \n");
    led1 = 1;
    wait(10);
    led1 = 0;
    while(true) {
      if (pc.readable()) {
        led1 = !led1;
        pc.putc(pc.getc());
     }
   }
}

Yes, I did wait long enough and I get 'Started: ' in my terminal.. Like I said, the led is changing as it should and it only changes after 10 seconds and I can send messages from the nucleo to the pc fine. Nevertheless, trying your could doesn't echo anything back, or toggle the led after the initial 10 seconds.

posted by Timea Taligás 26 Apr 2016