Skip to main content
sam239955
Associate III
August 31, 2012
Question

Recieve more then 1 Byte over USART

  • August 31, 2012
  • 19 replies
  • 4310 views
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
This topic has been closed for replies.

19 replies

Tesla DeLorean
Guru
August 31, 2012
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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
sam239955
sam239955Author
Associate III
August 31, 2012
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
sam239955Author
Associate III
August 31, 2012
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 

''
);
}

Tesla DeLorean
Guru
August 31, 2012
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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
sam239955
sam239955Author
Associate III
September 1, 2012
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 ?
Tesla DeLorean
Guru
September 1, 2012
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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
sam239955
sam239955Author
Associate III
September 1, 2012
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 !
Tesla DeLorean
Guru
September 2, 2012
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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
sam239955
sam239955Author
Associate III
September 2, 2012
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!!!!
Tesla DeLorean
Guru
September 2, 2012
Posted on September 02, 2012 at 14:15

I'd probably do some of the processing under interrupt, and make some effort to validate and synchronize the incoming data.

As to books, I learned C and Assembler a very long time ago. The K&R book is the best reference for C.

For the Cortex-M3, you could start with the ARM Technical Manuals, and Joseph Yiu's fine books, anyone of his Cortex series/editions would provide a good grounding. The STM32 specific stuff is covered well in the Data Sheets and Reference Manuals.

I suspect however you're looking for something that covers more of the mechanics of how things work in general. In this regard I'd suggest looking at some of the microprocessor books of the early 80's, while they didn't consider themselves ''embedded'' back then at lot of the systems are similar and resource limited, and addressed a lot of fundamental issues.

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