3 years, 5 months ago.

mbed lpc1768 receives only the first 16 characters of a string, why?

I have a c ++ program that sends a string to mbed lpc1768, which receives only the first 16 characters of a string, why?

This is the code to receive the string:

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx

char stringa[50];  //stringa di ingressoi

void dev_recv()
{   
    int j=0;
    while(pc.readable()) {
        
        stringa[j] = pc.getc();
        pc.putc(stringa[j]); 
        j++;

         }
             
        printf(stringa);
         
}

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

A couple things. You are not supposed to use printf inside an interrupt. It is not interrupt safe. Check out the Circular Buffer class. I would handle this by using the ISR to shuffle new char into the CircularBuffer while keeping an eye out for a predefined end of line/end of string char, say '\n'. Set a flag when you find it to indicate you have a new string that needs to be handled. You could handle it in main() just by waiting for flag to be set and do your printf from main().

Rtos threads work well here too, as you could just put the completed CircularBuffer object into a queue and let a dedicated thread handle it.

Any global variable you use inside ISR needs to be declared volatile (stringa in this case).

posted by Graham S. 21 Aug 2017
Be the first to answer this question.