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
Posted on July 23, 2011 at 14:52

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

You'd want to ensure the string was NUL terminated, and be using sscanf().

I think you'd want to accumulate all the characters in buf[] before copying them to t1.

What's the bounding condition, assuming you don't always receive 128 characters?
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 25, 2011 at 09:32

hello,

thanks for replying. The number 128 is for the example. I don't know how long will be the answer.

I have found for using sscanf() but this is for convert a string into an int.

I don't understand how I should use it.

Could you be a little more precise please?

Thanks

cerdani
Associate II
Posted on July 25, 2011 at 11:59

Have you created your .txt file?

Maybe it would be better if you put something like this at startup (if you haven't done this).

FILE * t1 = fopen( ''yourFile.txt'', ''w'');

cyril2399
Associate II
Posted on July 25, 2011 at 12:09

Yes it exists. I have created it like you say.

This is not the solution :)

thanks anyway

trevor23
Associate III
Posted on July 25, 2011 at 12:36

Surely it would be easier to use

fputch(t1, buf[i]);
cerdani
Associate II
Posted on July 25, 2011 at 12:53

t1 probably returns a NULL pointer. I think you may have to rewrite file creation and file writing functions yourself.

cyril2399
Associate II
Posted on July 25, 2011 at 15:37

I don't know this function : fputch

What does it do?

And for the files I create my file like this:

FILE *t1 = NULL;

t1 = fopen(''test1.txt'',''w'');

fclose(t1);

t1 = fopen(''test1.txt'',''r+'');

I am still trying, and still no succeed.

Thanks for your help

Posted on July 26, 2011 at 02:15

Sorry I was confused by the use of fprintf, where buf is a pointer, not a char value, and it was always the pointer to the first character.

int size= 128;

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

  {

   buf[i]= USART_ReceiveData(USART2);

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

  }

Something like this would frankly make more sense in C

int size= 128;

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

{

   fputc(USART_ReceiveData(USART2), t1);

}

Then again, all this presumes you have a file system to send the data too. Unless you have a file system, and a memory device driver of some sort, it usually makes more sense to store data to RAM or FLASH in a more direct fashion.
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 26, 2011 at 09:30

At this moment I'm jsut trying to see if I succeed to send and receive data by Usart way (or UART). I just want to see if I can get the datas.

After I will write all the datas into the Flash memory, but at a moment I will write all the flash memory into a .txt.

Thanks, I try what you tell me and I tell you if I fail or succeed...