Sahil 0 Posted June 4, 2014 Share Posted June 4, 2014 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 Quote Link to post Share on other sites
Lyon 3 Posted June 4, 2014 Share Posted June 4, 2014 Hi, There are several problems with your code - and I will take them one by one - lets start with the first one: char *stringrecv[4]; Please see this link: http://www.cplusplus.com/forum/beginner/30805/ where it is described a similar case with yours. Correct your code and come back for the next step. L Quote Link to post Share on other sites
Sahil 0 Posted June 5, 2014 Author Share Posted June 5, 2014 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++; } } Quote Link to post Share on other sites
morry 0 Posted June 6, 2014 Share Posted June 6, 2014 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 ... Sahil 1 Quote Link to post Share on other sites
Sahil 0 Posted June 6, 2014 Author Share Posted June 6, 2014 @ Your point is informative, thank you for that. Pradeepa 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.