Handle incoming triggers (SMS, Call) during another command (get GPS)in progress

Hi,

My question is: how to handle the serial buffer when there is an incoming SMS (+CNMI: ....) when previously I send an AT command and I am waiting for the result of my command?

The problem:

gsm.println("AT+CGNSINF")
#here I am waiting for the GNSS sentence to parse
BUT: instead of getting the GNSS, I receive +CNMI: "SM",7 which means a new SMS arrived, so at that time I cannot parse the GPS coordinates.

Do you have any idea, how to handle the non-expected data on serial line? Because I cannot make a state machine that prepared for any possible incoming data...

My hardware is SIMCOM SIM808 and Arduino Nano.

Thanks in advance!

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

Also have a look at the user-input code in Planning and Implementing a Program. I think the logic for sending an AT command and checking for a response will be the same.

...R

The problem is not with receiving data or dealing with serial port... The question was about how to keep the order of command-acknowledge command-acknowledge etc. when there could be unexpected data coming between the send and the acknowledge part. I think this is more likely a programming technical question than actual implementation.

Let me explain:
i want to send HTTP data with standard AT commands (HTTPINIT). There are fixed order of commands, like AT+HTTPINIT -> AT+HTTPPARA=CID,1 -> AT+HTTPPARA="URL","http://internet.com/demo_sim808.php?" etc. and each and every command results with some kind of acknowledgement such as string "OK", or "DOWNLOAD" or "+CGNS" etc. But If I read immediately after AT send, it could happen that instead of getting the result of my command, I get for example something related to an SMS interrupt message, and this messes up my checking process.

I would describe this as a logic problem rather than a programming problem. When you know what needs to be done the programming will be straightforward.

I suspect you need to keep track of the state the system is in, and only move from one state to the next when the correct response is received. This concept came up in another Thread recently. I reckon you will be able to adapt the idea to your problem.

You have not provided enough info for me to suggest the states of your system. The first one might be INIT.

...R

Yes! Absolutely this is a logic problem. The SMS, Incoming call and Bluetooth connect request (since sim808 module has the ability) are all interrupts. So I think the fair way of working would be if any of the following trigger appear the sim808 module should send an interrupt to my arduino, and not messing up the serial line with unpredictable texts. Because then I could finish my actual command, then handle the interrupt, then continue with the last context.

The only thing that I do now, is to disable notifications through serial line when I start sending http requests, and when it is finished I check if I previously got an SMS. Not so fancy, but it might work.

IMHO interrupts are quite unnecessary for a problem like this. Nothing is happening quickly enough or briefly enough to require an interrupt.

Have you spent time carefully studying the links I gave you?

...R

Because I cannot make a state machine that prepared for any possible incoming data...

Then, you'll never be able to solve your problem.

"any possible incoming data" is really stretching it. There are only two devices that can send data - the GPS and the GPRS. If you can't handle two states, you have real problems.

Here's one example.

And another (latest code here).