Page 1 of 1

Solved: 0x00 in String cuts the string and Length$

Posted: Tue Aug 19, 2014 2:32 pm
by Rudi
Hi guys ;-)


i added a simple flowchart (without chip) for simulation in fine step, please add the Variable Test in Simulation Debugger
in first calculation, the string only "~" because the strTest[1]=0x00 cuts the string
in second calculation, the string only "~\x01~" because the strTest[3]=0x00 cuts the string
in third calculation, the string then "~\x01~\x01~"

do you have a workaround for this.

RS232 muss send the String Test like this: "~\x00~\x00~"

Code: Select all

// calculation
strTest = ""
Test[0] = 0x7E
Test[1] = 0x00     // cuts the string-- next is never sending..
Test[2] = 0x7E
Test[3] = 0x00    
Test[4] = 0x7E
Thank you ;-)

Best wishes
Rudi
;-)

ps:
I found this:
http://www.matrixtsl.com/mmforums/viewt ... nding+0x00
LeighM wrote:
Yes, the RS232 component SendRS232String macro expects a null terminated character string.
So it will only send the characters up to the point where the first null (zero) is encountered.

ok this is RS232..
do i must split then the sending string and send the 0x00 allways as char 0x00 between or have you a Trick Tip ? ;-)


in the simulation debugger the simmulation is without a RS232 - is this generally in string too?


Best thank!

Re: No Hurry! Need a Workaround: 0x00 in String cuts the str

Posted: Tue Aug 19, 2014 5:10 pm
by Rudi
Hi Guys,

i solved it for me in RS232 Theme like this:

Code: Select all


Test[0] = 0x7E
Test[1] = 0x00
Test[2] = 0x7E
Test[3] = 0x00

I Know, that the String is 4 byte long, so i make a while icon with accumulator 4 times - start with 0 - the accumulator i named as variable i

so example in RS232 to send the string with the 0x00 correctly i use

Code: Select all


RS232::SendChar(Test[i])

it will send now

RS232::SendChar(Test[0]) // this will receive the 0x7E
RS232::SendChar(Test[1]) // this will receive the 0x00
RS232::SendChar(Test[2]) // this will receive the 0x7E
RS232::SendChar(Test[3]) // this will receive the 0x00


For the Simulation Debugger as String - i do not know a way just in time - i think this must done by "string the hex ? " ;-) .. but this is a other Theme .. ;-)
If someone have a other tip - "he.. she.. it" .. is welcome ;-)

Best wishes!
Rudi
;-)


BTW:

This you need to send Datagram in XBee corectly well! - XBee is biting 0x00 in the Protocoll ...also in 64 bit Destination Adress and 16 bit Destination Adress.
So - bookmark this ;-) for Base Understanding in http://www.matrixtsl.com/mmforums/viewt ... 54&t=15204

;-)

Re: Partially Solved : 0x00 in String cuts the string

Posted: Tue Aug 19, 2014 6:47 pm
by Rudi
Hi Guys!

The 0x00 in a String will cut functions too!

Be informed:

If the Data string like this:

Code: Select all


strTest[0] = 0x7E
strTest[0] = 0x00
strTest[0] = 0x03
strTest[0] = 0x10

The String function

Code: Select all


ByteVariable = Length$(strTest) 

will be only 1 not 4 because the 0x00 cut the string!


I will try a workaround like this for calculate the length of the "String Datagram"

i will used a extra Byte Variable for save the length of the string with 0x00

i check each byte in the string, and is it >= 0x00 the Byte Variable StringCount will be ++ too, if the byte of stríng is empty then this is the end of check routine and nothing is ++

and the end, the StringCount will be exactly 4 - in hex then 0x04.
This is needed in the XBEE Protocoll in the LSB Bit.

Best wishes
Rudi
;-)



for

Re: Partially Solved: 0x00 in String cuts the string and Len

Posted: Tue Aug 19, 2014 7:27 pm
by Rudi
Hi Guys
..

little Problem:
============

Code: Select all


strTest = ""

strTest[0] = 0x7E
strTest[2] = 0x7E
strTest[3] = 0x7E

strTest[1] is not "empty" ,, it is 0x0

is this normal for terminated string ? and how you resolved this in Projects if String[idx] must not counting if empty or counting if 0x00 .. null terminated allways if empty?

any tip?

Best wishes!
Rudi
;-)


edit

same:

i try with single '

Code: Select all


strTest = ""

strTest[0] = 0x7E
strTest[1] = ''
strTest[2] = 0x7E
strTest[3] = 0x7E

strTest[1] is not "empty" ,, it is 0x0

