5 years, 8 months ago.

Problem with storing serial interrupt data in buffer...

Hello guys, I want to interface sim900 modem with mbed microcontroller.I can ablr to send the At command without any problem ,but i cant able to read the response correctly using serial interrupt. I had made this receive interrupt as a callback function . After sending appropriate command i cant able to store the data in buffer, i dont know why? .I am receiving only the last arrived data (only single char eg: 'K'). Help me in solving this problem...

  1. include "mbed.h" char buffer[255]; void rx_interrupt() { while(sim900.readeable()) { buffer[ptr++] = sim900.getc(); } }

void main() { sim900.attach(&rx_interrupt,serial::rxiqr) sim900.printf("AT\r\n"); interupt function happens here.... }

2 Answers

5 years, 7 months ago.

may be you have to remove the 'while' into the interrupt... because another interrput can be raised during the while...

Try it with :

#include "mbed.h" 

char buffer[255]; 

void rx_interrupt() 
   if (sim900.readeable()) 
       buffer[ptr++] = sim900.getc(); 

you have to use '<<code>>' your code '<</code>>' tags to make your code more 'readable' when you post.

Second thing, it's better to use volatile variables into the interrupt if they are global. So try : volatile char buffer[255]; and the same for ptr if ptr is not local.

while readable not if readable or you can get nasty things happening if some other interrupt delays the interrupt service until after 2 bytes have arrived.

But yes ptr should be global and defined as a volatile or the main loop has no way of telling how many bytes are waiting.

posted by Andy A 26 May 2015
5 years, 7 months ago.

From the Handbook (see Ticker)

"No printf, malloc, or new in ISR

In ISR you should avoid any call to bulky library functions. In particular, certain library functions (like printf, malloc and new) are non re-entrant and their behaviour could be corrupted when called from an ISR."

I presume getc() has similar behaviour, however this example uses it in an ISR


This also has some useful information


getc() is fine in an interrupt. It's waiting for a flag in a register and then reading a byte. In fact serial port interrupts would be fairly pointless without being able to use getc(). If it gets called from an ISR while being in the middle of a call in the main code then the main loop may or may not also see that byte of data but that's about all that would happen.

Just avoid something like scanf().

posted by Andy A 26 May 2015