Page 1 of 1

Sending a byte array as a string?

Posted: Mon Mar 21, 2016 4:57 pm
by GTF
Not sure if I am doing this correctly. I created a variable such as "data_string[14]" to send 14 bytes of data via UART to another MCU using a circular buffer to receive the data. The data is organized as:
data_string[0] = byte1
data_string[1] = byte2
.
.
.
data_string[13] = byte14

If I send the string "data_string" NumBytes returns only 1. While if I send data_string[0] to data_string[13] individually, all bytes are received. I can post examples tonight when I get home.

Re: Sending a byte array as a string?

Posted: Mon Mar 21, 2016 6:02 pm
by Benj
Hello,

Are any of your byte values 0?

For a string variable the data bytes usually contain ASCII values e.g. 32 is a space, 48 is a '0' character etc. The 0 tells the function where the string data ends so we don't go off into an uninitialised part of the string variable or even worse off into uninitialised memory or other variables.

So if one of the data bytes is a 0 and you call the send string function then it will only send out data bytes until it hits the 0 and then it will end.

What might be better is to send the data using the send byte function in a loop and this way you know how many bytes you wish to send.

bytestosend = 15
counter = 0
while counter < bytestosend
{
sendbyte string[counter]
counter = counter + 1
}

Re: Sending a byte array as a string?

Posted: Mon Mar 21, 2016 6:48 pm
by GTF
Thanks Benj. That is basically what I am doing. Just thought I would give it a try with sending as a single string. It is possible that the values of some of the bytes are 0, as they are derived from 10 bit values that are split into high and low bytes. So a variable with a current value under 256 will have a high byte of 0.

Re: Sending a byte array as a string?

Posted: Mon Dec 13, 2021 1:19 pm
by George_B
Benj wrote:
Mon Mar 21, 2016 6:02 pm
Hello,

Are any of your byte values 0?

For a string variable the data bytes usually contain ASCII values e.g. 32 is a space, 48 is a '0' character etc. The 0 tells the function where the string data ends so we don't go off into an uninitialised part of the string variable or even worse off into uninitialised memory or other variables.

So if one of the data bytes is a 0 and you call the send string function then it will only send out data bytes until it hits the 0 and then it will end.

What might be better is to send the data using the send byte function in a loop and this way you know how many bytes you wish to send.

bytestosend = 15
counter = 0
while counter < bytestosend
{
sendbyte string[counter]
counter = counter + 1
}
This topics and your answers generally from all the Matrix Team but especially from you BEN are "treasures". The only thing is that you have to dig a lot to find what you are after. But that is ok.
Thank you is the minimum i can say for your support efforts.

George