cancel
Showing results for 
Search instead for 
Did you mean: 

Usart receive problem

cyril2399
Associate II
Posted on July 22, 2011 at 15:10

Hello,

I try to write what I receive into a .txt.

First I do this:

RCC_Configuration();

GPIO_Configuration();

 

  USART_InitStructure.USART_BaudRate = 115200;

  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(USARTy, &USART_InitStructure);

  USART_Cmd(USARTy, ENABLE);

  USART_SetPrescaler(USARTy, 0x1);

  USART_IrDAConfig(USARTy, USART_IrDAMode_Normal);

  USART_IrDACmd(USARTy, ENABLE);

I think this is the good configuration (well sure...)

then I send the command like this:  USART_SendData(USART2, 'a');

then I have tried to do:

int size= 128;

for (i=0;i<size;i++)

  {

   buf[i]= USART_ReceiveData(USART2);

   fprintf(t1,''%c'',buf);

  }

and I tried to use the USART_Scanf(128);

but none of them runs. I don't know how to write into t1, all the text I receive.

thanks to any help...
20 REPLIES 20
cyril2399
Associate II
Posted on July 26, 2011 at 09:48

with what you gave me, I actually have only a sequence of ''enter''.

If size is bigger then I have more enter.

I can't see letters...

I have also tried with USART_Scanf(), but I think is won't help me. I put you the function but it is only for getting a numerice value from hyperterminal. Maybe we can use a part of it...

/**

  * @brief  Gets numeric values from the hyperterminal.

  * @param  None

  * @retval None

  */

uint8_t USART_Scanf(uint32_t value)

{

  uint32_t index = 0;

  uint32_t tmp[2] = {0, 0};

  while (index < 2)

  {

    /* Loop until RXNE = 1 */

    while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_RXNE) == RESET)

    {}

    tmp[index++] = (USART_ReceiveData(EVAL_COM1));

    if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))

    {

      printf(''\n\rPlease enter valid number between 0 and 9'');

      index--;

    }

  }

  /* Calculate the Corresponding value */

  index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);

  /* Checks */

  if (index > value)

  {

    printf(''\n\rPlease enter valid number between 0 and %d'', value);

    return 0xFF;

  }

  return index;

}

cerdani
Associate II
Posted on July 26, 2011 at 14:08

Are you still trying to put all your received data on a .txt file?

clive1 has already stated that you need a file system and a driver to do that.

If you only want to check if you have received your input correctly, just implement a data loop-back.

maybe something like this:

while(1)

{

     c = USART_ReceiveData(USART2);

     printf(''%c\n'',c);

}

cyril2399
Associate II
Posted on July 26, 2011 at 17:00

ok the answer is:

for (i=0;i<100;i++)

{

    while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)

    {

    }

     fprintf(t1,''%c'',USART_ReceiveData(USART2));

   

}

The RXNE has to be full before sending a data (1 bytes). If not the receive data is not a letter or a space etc.

But now I have another problem... it stops before the end of the transfert. Always at the same time. And then it is stuck into USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET which keep into his state.

I think that the register is full It stop to store the bytes. Or something like this.

Is anyone have the real reason? And how to solve it?

Thanks to everyone

Posted on July 26, 2011 at 17:02

I'm still not sure where you think the .TXT file is going to be stored.

The following will input data into a buffer, 128 characters. It will then output this to the console, which could be the serial port if you have the stdio functionality suitably hosted.

char buf[129];

int size= 128;

for (i=0;i<size;i++)

  {

   while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);

   buf[i]= USART_ReceiveData(USART2);

  }

buf[i] = 0; // NUL terminate

puts(buf); // print to console
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cyril2399
Associate II
Posted on July 28, 2011 at 09:24

Hello,

Your method doesn't work. I have try and it write something not good.

I don't have a console, so I don't understand how I could check if it works...

My only way to see if it works is to write it into a .txt (which works).

But as I said, after a time it stop to write. At a certain time it goes stuck because of the state of the flag.

Posted on July 28, 2011 at 14:00

Well that's unfortunate, you seem to be in a lot of trouble.

Are you using a real part or a simulation? What tool chain?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cyril2399
Associate II
Posted on July 28, 2011 at 14:49

I have IAR embedded workbench.

And there is no console like in visual studio.

But if it runs, it runs !

cyril2399
Associate II
Posted on August 01, 2011 at 11:25

I want to try something but I don't know how...

I want to put as parameter into my while loop a ''end of transmission''.

I want to stop the while when the last caracter is send. What is the function?

Thanks

cyril2399
Associate II
Posted on August 01, 2011 at 11:53

last character (sorry)

Posted on August 01, 2011 at 17:33

You could spin on USART_FLAG_TC (Transmission Complete)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..