Cannot get string input to work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andrewc
    New Member
    • Jul 2009
    • 6

    Cannot get string input to work

    I am trying to get a 2 character string into my program in order to then determine a command based on the string. This is in an embedded application.

    Right at the top of my program I have declared

    static char comin[3]; // declared as 'static' - I will use it in a number of places
    .
    .
    .

    void main ()
    {
    //process comin here and execute command based on string in comin
    }


    //here is the isr that goes and fetches the string from the serial port

    void uart_rx_isr (void) interrupt 4 using 3
    {
    EA=0; // disable interrupts while this one is serviced
    gets(comin,3);
    EA=1; //re-enable interrupts
    }

    I am using a Keil compiler

    gets() supposed to take in 3 chars and then exit, or, if it encounters a \n before 3 chars, it is supposed to terminate. It replaces \n with a null ('0').

    However, when I look at the contents of comin in the simulator, all values are 0 and the program hangs in the interrupt service routine - it just sits there.

    I think I am doing something wrong with my pointers or array wrt comin.

    Can someone point me the right direction here?

    Appreciate your help.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I would not use gets(). That function will fetch chars until it sees a \0 regardless of whether it has trashed your memory.

    Use get() in a loop to fetch the correct number of chars. If you want a string in your array, use get() for fetch two chars and then you append provide the
    \0 yourself.

    Comment

    Working...