Sending a byte array as a string?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
GTF
Posts: 170
Joined: Sat Dec 10, 2011 7:21 pm
Location: Canada
Has thanked: 20 times
Been thanked: 52 times
Contact:

Sending a byte array as a string?

Post 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.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Sending a byte array as a string?

Post 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
}

GTF
Posts: 170
Joined: Sat Dec 10, 2011 7:21 pm
Location: Canada
Has thanked: 20 times
Been thanked: 52 times
Contact:

Re: Sending a byte array as a string?

Post 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.

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: Sending a byte array as a string?

Post 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

Post Reply