6 years, 6 months ago.

Getc doesn't work

I use the carte NUCLEO-F303K8, but the fonction getc doesn't work. How can I do?

Ex1(Not work): for(uint8_t i = 0; i < 9 ; i++) { if(sv.readable() == 1) rxBuf[i] = getc(sv); } Ex1(Not work): for(uint8_t i = 0; i < 9 ; i++) { while(sv.readable() == 0); rxBuf[i] = sv.getc(); } Thank you!

1 Answer

6 years, 6 months ago.

Hello Shanglin,

The code below worked fine with my NUCLEO-F103RB board. Hopefully will do also with your NUCLEO-F303K8.

#include "mbed.h"

Serial   pc(USBTX, USBRX);
char     rxBuf[10];
uint8_t  i = 0;        

int main()
{
    while(1) {
        for(i = 0; i < 9 ; i++) {
            while(pc.readable() == 0);
            rxBuf[i] = pc.getc();
        }
        
        rxBuf[i] = '\0';    // terminates the char array to allow printing with string format
        pc.printf("%s\r\n", rxBuf);        
    }
}

NOTE: To test it, send chars '1', '2', ..., '9' one by one over the connected PC's serial terminal. Once the last char has been sent the string '123456789' should be displayed on the serial terminal.