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.

CCS/MSP430F5529: Using UART and PuTTY for simple commands

Part Number: MSP430F5529


Tool/software: Code Composer Studio

I have a program that requests a user name and password through a terminal (I'm using PuTTY), and then compares it to a pre-stored value.  It is printing the "correct answer" statement prematurely, and I am unsure if it is storing the user name and password inputs correctly, as well.

//Instructions: Set the following parameters in PuTTY
//Port:         COM4
//Baud rate:    115200
//Data bits:    8
//Parity:       None
//Stop bits:    1
//Flow Control: None

#include <msp430.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

//values for outputs and input
char firstquestion[28] = "Please enter the user name: ";
const char username[9] = "user name";
char usernameinput[9];
char secondquestion[21] = "Enter the password: ";
const char password[9] = "password";
char passwordinput[9];
char correctanswer[22] = "Welcome to CPE 325!!!";
char incorrectanswer[34] = "Incorrect user name or password!!!";


//loop counter
unsigned int i=0;

//new line and carriage return
void newline(void)
{
    while(!(UCA0IFG&UCTXIFG));  //waits for character
    UCA0TXBUF='\n';             //sets character to new line
    while(!(UCA0IFG&UCTXIFG));  //waits for character
    UCA0TXBUF='\r';             //sets character to carriage return
}
//prompt for user name
void userdata(void)
{
    for(i=0; i<strlen(firstquestion);i++)   //loops through first question elements and outputs
    {
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=firstquestion[i];         //sets character to for element of first question
    }

    newline();                              //calls new line and carriage return function

    for(i=0; i<50;i++)                      //loops through received characters loop needs to go long enough for password and user name
    {
        while(!(UCA0IFG&UCRXIFG));          //waits for character
        if(UCA0RXBUF== 0x0D)                //if character is the "Enter key" will break from receiving characters
            break;
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=UCA0RXBUF;                //sets transmit buffer equal to receive buffer to echo print what user types
        usernameinput[i]=UCA0RXBUF;         //sets first element of user name equal to first character received from transmit buffer
    }
    UCA0RXBUF = 0;                          //clears transmit buffer
    usernameinput[i]= 0x00;                 //adds null character to end of user name array
}

void passdata(void)
{
    newline();
    for(i=0; i<strlen(secondquestion);i++)  //loops through second question elements and outputs
    {
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=secondquestion[i];        //sets character to for element of second question
    }

    newline();                              //calls new line and carriage return function

    for(i=0; i<50;i++)                      //loops through received characters loop needs to go long enough for password and user name
    {
        while(!(UCA0IFG&UCRXIFG));          //waits for character
        if(UCA0RXBUF== 0x0D)                // if character is the "Enter key" will break from receiving characters
            break;
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF='*';                      //instead of echo printing prints '*' for the bonus
        UCA0TXBUF=UCA0RXBUF;
        passwordinput[i]=UCA0RXBUF;         //sets first element of password equal to first character received from transmit buffer
    }
    UCA0RXBUF = 0;                          //clears transmit buffer
    passwordinput[i]= 0x00;                 //adds null character to end of password array
}

void correct(void)
{
    newline();                              //calls new line and carriage return function

    for(i=0; i<strlen(correctanswer);i++)   //loops through the output if the user name and password are character
    {
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=correctanswer[i];         //sets character to each element of output
    }

    newline();                              //calls new line and carriage return function
}

void incorrect(void)
{
   newline();                               //calls new line and carriage return function

    for(i=0; i<strlen(incorrectanswer);i++) //loops through output if user name and password are incorrect
    {
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=incorrectanswer[i];       //sets character to each element of output
    }

    newline();                              //calls newline and carriage return function
}

void comparedata(void)
{
    unsigned int x=0;                       //values for testing user name
    unsigned int y=0;                       //value for testing password
    for(i=0; i<strlen(username);i++)        //loops through user name
    {
        if(username[i]==usernameinput[i])   //if user name element is equal to the inputed user name element
            x=1;                            //sets x to 1
        else
        {
            x=0;                            //if not equal sets x to 0
            break;                          //also breaks if it is not equal
        }
    }
    for(i=0; i<strlen(password);i++)        //loops through password
    {
        if(password[i]==passwordinput[i])   //if password element is equal to the inputed password element
            y=1;                            //set y to 1
        else
        {
            y=0;                            //if not equal sets y to 0
            break;                          //also breaks if it is not equal
        }
    }
    if((x && y) == 1)                       //if x and y equal 1 then both password and user name are the same
        correct();                          //if they are the same calls correct function to print specific output
    else
        incorrect();                        //if either of them are not equal call incorrect function to print specific output
}

void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;   // Stop WDT
    P3SEL |= BIT3 + BIT4;       // Set USCI_A0 RXD/TXD to receive/transmit data
    UCA0CTL1 |= UCSWRST;        // Set software reset during initialization
    UCA0CTL0 = 0;               // USCI_A0 control register
    UCA0CTL1 |= UCSSEL_2;       // Clock source SMCLK
    UCA0BR0 = 0x09;             // 1048576 Hz / 115200 lower byte
    UCA0BR1 = 0x00;             // upper byte
    UCA0MCTL |= UCBRS0;         // Modulation (UCBRS0=0x01, UCOS16=0)
    UCA0CTL1 &= ~UCSWRST;       // Clear software reset to initialize USCI state machine

    while(1)
    {
        userdata();                             //calls user name function
        passdata();                             //calls password function
        comparedata();                          //calls compare function
    }
}

  • Paul Mitchell said:
    char firstquestion[28] = "Please enter the user name: ";

    That string has 28 characters, and so the firstquestion[28] array doesn't include space to store the terminating null character.

    As a result, strlen(firstquestion) will run off the end of the firstquestion[28] array until it finds the next null in memory.

    I.e. the following will display "Please enter the user name: " and then following characters in memory until finds a null character:

        for(i=0; i<strlen(firstquestion);i++)   //loops through first question elements and outputs
        {
            while(!(UCA0IFG&UCTXIFG));          //waits for character
            UCA0TXBUF=firstquestion[i];         //sets character to for element of first question
        }

    To avoid having to maintain the array size for strings which are constant, suggest instead of an array declare a pointer to a const string. E.g.:

    const char firstquestion *const = "Please enter the user name: ";

**Attention** This is a public forum