Re: Partially Solved: 0x00 in String cuts the string and Len

Posted: Tue Aug 19, 2014 9:24 pm
by kersing
Rudi,

In C a zero value is used to terminate a sting. So the behavior is as I would expect it (because Flowcode generates C code) If you want to use byte arrays with zero values you will need to keep track of the number of bytes in the array yourself.

Best regards,

Jac

Re: Partially Solved: 0x00 in String cuts the string and Len

Posted: Wed Aug 20, 2014 10:53 am
by Benj
Hello Rudi,

I agree with Jac, a string is generally used for an array of characters which is terminated with a null character i.e. 0x00.

If you need an array of bytes then it might be better to use the byte variable in an array rather than a string. This way you are forced to use it a byte at a time which as you say works as expected.

To convert a byte variable to an array of bytes simply add the [x] after the variable name where x is the number of elements in the array, indexed at locations 0 to x-1.

Re: Partially Solved: 0x00 in String cuts the string and Len

Posted: Wed Aug 20, 2014 12:40 pm
by Rudi
Hi Benj, hi Jac

Many Thanks for your Time and Help!!!
Best thank to you!

I am honest - my favorite language is Assembler, Pascal / Lazarus / Delphi

and with ( $ is a Info for compiler that comes a Integer as Hex ( likewise))

Code: Select all

VAR MyString : String;
begin
MyString := char($7E) + char($00) + char($17);
SerialCom.write(MyString,Length(MyString);
end
i am very spoiled ( i hope this is the correct translating for "verwöhnt" )

I have test in Code::Block the same and this cut the string too, so i wounder me ;-)

Code: Select all

MyString[0] = 0x7E;
MyString[1] = 0x00;
MyString[2] = 0x17;
printf(" %s", MyString);
Let me see only "~" at the console ;-)

ok - i have understand yesterday so:

I allways make this:

Code: Select all

MyString[0] = 0x7E;
MyString[1] = 0x00;
MyString[2] = 0x17;

RS232.SendString (MyString);
This cuts the Stríng.

So i change one Time yesterday to:

Code: Select all


MyString[0] = 0x7E;
MyString[1] = 0x00;
MyString[2] = 0x17;

RS232.SendChar(MyString[0]);
RS232.SendChar(MyString[1]);
RS232.SendChar(MyString[2]);

Will do my work and sends the 0x00 well
a "string" with 50 char ... is many write work..
and so if the "string" is very long i use a while with .i variable for length of the string ( and this was the wrong thinking from me, because the lengt is cut too with 0x00 in a string ;-) )

"pseudo.. likewise"

Code: Select all


MyString[0] = 0x7E;
MyString[1] = 0x00;
MyString[2] = 0x17;
int i = 0;

while (i < Length$(MyString) )
{
RS232.SendChar(MyString[i]);
i++;
}
and wounder me because only one "character" was send ;-) ;-) ( 0x00 cuts the length% function too ;-) )

ok, i think i have understand yours now..let me try ;-) ;-)..
my thinking wrong is,, all what is "string" theme - it will cut ( null terminated ) if 0x00 in it
length$..
printf("%s",MyString);

ok i think i have understand - this the same like in MIDI Theme with sending "char" as Hex..

The first ( from me ) think .. was wrong

pseudo .. ( forget this code block this was a wrong thinking from me... but i will write down here.. )..

Code: Select all


INT MyData[30] ) ; // generate a INT Array of Bytes with 30 fields.. ( i will look next time how i can set all fields to empty values ( initialize ) ) in one way.. in C )

MyData[0] = 0x7E;
MyData[1] = 0x00;
MyData[2] = 0x17;
..
..
// Is there a Function in Flowcode to check how many fields are used? i_used = idx 0 .XX. 1.XX. 2.XX. 3.XX. 4.// 5.//
// 4 Fields are used - so a while can run 4 times for sending..

int i_used ....

while (i < i_used )
{
RS232.SendChar(MyString[i_used]);
i_used++;
}
forget the code block...



i think this is the right you are mean ;-)...

Pseudo mix ;-)

Code: Select all


char MyDatagram[] = {0x7E, 0x00, 0x65, 0x00, 0x16, 0x28, 0x10, 0x66};
size_t MyDatagramSize = sizeof(MyDatagram);


for (int i=0; i<MyDatagramSize; i++)
{
RS232.sendchar(MyDatagram[i]);
}

Ok ? ;-)

.. i hope.. because i allways learn from simple the Best ;-)

Thank you Benj, Jac and LeighM for allways right / properly food for thought!!!


Best wishes!!!
Rudi
;-)