cancel
Showing results for 
Search instead for 
Did you mean: 

Recieve more then 1 Byte over USART

sam239955
Associate II
Posted on August 31, 2012 at 20:51

Hi,

at the moment I'm using the following code to recieve a byte:

char
ReceivedData = NULL;
void
main() {
.
.
.
while
(1){
/* Wait until a byte is received */
while
(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
{
}
/* read byte and print via usart2 it */
ReceivedData = USART_ReceiveData(USART2);
printf
(
''Positionen: %d \r\n''
, ReceivedData);
//abbort if 3 received

if
(ReceivedData == 3)
break
;
}
.
.
}

I now want to recieve more then 1 byte and store it an recieve Buffer ''RXBUFF[]''. Because I want to send a byte array from my pc to the controller and then I want to deserialze the data to int16 or int32 ... How can i achieve that? how can I recieve my bytearray with the stm32f103 ? WR Alex #usart-stm32-loopback #usart-stm32
19 REPLIES 19
Posted on August 31, 2012 at 21:07

char RXBUFF[16];
void main()
{
int i = 0;
while(1)
{
/* Wait until a byte is received */
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
/* read byte and print via usart2 it */
RXBUFF[i++] = USART_ReceiveData(USART2);
if (i >= 16) i = 0;
}
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
sam239955
Associate II
Posted on August 31, 2012 at 22:15

Thank you clive,

can you show me how i can ''printf'' (loopback)  the Array to check that everything transmitted complete ?

printf(''Data Recieved: %c'', RXBUFF);

doesnt work
sam239955
Associate II
Posted on August 31, 2012 at 22:41

I want to make an easy loop back function and after implementing ur code it looks like this but i doesnt work.

while
(1)
{
//Bytes RECV ( RXBUFF[248] )
/* Wait until a byte is received */
while
(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
{}
RXBUFF[j++] = USART_ReceiveData(USART2);
if
(j >= 248) 
j = 0;
//Bytes SEND ( RXBUFF[248] )
while
( j>= 248)
{
USART_SendData(USART2, RXBUFF[j]);
j++;
/* Loop until the end of transmission */
while
(USART_GetFlagStatus(IMU_COM2, USART_FLAG_TC) == RESET)
{}
}
printf
(
''all bytes recv. and sended 

''
);
}

Posted on August 31, 2012 at 23:18

while(1)
{
/* Wait until a byte is received */
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
RXBUFF[j] = USART_ReceiveData(USART2);
if (USART_GetFlagStatus(USART2, USART_FLAG_TXE) != RESET)
USART_SendData(USART2, RXBUFF[j]);
j++;
if (j >= 248)
j = 0;
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
sam239955
Associate II
Posted on September 01, 2012 at 02:08

Thank you clive for helping me,

I implemented your code in a function:

void
GetScrewPositions()
{
printf
(
''ready to get Data 

''
);
int
j =0;
while
(1)
{
//Wait until a byte is received
while
(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
RXBUFF[j] = USART_ReceiveData(USART2);
if
(USART_GetFlagStatus(USART2, USART_FLAG_TXE) != RESET)
USART_SendData(USART2, RXBUFF[j]);
j++;
if
(j >= 248)
j = 0;
printf
(
''All bytes recieved and sended

''
);
}
}

IT WORKS NOW! do you know how to deserialize Data in the quickest way on a stm32 ?
Posted on September 02, 2012 at 00:03

do you know how to deserialize Data in the quickest way on a stm32 ?

Depends on what the data looks like, your could do it as a state machine in an interrupt, or build a line buffer and pass it off to a decoding/parsing task.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
sam239955
Associate II
Posted on September 02, 2012 at 01:29

hi clive,

I mean, how can I write my recieved data

 RXBUFF[248]

to my variables. Because I recieve 60x int32_t ... and RXBUFFER containts only bytes. like 2F00 = uint16 -> 47 you know what I mean. I want to deserialize my data to my datatypes like this. int32 mynumber1 = RXBUFFER[0] // 4 bytes of rxbuffer parsed to int32 number1 int32 mynumber2 = RXBUFFER[4] ... int32 mynumber60 = RXBUFFER[240] Is there a way for this ? I really thank you for helping me !
Posted on September 02, 2012 at 02:35

At bit kludgy but

int32 mynumber1 = *((int32 *)&RXBUFFER[0]); // 4 bytes of rxbuffer parsed to int32 number1

int32 mynumber2 = *((int32 *)&RXBUFFER[4]);

or marginally less

int32 *array32 = (int32 *)RXBUFFER;

number1 = array32[0];

number2 = array32[1];

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
sam239955
Associate II
Posted on September 02, 2012 at 04:46

works perfect! thank you !

one last question. I just want to recieve my 248 bytes now. I modified the function a little bit. And it works so far when im sending a file with exact 248bytes.

#define buflgth 248 //my byte count
int j =0;
int cnt = 0; 
while(cnt <= buflgth-1)
{
//Wait until a byte is received
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
RXBUFF[j] = USART_ReceiveData(USART2);
cnt++; 
j++;
if (j >= buflgth)
j = 0;
}

is there an easier or better way? where can I find a Book about stm.. or other question where do you learned everything about stm32 clive? ... 😉 thank you very much!!!!