Big Endin to little Endin
RS_Jim
Posts: 1,764
Hi,
I wish to send two 10 Bit A/D values as 2 succesive words from an Aurdino Device to a Prop. The Prop is coded in Spin. In the Prop, I want the two words to endup in two longs. Any suggestion as to the bestway to code this in the Prop. The link between the two is serial, starting as wired moving to wireless once it is working. The link works iin ASCII(using 4portSerialPlus) but I don't want to deal with figuring out how many characters are involved with each word as the values can range from 0 to 2500.
Thanks in advance for any suggestions.
Jim
I wish to send two 10 Bit A/D values as 2 succesive words from an Aurdino Device to a Prop. The Prop is coded in Spin. In the Prop, I want the two words to endup in two longs. Any suggestion as to the bestway to code this in the Prop. The link between the two is serial, starting as wired moving to wireless once it is working. The link works iin ASCII(using 4portSerialPlus) but I don't want to deal with figuring out how many characters are involved with each word as the values can range from 0 to 2500.
Thanks in advance for any suggestions.
Jim
Comments
Massimo
-Phil
You need to reserve a byte value for "a number is coming" which will never be used for anything else anywhere. Control codes like <stx> are good for this. You also need to reserve end bytes that are not numbers to identify each type of number you might receive. Let's say letters A and B, which are not numbers, identify your A/D values. In between you will have digits:
<stx>729A<stx>44B etc.
Now in your receive code you begin by waiting for the <stx>, ignoring everything. This has the advantage of synchronizing things if you get out of step. When you receive <stx> you go into a loop like this:
What this does: After you get the <stx> to say a number is coming, you set the outvaue to zero and get the next character. If the upper nibble is 3, it's an ASCII number (or a punctuation mark, but we'll assume not) and you mask the lower nibble, multiply the existing outvalue by 10, and add the lower nibble to the outvalue. In this way your outvalue progresses:
0 * 10 + 7 = 7
7 * 10 + 2 = 72
72 * 10 + 9 = 729
A = write it to A.
There are other ways to do this; you could use a prefix to identify the data and a common suffix like CR, but then you have to store the prefix until the number is complete to remember what to do with it. This is a method that doesn't care how many digits there are, is easy to code, and doesn't require a buffer or library to implement.
Jim