UART rx interrupt in mbed problem

I’m trying to write code using MBED, but, there are some problem.

The thing is that I want to get “character arry”.
If I send MBED board “abcde” , board will get “abcde” and do something.

there is my code.

#include <mbed.h>

char message[200];

void Rx_interrupt() ;

int i;

Serial pc(PA_2,PA_3);

int main() {
pc.baud(115200);
pc.attach(&Rx_interrupt, Serial::RxIrq);
while(1)
{

}
return 0;

}

void Rx_interrupt() {

char a1[6]="abcde";

memset(message,0, sizeof(message));
i=0;
while(pc.readable()){

	
	

	
	message[i]=pc.getc();
	i++;
}

if(!strcmp(a1,message))
{
	pc.printf(message);
	}
	
else
{
	
	pc.printf("FAIL");
}

}

============================================
this code is that I made.

if I send “abcde”, board should print “abcde” . <—my purpose

but, board print “FAIL” only 1time. what should I do?

I think that there have some problem in “pc.getc()”

  • you must not use printf in an ISR. Toggle a LED or use queues instead, there is an example in the documentation
  • the while() loop will not deliver the string in one peace, you will get a few chars, then the loop ends. In the next interrupt call, your are clearing your buffer and reading the rest. So, clearing the buffer should be performed when the string is complete or too much garbage was received.