Parsing Hayes modem AT commands, part 2

See also: Part 1

I know I am supposed to be posting my updated joystick code, which I will do as soon as I have a moment, but I wanted to share some other new stuff first so it can start getting in to the Google indexes.

I have created the first part of some quick-n-dirty code to handle Hayes modem “AT” commands. For a bit of background on the Hayes modem command set, check this Wikipedia entry:

http://en.wikipedia.org/wiki/Hayes_command_set

For some more specific details, check out this Wikibooks entry:

http://en.wikibooks.org/wiki/Serial_Programming/Modems_and_AT_Commands

The Hayes modem company needed a way for the controlling system (user, terminal program, BBS, whatever) to switch the modem from Data Mode (where bytes come in, and bytes go out) to Command Mode, where the modem could be controlled (dial a number, hang up, configure how many rings to answer on).

To get in to Data Mode, a series of three characters is sent – the default being “+++” – with a one second pause (called the “guard time”) before and after them. That way, if the string “+++” appears in normal data being transmitted (like this article), the modem doesn’t respond to it. It would take a pause, then the “+++”, then another pause, and the modem would stop sending and receiving data and present the host system/user with an “OK” prompt and then accept commands.

My code was designed to be used in the main loop() where data would be processed. Code would check for incoming data from the remote device and handle it (such as seeing bytes come in over a modem or serial port), and then look for local input (like the user typing, or a program sending data out). If it was a dumb terminal program, that loop might look like this pseudo code:

loop()
{
    char ch;

    ch = readIncomingData();    // Get byte from remote system.
    Serial.write(ch);           // Print byte to local screen.
    ch = Serial.read()          // Get byte from local input.
         writeOutgoingData(ch); // Print byte to remote system.
}

This, of course, is not working code. A few more lines would be needed to check for valid data and such, but hopefully you get the idea. The loop would continuously run, reading data if it were there, or moving on to the next instruction.

My code is designed to plug in to that loop, after each byte is read from the local user:

loop()
{
    char ch;

    ch = readIncomingData();                      // Get byte from remote system.
    Serial.write(ch);                             // Print byte to local screen.
    ch = Serial.read()                            // Get byte from local input.
         if (cmdModeCheck(ch) == true) cmdMode(); // ADDED
    writeOutgoingData(ch);                        // Print byte to remote system.
}

The idea is to let me code inspect each character that comes from the host system, and then see if there has been a pause of at least “guard time” since the previous character, and if so, start evaluating the characters to see if they are the “+++” escape sequence. If three plusses are seen in a row, it then sees if “guard time” has elapsed and, if so, return a true value to let the calling function know it is time to switch in to Command Mode.

According to the Hayes modem standard, the character used for the escape sequence (“+”) is configurable, as is the guard time. Because of this, I use two global variables for these, so the user can modify them and affect my cmdModeCheck() function:

// Define the escape sequence.
#define ESC_GUARD_TIME 1000 // Seconds required before/after escape sequence.
#define ESC_CHARACTER  '+'  // Default escape character.

// For Command Mode.
unsigned int escGuardTime = ESC_GUARD_TIME; // Delay before/after esc sequence.
static char  escCharacter = ESC_CHARACTER;  // Escape character

I am just using the #defines for default values. A function will be written later that will let the user set them, like setCmdModeCharacter(‘?’) to make it “???” or setCmdModeGuardTime(2) to change the guard time to two seconds.

Not much here so far, so let’s look at the actual function. I will insert some comments

// Magic numbers.
#define ESC_TIMES 3 // Number of escape characters ("+++").

