6 years, 5 months ago.

Serial Recieve Trouble

Hi all,

I want to make a simple Serial program using two serial port. But I can't get an expected value which I sent to enc1 from another CPU.

When I sent 1 byte to enc1, enc1.readable() became 1. This is expected behavior. But it never became 0 after enc1.getc(). Further, the returned value of enc1.getc() was always 0xFF.

Also I tried to interrupt Serial recieve. When I sent 1 byte, interrupt was occurred. After enc1.getc(), interrupt was never ended.

I am using NUCLEO-767ZI. Please advice me. Thank you.

include the mbed library with this snippet

#include "mbed.h"
#include "Serial.h"
#include <stdio.h>

// UART
Serial enc1(PD_5, PD_6);
Serial pc(USBTX, USBRX);

void test() {
    for (;;) {
        if (enc1.readable() == 1) {
            printf("%c\r\n",  enc1.getc());
        }
}

int main() {
    pc.baud(921600);
    enc1.baud(9600);

    for (;;) {
        test();
    }
}

1 Answer

6 years, 5 months ago.

Hello Yuya,

Maybe the test described below could help you to fix the issue. I had the following configuration:

  • NUCLEO-F103RB as board #1 sending one byte (character 'A') each second over serial line (pin D8 of board #1 was connected to pin PD_6 of board #2):

#include "mbed.h"

DigitalOut  led1(LED1);
Serial      serial(D8, D2);

int main() {
    while(1) {
        serial.printf("A");
        led1 = !led1;
        wait(1.0);
    }
}
  • NUCLEO-F767ZI as board #2 receiving data over serial line:

serial

#include "mbed.h"
#include "Serial.h"
#include <stdio.h>
 
// UART
Serial enc1(PD_5, PD_6);
Serial pc(USBTX, USBRX);
 
void test() {
    for (;;) {
        if (enc1.readable() == 1) {
            pc.printf("%c\r\n",  enc1.getc());
        }
        else
            pc.printf("0");
            
        wait_ms(200);
    }
}
 
int main() {
    pc.baud(230400);
    enc1.baud(9600);
 
    for (;;) {
        test();
    }
}

The serial terminal of connected PC was correctly displaying:
0000A
0000A
0000A
...

NOTE: In your snippet, pc's serial baud rate is set 921600 bps in the main function, but you call the global printf function in the test function which was set to 9600 bps in mbed library.

Accepted Answer