Jump to content
43oh

Can not save the received characters of UART to a string


Recommended Posts

Dear All,

 

I want to collect the received characters from hyperterminal through UART0 of my launchapd and to save them to a string and then to print it back on the hyperterminal.

 

When I try to print it (e.g. UARTCharPut(UART0_BASE, *stringrecv[0]) ;) it prints nothing.

 

my code is as follows;

 

int
main(void)
{
char uartrecv;
char *stringrecv[4];
 int i=0;
//SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
 
 
    //
    // Set the clocking to run directly from the external crystal/oscillator.
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
    // crystal on your board.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                  SYSCTL_XTAL_16MHZ);
 
inituart0();
UARTsendString("\n\rWELCOME\n\r");
UARTsendString("Please, input a string: ");
    do
    {
        //
        // Read a character using the blocking read function.  This function
        // will not return until a character is available.
        //
        uartrecv = UARTCharGet(UART0_BASE);
*stringrecv = uartrecv;
 
i=i+1;
 
        //
        // Write the same character using the blocking write function.  This
        // function will not return until there was space in the FIFO and
        // the character is written.
        //
//UARTCharPut(UART0_BASE, '\n');
//UARTCharPut(UART0_BASE, '\r');
UARTCharPut(UART0_BASE, uartrecv);
 
 
    //
    // Stay in the loop until either a CR or LF is received.
    //
    } while((uartrecv != '\n') && (uartrecv != '\r'));
 
UARTCharPut(UART0_BASE, '\n');
UARTCharPut(UART0_BASE, '\r');
 
    //
    // Put a character to show the end of the example.  This will display on
    // the terminal.
    //
    UARTsendString("Your string is: ");
//UARTsendString(*stringrecv);
UARTCharPut(UART0_BASE, *stringrecv[0]);
UARTCharPut(UART0_BASE, *stringrecv[1]);
UARTCharPut(UART0_BASE, *stringrecv[2]);
 
UARTsendString("\n\rSee you next time.\n\r");
    //
    // Return no errors
    //
    return(0);
}
 
It prints correctly all the data i send to the hyperterminal except the data i store in the pointer  *stringrecv.
 
Is there something wrong with saving the characters to the string or retrieving back from the string ?
 
Any suggestion is welcome.
 
Regards,
Iftikhar
Link to post
Share on other sites
Hi Lyon

Thank you for your reply.

 

I have made some modifications in the code. Now it is working fine. The link you mentioned was informative.

 

int

main(void)

{

char uartrecv;

char stringrecv[]="";

int i=0;

 

//SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

 

 

    //

    // Set the clocking to run directly from the external crystal/oscillator.

    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the

    // crystal on your board.

    //

    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |

                  SYSCTL_XTAL_16MHZ);

 

inituart0();

UARTsendString("\r\nWELCOME"); 

UARTsendString("\r\nPlease, input a string: ");

    do

    {

        

        // Read a character using the blocking read function.  This function

        // will not return until a character is available.

        

        uartrecv = UARTCharGet(UART0_BASE);

stringrecv = uartrecv;

i++;

 

       //Echo the character typed by the user

UARTCharPut(UART0_BASE, uartrecv);

 

 

    //

    // Stay in the loop until either a CR or LF or i<10 is received.

    //

    } while((uartrecv != '\n') && (uartrecv != '\r') && (i<10));  

 

 

 

UARTCharPut(UART0_BASE, '\n');

 

 

    UARTsendString("\r\nYour string is:  ");

UARTsendString(stringrecv);

 

UARTsendString("\r\nSee you next time.");

   

    return(0);

}

 


void UARTsendString(char *pt){

 

  while(*pt!='\0'){

    UARTCharPut(UART0_BASE, *pt);

    pt++;

  }

 

}

Link to post
Share on other sites

Please note that this statement does not allocate any memory for your received string:

char stringrecv[]="";

 

There could be strange effects when your code gets more complex since the memory area of your stringrecv array is not protected and could be overwritten by other variables.

 
An common solution is to define a fixed buffer size:
#define UART0_BUFFER_SIZE 32
...
char stringrecv[uART0_BUFFER_SIZE]; //32 bytes will be reserved for this array
...
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...