How to Receive unknown number of bytes

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi,

Iam using Discover wi-fi STM32F4DIS module. I want to receive data whose size is not known. In wiced_uart_receive_bytes() if suppose i initialized the size to be 40 bytes and iam receiving only 30 bytes this is not processing the 30 bytes and it is waiting for 40 bytes to be received. Actually i don't know the size of the data which has to be received. The data can be of any number of bytes and i need to process the received bytes.

Please help me...

Thank you

0 Likes
1 Solution
Anonymous
Not applicable

Easiest way would be to put a header on your data. For example, if your data is 30 bytes long, you could first send the bytes 0xAB 0x1E where 0xAB is a sync byte and 0x1E is the payload length.

Start up a thread that keeps reading single bytes of data, throwing them away until the sync byte is received. Once the sync byte is received, read one more byte to get the payload length. Then read [payload length] bytes and you should have your data.

There are, of course, ways to make this more robust (checksums), but this should work for you.

EDIT: edited reply with more detailed approach

View solution in original post

2 Replies
Anonymous
Not applicable

Easiest way would be to put a header on your data. For example, if your data is 30 bytes long, you could first send the bytes 0xAB 0x1E where 0xAB is a sync byte and 0x1E is the payload length.

Start up a thread that keeps reading single bytes of data, throwing them away until the sync byte is received. Once the sync byte is received, read one more byte to get the payload length. Then read [payload length] bytes and you should have your data.

There are, of course, ways to make this more robust (checksums), but this should work for you.

EDIT: edited reply with more detailed approach

Anonymous
Not applicable

Thank you for your response!!!

0 Likes