This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C167 reading/scanning input from GSM module from RS232

Hello,
I am a novice in C language and have been trying to find a method of reading/scanning input from GSM, then comparing it against a text (e.g OK or ERROR). Then if true set a break.

I have tried scanf with no avail. I have also tried reading string which works but am unsure how to compare with my text.Has anyone any advice with what function would be useful?

Any replies greatly appreciated.

Andy

  • Look up strcmp and associated functions in the Standard 'C' library.

  • I can get string by using gets() but can only compare two strings that are entered but not read.

    Any ideas to how to get round this?

    Do I have to place read string into memory etc?

  • "I can get string by using gets() but can only compare two strings that are entered but not read."

    I'm afraid I don't really understand your question. At a guess the GPS outputs a variety of (NMEA0183?) strings and you are trying to identify the one of interest before sucking the data out of it.

    "Do I have to place read string into memory etc?"

    Yes. It is probably easier to write your own routine to read in the string. That way you can detect the start and end characters to ensure that you capture one complete string with everything in the right place. You should then be able to use sscanf() to extract the data.

    Stefan

  • An example of the code I was trying to use is belo. It does not work. I will try sscanf now. Thanks.


    #include <stdio.h> /* standard I/O .h-file */
    #include <reg167.h> /* special function register 80C167 */
    #include <string.h>
    {
    char arr[50];
    gets(arr);

    printf("\nInput read into array was:\n"); //* just for confirmation*//
    puts(arr);
    char *string[1] = {"OK"} ; //* string 1 = OK (text)
    char *string[2] = gets(arr);
    int strcmp(string[1], char* arr);
    }
    {}

    {
    }
    }

  • please remember to use the < pre > and < /pre > tags for code - as it says in the instructions when you make a post!

    You are confusing your arrays and pointers - you need to re-read the section on strings in your 'C' textbook.

    The code you've shown won't compile, not least because you have defined the symbol 'string' twice:

    char *string[1] = {"OK"} ; //* string 1 = OK (text)
    defines 'string' as an array of 1 pointer;
    string[1] is a pointer to a char - in fact, the 1st char of "OK"
    char *string[2] = gets(arr);
    redefines 'string' as an array of 2 pointers!

    Take a look at the examples of strcmp and gets in the Manual, and that textbook.

  • Note also that your definitions of 'string' come after executable code - you can't do that in 'C'.

    You can do it in C++, but not in 'C'.

    Be sure that you're rading a 'C' textbook, not C++!

  • Interfacing to GSM can be a pain. You may find the code below useful - I certainly did - it is a string compare function with a limited wildcard facility.

    /*****************************************************************************
     *
     *  String Compare
     *
     *  Trigger:    Local call.
     *
     *  Input:      Pointers to the 'expected' and 'search' strings.
     *
     *  Output:     Returns TRUE to indicate a match and FALSE otherwise.
     *
     *  Function:   Compares two strings and returns TRUE if identical.
     *
     *  Notes:      Allows the use of character (?) and string (~) wildcards in
     *              the specification of the expected string.
     *
     *              THE ORDER OF EXPECTED AND ACTUAL POINTERS IS IMPORTANT
     *              WHEN THE WILDCARDS ARE USED.
     *
     *              THE EXPECTED STRING MUST BE IN CODE MEMORY AND THE
     *              ACTUAL STRING MUST BE IN XDATA.
     *
     *****************************************************************************/
    
    bit user_strcmp(unsigned char code *er, unsigned char xdata *ar) large
    {                                                   // ar = actual_string
                                                        // er = expected_string
        bit    match = TRUE;                            //
                                                        //
        do                                              // String compare loop
        {                                               //
            if( *er == '?' )                            // If a character wildcard is
            {                                           // in the expected string:
                er++;                                   // don't test this actual char
            }                                           //
            else if( *er == '~' )                       // If a string wildcard:
            {                                           //
                er++;                                   // Start the inner loop
                                                        // looking for the next
                                                        // char after the ~.
                while( (*ar != *er ) && ( *ar != '\0')) // Inner loop searches
                {                                       // through the actual string
                                                        // until either a match with
                                                        // the char after the expected
                    ar++;                               // or the end of the actual
                }                                       // string.
                                                        //
                                                        // If the loop terminated due
                                                        // to the actual \0 char
                if( *er != *ar )                        // but we were expecting
                {                                       // something else
                    match = FALSE;                      // we need to test again to
                }                                       // catch this condition.
                if( *er != '\0' )                       //
                {                                       //
                    er++;                               //
                }                                       //
            }                                           //
            else                                        // Normal test (No wildcard)
            {                                           //
                if( *er != *ar )                        //
                {                                       //
                    match = FALSE;                      //
                }                                       //
                if( *er != '\0' )                       //
                {                                       //
                    er++;                               //
                }                                       //
            }                                           //
        }                                               //
        while( ( *ar++ != '\0') && (match) );           //
                                                        //
        if( *er != '\0' )                               //
        {                                               //
            match = FALSE;                              //
        }                                               //
                                                        //
        return( match );                                //
    }                                                   //
    
    

  • "You may find the code below useful ...

    Input:      Pointers to the 'expected' and 'search' strings."

    But his previous post demonstrates a fundamental problem in his understanding of strings & pointers (and other aspects of the 'C' programming language) - that needs t be addressed before this code can be of any use!

  • "reading/scanning input from GSM, then comparing it against a text (e.g OK or ERROR)."

    These textual messages - OK, ERROR, CONNECT, RING, etc - are designed for use by a human operator sitting at a terminal and typing commands; they are not intended for machine interpretation.

    You will make you life very much more difficult by trying to read & parse these human-oriented messages in your software.

    These messages are known as the "Verbose" (or "Verbal") forms; there is also a "Terse" (or "Numeric") mode - intended specifically for machine-oriented interactions.

    In "Terse" mode, the modem gives simple numeric code responses - much easier to handle in software!

    Look for ATV in your modem's Manual

  • I tried using the section of code below to read a string and then compare it. I get 3 warnings and this may be why I am not getting correct results. If i type ok press carriage return the result will be zero which is what I am looking for.

    If i type anything else it is not zero which is fine but when i then try to type OK it still gives error. Any ideas if this is due to warnings?

    I may try _getchar , is this a better method of getting input?

    {


    char arr[50];

    printf("Enter up to 50 characters, with spaces\n");
    gets(arr);

    printf("\nInput read into array was:\n");
    puts(arr);
    }
    {
    char *string[1] = {"OK"} ;
    int result;
    char *arr = puts(arr);
    result = strcmp(string[1], puts(arr));
    printf("Result comparing OK to input is %d\n", result);
    }

  • Should:

    result = strcmp(string[1], puts(arr));
    
    Be:
    result = strcmp(string[0], puts(arr));
    

  • "Any ideas if this is due to warnings?"

    As you never said what warnings they are, it's impossible to tell!
    (unless you're telepathic)

  • Warnings are;

    warning 96: 'gets' too few actual parameters
    warning 40: 'int' converted to near pointer

    get warning 40 twice.

  • I think the following will do what you want:

    char arr[5];
    int result;

    gets(arr,4); //Note non-standard Keil gets()
    puts(arr);
    result=strcmp("Ok",arr);
    printf("Result=%d\n",result);

    There are many mistakes in the code you posted - you really need to work on your 'C' a bit with a good book.

    Stefan

  • Thankyou very much for taking the time to help me. I have several books on C I have got out of uni and have been using the PDF files.

    Do you recommend any books that are more Keil specifc?

    Thanks again