I cannot see where yo declared sFlag in your code. However, remember when setting a var in an ISR and also in main loop code, declare it as volatile:-
volatile bool sFlag;
Also, you do not need to look for \n directly. Have MODSERIAL's autoDetectChar() do it for you.
#include "mbed.h"
#include "MODSERIAL.h"
MODSERIAL pc( USBTX,USBRX );
char buf[100];
volatile bool sFlag;
void modserialCallback( MODSERIAL_IRQ_INFO *info )
{
sFlag = true;
}
int main()
{
sFlag = false;
pc.baud(115200);
pc.autoDetectChar( '\n' );
pc.attach( &modserialCallback, MODSERIAL::RxAutoDetect );
while (1)
{
wait(0.3);
if ( sFlag )
{
sFlag = false;
pc.move( buf, 100 );
pc.printf("buf = <%s>\n", buf);
}
}
}
I haven't tried that, it's "pseudo code" but should give you an idea.
Edit: Note I used MODSERIAL::move() to copy the contents of the RX buffer to buf and then clear the RX buffer. The ::move command comes in two flavours:
::move( char *dst, int max )
which moves until autodetect char is found or buffer empty
::move( char *dst, int max, char inCharStopAt )
which moves until inCharStopAt char is found or buffer empty
Hello
I am trying to convert some custom written buffer code over to the MODSERIAL library and am having some difficulty getting it to work. I have looked through the example programs and pieced together what I think should work but it doesnt seem to work. Below is a snippet of my code showing what I am trying to do. Basically I send serial strings terminated with '\n' to the mbed and the mbed fills a buffer until it sees the '\n' character and then flags to parse the data.
It never prints the "got char" or "Parsed" or anything for that matter.
#include "mbed.h" #include "MODSERIAL.h" MODSERIAL pc(USBTX,USBRX); char buf[100]; void parse(); void rxCallback(MODSERIAL_IRQ_INFO *info); int main() { pc.baud(115200); // pc.attach(&Serialint,Serial::RxIrq); pc.attach(&rxCallback, MODSERIAL::RxIrq); while (1) { wait(0.3); if (sFlag==1) { NVIC_DisableIRQ(UART0_IRQn); //added to fix lockup 9-27-11 pc.printf("got char"); parse(); pc.printf("parsed"); NVIC_EnableIRQ(UART0_IRQn); //added to fix lockup 9-27-11 } } } void rxCallback(MODSERIAL_IRQ_INFO *q) { MODSERIAL *pc = q->serial; // char c = pc->rxGetLastChar(); if ( pc->rxGetLastChar() == '\n') { sFlag=1; } } void parse() { char *cp; cp = buf; while (pc.readable() ) { *cp++ = pc.getc(); } --cp; //move back to CR or LF *cp = '\0'; //terminate buffer //print buffer contents pc.printf("buf = <%s>\n", buf); }