cancel
Showing results for 
Search instead for 
Did you mean: 

USART judges that the input character has no response

RCHEN.3
Associate

#include "stm32f4xx.h"
#include "main.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
 
void My_Usart2_Printf(char *string){
       while(*string){
        
        USART_SendData(USART2, (unsigned short int) *string++);
 
        
        while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
    }
}
 
int   Uart_Init(){
    
      USART_InitTypeDef USART_InitStructure;
      GPIO_InitTypeDef GPIO_InitStructure;
 
    
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);  
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  
    
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
 
      
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
      GPIO_Init(GPIOA, &GPIO_InitStructure); 
 
      
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; 
      GPIO_Init(GPIOA, &GPIO_InitStructure); 
 
      
      USART_InitStructure.USART_BaudRate = 9600; 
      USART_InitStructure.USART_WordLength = USART_WordLength_8b; 
      USART_InitStructure.USART_StopBits = USART_StopBits_1; 
      USART_InitStructure.USART_Parity = USART_Parity_No;
      USART_InitStructure.USART_HardwareFlowControl = 
USART_HardwareFlowControl_None; 
      USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  
      USART_Init(USART2, &USART_InitStructure); 
    
      
      USART_Cmd(USART2, ENABLE);
	
      NVIC_EnableIRQ(USART2_IRQn);
 
}
 
int    main(){
    
	Uart_Init();
	 while (1){			 
			 }
      }
 
	/* Determine if the 2 string arrays (Char Array)
   are the same Same return, 1, not the same return 0 */
 
int charArrayEquals(char a [], char b []){
    int as = strlen(a);
    int bs = strlen(b);
    int i = 0;    
            
    if(as != bs){
        return 0;
    }
    
    for(i = 0; i < as; i++){
        if(a [i] != b [i]){
            return 0;
        }
    }    
    
    return 1;
}
 
char buff [] = "";
 
/*
 * USART2 Interrupt function
 */
void USART2_IRQHandler(){
    
    
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){
        
        char c = USART_ReceiveData(USART2); //Received characters
        char check [] = "Hello"; // String array to be judged
        
        if(c != '\r' && c != '\n'){
           // Add the received characters to the end of the buff
           sprintf (buff, "%s%c", buff,c); 
        }else{
            My_Usart2_Printf(buff); // Return the message entered by the user
            
            if(charArrayEquals(buff, check)){ // Determine if the buff is the same as the check string array
                My_Usart2_Printf(" ... OK\n"); // If the input is Hello, it will display OK.
            }else{
                My_Usart2_Printf("795222\n"); // If the input is not Hello, the line is directly wrapped.
            }
                
            memset(buff, 0, strlen(buff)); // Empty the buff string
        }
        
    }
}

1 REPLY 1
Danish1
Lead II

You've given us some code. You say it doesn't do something.

You haven't done useful things like tell us how you put that code together.

Or what it actually does.

Or what your level of knowledge is, what programming languages you've used a lot.

If this is a class assignment, then it would NOT be helpful to you for us to simply provide you with working code. Ok you'd get this assignment in and marked, but you wouldn't learn the vital steps of understanding what we hope is your own code. So you would be worse off when faced with the next assignment, or your final exam, or when trying to do something for your own project or for an employer.

My guess is that you're not really used to C, but more comfortable in something like C# or Java.

This is because I didn't spot anything in your code that tells me (or, more importantly, the compiler) to allocate some memory for your incoming string.

Your code actually sets up a pointer-to-a-character "buff", using the C convention that a string (or character-array) is a sequence of characters in memory starting at where-you-point and ending with a character that has an ascii value of 0. And you point it to an empty-string, which the compiler will probably put in read-only memory.

My understanding of C# and Java is that their string-handling is much easier to use than C, and the compiler would do the hard work of allocating this memory, and making extra space as your string gets longer. But in C the easiest approach is to decide how long the maximum length will be and tell the compiler to allocate that amount of memory (e.g. "char buff[50];"). You also need to initialise the string to zero length, probably at the start of main(). And you need to think what should happen when the string approaches this maximum length - there have been many security-breaches because this complication of C has not been handled correctly.

Hope this helps you get your code working.

Danish