Shop OBEX P1 Docs P2 Docs Learn Events
Big Endin to little Endin — Parallax Forums

Big Endin to little Endin

RS_JimRS_Jim Posts: 1,753
edited 2018-03-28 14:29 in Propeller 1
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

Comments

  • max72max72 Posts: 1,155
    If data is sent as a string could you use the string to number conversion objects (numbers, simple numbers..)

    Massimo
  • RS_Jim wrote:
    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 cane range from 0 to 2500.
    IOW, the values range from 0000 to 2500, right? (Always four digits.)

    -Phil
  • This depends heavily on what sort of protocol you want to implement. Here's one that makes everything very simple and doesn't require a receive buffer.

    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:
    outvalue := 0
    repeat
      c := ser.rx
      if (c & $F0) == $30
        outvalue *= 10
        outvalue += (c & $F)
      else
        quit
    case c
      "A": 'Write it to A
      "B": 'Write it to B
    

    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.
  • Thanks all for the ideas. I like PhiPi's suggestion of always 4 digits. I may try sending leading zeros and see what that leads to.
    Jim
Sign In or Register to comment.