boolean cmdModeCheck(char ch)
{
    static unsigned long escCheckTime = 0; // Next time to check.
    static byte escCounter = 0;            // Number of esc chars seen.

    // If no character is being passed in, we are just doing a check to see if
    // we are in a "wait for end guard time" mode.
    if (ch == 0)
    {
        // See if we are waiting to enter command mode.
        if (escCounter == ESC_TIMES)
        {
            // Yep, we have already found all the escape sequence characters.
            if ((long)(millis() - escCheckTime) >= 0)
            {
                // And the pause has been long enough! We found an escape sequence.
                escCounter = 0;
                escCheckTime = millis() + escGuardTime;

                return true; // Yes, it is time for Command Mode.
            }
        }
    }
    else // if (ch==0)
    {
        // If there has been a pause since the last input character...
        if ((long)(millis() - escCheckTime) >= 0)
        {
            // Check to see if it's an escape byte.
            if (ch == escCharacter)
            {
                // Move to next character to look for.
                escCounter++;

                // Are we out of escape characters to check for?
                if (escCounter >= ESC_TIMES)
                {
                    // Set after delay to signify end of escape sequence.
                    escCheckTime = millis() + escGuardTime;
                }
            }
            else
            {
                // Reset. Not an escape character.
                escCounter = 0;
                escCheckTime = millis() + escGuardTime;
            }
        }
        else
        {
            // Reset. Not an escape character.
            escCounter = 0;
            escCheckTime = millis() + escGuardTime;
        }
    } // end of if (ch==0) else

    return false; // No, it is not time for Command Mode.
}

I should explain that, in order for this function to work, it needs to be called often enough to determine if the guard time (1 second) has elapsed between incoming data. If the input is idle, the function still must be called, but with ch=0 (no data) which is why, at the top, it looks for an incoming character of 0 then it will handle timing stuff.

The way it works is by keeping a count of how many escape characters in a row we have seen so far. I use static variables so they maintain their values between calls to the function. Since I do not use them anywhere outside of this function, I saw no reason to make them global.

“escCounter” starts out at 0. If we get a “ch=0” value, we want to see if we are waiting on the ending guard time. i.e., have we gotten as far that we’ve seen 3 escape characters in a row, and are now looking for a 1 second pause? If escCounter is 3 (ESC_TIMES), we use a nice timing rollover routine from the Arduino.cc Playground to see if it’s been at least guard time with no other characters coming in. If it has been that long, we reset out escCounter back to 0, and make a note of some future time (representing now + guard time in the future). More on this in a moment, but by returning “true” at this point, we have reported back that we have seen “(pause)+++(pause)”.

Ideally, I should have explained the previous code after explaining this next bit first, since it is setting some things up we haven’t discussed yet. Hang in there and things should become clearer…

At the else, we handle a situation if “ch!=0” (i.e., if we were passed in a character from the host system/user). When we initialized this function, there was an “escCheckTime” variable set to 0. In this loop, we test to see if the time difference between now “escCheckTime” to see if we have gone past it. The very first time this runs, that will be some number minus 0, which will always be greater than 0, and thus always look like it’s been that long. Since we are just starting up, we always want to consider the first character we see. Subsequent calls will be using an “escCheckTime” that is generated based on “now + guard time”, so we are really checking to see if we have passed that future moment in time.

As you will see in a moment, every time a non escape character comes in, or anything else comes in during our guard time wait period, we just reset everything since it’s clearly not an escape sequence.

BUT, if the character we receive IS our escape character (“+”), we increment our “escCounter” to indicate how many of them we have seen in a row. It would go from 0 to 1 this first time.

If the counter gets up to 3 (ESC_TIMES), we have seen three of the escape characters in a row without being reset, so we set our escCheckTime to be “now plus guard time”. If you go back a few paragraphs, you will see we check this in the “nothing to do here, move along” function, so it can say “oh, it’s been at least guard time, AND we have seen our escape character sequence. Must be Command Mode time!”

But if it was NOT our escape character (else), we reset escCounter back to 0, and reset our check time yet again to “now plus guard time” to start the whole process over.

The next else is from the “has it been longer than guard time” if statement, and if we got a character before our guard time elapsed, we know it’s not an escape character so reset everything (just like the previous paragraph).

I hope to clean that up a bit so it doesn’t have to do it in two places like that.

At the end, we return false, because if we got there, we are in the process of either normal data, or scanning through a potential escape character sequence.

When I return, I will provide a full code example, and hopefully have some enhancements to the above function. Until then… I have to get back to packing. My rent is going up and it looks like I will be moving soon.

4 thoughts on “Parsing Hayes modem AT commands, part 2

  1. Remi Chateauneu

    Hi,

    Yes, I am interested by your AT commands parser: My goal is to plug it on a HF radio modem. I can program in C and C++.

    Reply
    1. Allen Huffman Post author

      Greetings! I have a fully functional “+++” escape parser that honors the proper guard times and such, but I have not completed work on the actual AT parser. I got sidetracked for the past few months but I hope to get back to work on it soon.

      Reply

Leave a Reply to Remi ChateauneuCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.