This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to read in a number from PuTTY such as "123" and store it as a int value.

Hello forum, 

So i was attempting to read a value input from the user such as "123" or "234552" etc. from the PuTTY and store it into a int variable.

Ex.

user input = 1234

stored value into.....

int = user input =1234;

I had tried some ways using a char pointer and using UARTChartGet(base) but it was not not storing it properly.

void UART_InString(char *bufPt, unsigned short max) // max is number of chars, and *bufPt points back to a : char Instring;
{
	int i;

	int length=0;
	char character;
	*bufPt = 0;
	character = UARTCharGet(UART0_BASE);
	while(character != 13)// number for carriage return
        {
		if(character == 8)// number for backspace
                {
			if(length){
				bufPt--;
				length--;
				UARTCharPut(UART0_BASE, 8);
			}
		}
		else if(length < max){
			*bufPt = character;
			bufPt++;
			length++;
			UARTCharPut(UART0_BASE, character);
		}
		character = UARTCharGet(UART0_BASE);
	}
}

i hope i was on the right path. I was also planning on using atoi() function after the value was input to transfer to int variabe not sure how yet.  Also if there is an easier way please point me in the right direction if possible.

  • also as a reference i used the code Uart_InString from UTexas
    link: users.ece.utexas.edu/.../UART.c
  • A suggestion. First try it without adding in the string editing functionality.

    Robert

    And unless you are connecting to a terminal you will not need string editing (sometimes not even then).
  • int main (void){
    char InString[4];
    char* inPtr;
    
    inPtr = Instring;
    
    
    UART_InString(inPtr, 4);
    
    return 0;
    }
    
    
    void UART_InString(char *bufPt, unsigned short max)
    {
    
    	int length=0;
    	char character;
    	character = UARTCharGet(UART0_BASE);
    	while(character != 13){
    		if(character == 8){
    			if(length){
    				bufPt--;
    				length--;
    				UARTCharPut(UART0_BASE, 8);
    			}
    		}
    		else if(length < max){
    			*bufPt = character;
    			bufPt++;
    			length++;
    			UARTCharPut(UART0_BASE, character);
    		}
    		character = UARTCharGet(UART0_BASE);
    	}
    
    	*bufPt = 0;
    
    }

    So i figured out what was wrong. It was how i was passing my parameters. So here is the solution.