Embedded Freaks..

August 19, 2008

Handling URC (Unsolicited Result Code) in Hayes AT Command

Filed under: programming-tips — Tags: — kunilkuda @ 11:19 am

URC is the message that is sent from the ME (Mobile Equipment, eg. GSM/GPRS/CDMA modem) that’s not the result of the AT command.

Here’s some examples: RING is sent when the modem is called, CONNECTED is sent when the modem answer the call, or +CNMI is sent when modem received new SMS. You can think of URC as modem’s soft interrupt.

So, what’s the problem with the URC ? Unless you haven’t programmed any of application that deals with the modem directly, you will notice that URC sometimes destroy your parser state machine when it fires in middle of executing AT command.

Here’s the illustration of the URC problem:

application>AT+CMGL=4 # List all of SMS inside the ME
ME> RING # Incoming call
ME> OK # There's no SMS inside the ME
ME> RING # Incoming call

The RING between AT+CMGL and OK is happenned because the AT command hasn’t reach the serial port, while the incoming call is received. This may break the AT command parser. But there’s a way to help it.

Turn ON the ECHO (ATE1)

Most of application samples that I’ve seen turn off the ECHO (by sending ATE0). The reason is unknown, but most of them saying to reduce the serial port communication (hence reducing the parser effort). But that is not correct.

If you want to build robust communication, you should turn ON the ECHO. The reason is simple, you can detect the sequence of the command/URC that’s received by the modem. Here’s the actual sequence of the illustration above (viewed from the modem’s echo):


application>AT+CMGL=4 # List all of SMS inside the ME
ME> OK # There's no SMS inside the ME
ME> RING # Incoming call
ME> RING # Incoming call

Most of modems will have this feature activated in the start-up. Otherwise, you have to activated it manually using ATE1 command.

Give Delay for Each AT Command

All of AT commands are ended with OK (or ERROR/+CME ERROR/+CMS ERROR). Once you\’ve received the string, you need to wait (ie. 100ms) before launching another AT command. By this way, you will not crash your AT command with the URC from modem (The modem will guarantee that it will not issue URC while AT command is processed..but it will issue URC once the AT command is finished..That’s why you have to wait).

Use The Special Purpose Pin

This feature is not available in all of modems. But in certain modems, such as Siemens TC35, it has special pin that will be pulled low during the URC. This might be used as the low triggered interrupt for the main MCU/MPU.

I bet this feature is far more useful than sending URC..but since it’s not standard feature, I cannot rely on it.

Advertisements

7 Comments »

  1. The reason many don’t bother with the echo, it seems, is because you *can’t* reliably detect the sequence of command/URC that’s received by the modem when the URC interrupts the echo.

    e.g.
    +CMTI: “SM”,1
    AT+CMG
    +CMTI: “SM”,2
    R=1
    OK

    Interested in your thoughts on recognizing the AT+CMGR=1 in there. Buffer the incomplete echo, process the URC then complete the echo?

    Comment by Mark F — May 2, 2009 @ 12:28 am

  2. Hi Mark,

    I’ve left this part (along with the company) for almost 6-months already, so, I’m sorry if my memory if not so good. But as I remember, the modem won’t fall into that scenario. Once it receive new character from host, the modem will wait until its GUARD TIME is ended (there’s AT command to control the modem’s GUARD TIME), before it sends the URC. So, be assured that the modem won’t interrupt your AT command before you’ve finished.

    Here’s the illustration:
    +CMTI: “SM”,1
    AT+CMG //< Modem will wait until this command finished or GUARD TIME elapsed
    R=1
    // Host waits for 100ms for modem answer
    OK //< Modem response the AT+CMGR
    // Host should wait for other 100ms before sending another command
    // If there’s any URC at modem’s buffer, modem will send it within this 100ms time frame
    +CMTI: “SM”,2

    The main point is this, whoever that receive the character first, must wait until the GUARD TIME is ended or all of the URC/AT command characters are received. To do that, it’s advised to turn on and analyze the echo. So the host can sense who’s sending the character first.

    If the host pushes the AT command while receiving the URC, the modem will hang. I’ve tried this with Telit and Siemens modem before, so I know that it’s not like duplex communication as it’s in your scenario.

    Comment by kunilkuda — May 3, 2009 @ 10:13 pm

  3. My case in the post was something like this:

    uart.send(“AT+CMGL=4\r”);
    while(!uart.isNewlineReceived()); // Wait until newline received in RX
    string response = uart.get();

    // Expecting something like this:
    // +CMTI:”SM”,4…etc
    // OK
    // But I got:
    // RING
    // +CMTI:”SM”, 4…etc
    // OK

    After I turned on the echo, I found that the response was something like:

    // RING
    // AT+CMGL=4
    // +CMTI:”SM”, 4…etc
    // OK

    So the URC (‘RING’) never interrupts my AT command at all.

    Comment by kunilkuda — May 3, 2009 @ 10:35 pm

  4. Thanks for the follow-up.

    I added a 100ms delay following command completions and could no longer reproduce the interrupting URC.

    This *may* just be a coincidence however since the only mention of guard time in the documentation of the Telit module I’m working with concerns the ‘+++’ break. I’ll check what Telit themselves have to say about it…

    Thanks again.

    Comment by Mark F — May 5, 2009 @ 9:28 pm

  5. Isn’t there a way to deactivate all URC? I don’t need them.

    Comment by Michele — September 18, 2009 @ 10:25 pm

  6. Some can be deactivated, some cannot. Refer to your manual..I remember that most of URCs that’re started with ‘+’ sign can be disabled. Others cannot

    Comment by kunilkuda — November 13, 2009 @ 5:45 pm

  7. […] comms with the modem. This is obviously bad for us as it corrupts the data. The following link https://embeddedfreak.wordpress.com/2…es-at-command/ gives recommendations to make sure that you get a response to your AT command before the URC is […]

    Pingback by Unsolicited Result Codes during modem data transfer — May 24, 2012 @ 7:01 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: