Hi mbed fans,
I try to read string from serial when it comes.
I tried a lot of variant code blocks found here, and I still can not find out way how to interrupt main thread to catch income message via serial line.
There must be the way.
can someone show me (and others too) simple way how it do it.
thank you.
indent preformatted text by 4 spaces
#include "mbed.h"
#include "platform/mbed_thread.h"
#define STRING_SIZE 20
RawSerial pc(USBTX,USBRX, 9600);
DigitalOut led(LED2);
char message[STRING_SIZE];
bool flag_serial_catch = false;
void read_serial()
{ int string_possition;
char chr;
flag_serial_catch = true;
do
{
chr = pc.getc();
pc.putc(chr); // it should be for clear Serrial::Irq
message[string_possition] = chr;
string_possition++;
}
while (chr != '\n'); // end of line character
}
int main()
{
pc.attach(&read_serial, Serial::RxIrq);
while(1)
{
wait(5);
led = !led;
if (flag_serial_catch)
{
pc.printf("caught message is : %s\r\n", message);
flag_serial_catch = false;
}
else
pc.printf("Noting on serial port. \r\n");
}
}