Shop OBEX P1 Docs P2 Docs Learn Events
Understanding Variable Arrays, NIB0, NIB1, etc. — Parallax Forums

Understanding Variable Arrays, NIB0, NIB1, etc.

xanatosxanatos Posts: 1,120
edited 2009-06-25 04:53 in BASIC Stamp
Hi,

I am transmitting from one BS2sx to another via·2 XBees,·a string that contains 3 bytes strung together:

· SEROUT XTX, Baud, [noparse][[/noparse]DEC2 space, DEC2 hpaid, DEC2 mpaid, CR]·· ' Send vars

On the receive side, I am happily replicating this concatenated string using this code:

· SERIN RX, 84, [noparse][[/noparse]STR X\7\CLS]

When I·· DEBUG STR X, CR· I get the six digit code displayed perfectly.

I have defined "X" on the receive side as:

· X······VAR··· Byte

What I would LIKE to do is to make X an array of 3 bytes (· X VAR BYTE(3) ) and then break up the string so I can reference them along the lines of·· ---· space = X(1)·· hours = X(2)· mins = X(3) --- OR - I could also be perfectly happy with something like X.NIB0, X.NIB1, X.NIB2.

HOWEVER - when I try any of these things, my data goes to mud.· I admit some remaining confusion on how this aspect of data handling works.· Please, don't tell me to read the manual, I have it burned into my retinas, I'm just not understanding some aspect of this.· I have tried various data display formats to no avail (DEC2, HEX2) - the only way I can properly replicate the data is the DEBUG STR X line as indicated above.

Can one of ye enlightened ones tell what I am missing and provide a bit of guideance so I can properly and reliably break this string into its component parts?

Thanks VERY much,

Dave X


·

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-06-25 00:59
    Dave,
    It is transmitting 6 bytes, and you are receiving those 6 bytes as a string. Here are a couple of ways you could do this.

    1) receive the data as numerical values instead of a string.
    space VAR byte
    hours VAR byte
    mins VAR byte
    SERIN RX\xrts, 84, [noparse][[/noparse]DEC space, DEC hours, DEC mins]
    DEBUG DEC space, TAB, DEC hours, DEC mins

    Note that I changed the SERIN command a little bit to use RX\xrts. xrts is the flow control line from the XBee to the Stamp, so that the XBee won't send data too fast for the Stamp. May or may not need this, but the DEC modifier is slower for the Stamp to execute than the STR modifier.

    2) receive the data as you are now, but then convert the string values to numerical values:
    X VAR byte(6) '<---needs to be defined as an array
    SERIN RX, 84, [noparse][[/noparse]STR X\6] ' <--- six chars, may need timeout
    space = (X(0)-$30)*10 + (X(1) - $30) <--- two ascii codes for each decimal value
    hours = (X(2)-$30)*10 + (X(3) - $30)
    mins = (X(4)-$30)*10 + (X(5) - $30)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • xanatosxanatos Posts: 1,120
    edited 2009-06-25 01:32
    Hi Tracey,

    Thanks! The second version appears to work reasonably well. While I prefer the concept of the first version, two things happen. First, the xrts thing comes back as "undefined symbol"; when I run the code without it, it displays... nothing. I can make several transmissions, and I get nothing on the RX side's debug screen.

    I've tried a few things - all of which result is SOME data displaying, but it's always wrong. For example, this code:

    SERIN RX, 84, [noparse][[/noparse]space, hours, mins]
    DEBUG DEC2 space, TAB, DEC2 hours, DEC2 mins, CR

    Which receives the data string 122045, yields 49 322

    If I use the DEC, or DEC2 formatters in the SERIN line, I again get nothing.

    So - as I said - I would PREFER to use the first method you delineated, and if you can figure out what's going on there, it would be great, although the second method is functional except for it losing any leading zeros on the number pairs.

    THANK YOU! Whatever we find on the first code option, at least I have something working now! smile.gif

    Dave
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-06-25 02:44
    I had a couple of mistakes in my example 1, and correcting them might help.

    1) receive the data as numerical values instead of a string.
    space VAR byte
    hours VAR byte
    mins VAR byte
    SERIN RX, 84, [noparse][[/noparse]DEC2 space, DEC2 hours, DEC2 mins]   <--- DEC2 instead of just DEC
    DEBUG DEC space, TAB, DEC hours, TAB, DEC mins
    



    I left out the xrts in the code above. To use xrts, you have to set aside a pin with that name on the BASIC Stamp to be used as flow control, and that pin has to be physically connected to the flow control input on the XBee. You can read more about that in the examples, and in the discussion on Marin Hebel's web site[noparse][[/noparse]/url. The reason you might need flow control is that the BASIC Stamp takes some time to process the DEC2 modifier, and the XBEE might send the next value before the Stamp is ready to receive it. The purpose of the xrst is to enable the Stamp to tell the XBee when it is ready.

    The XBEE by default packetizes the data that travels through the air waves, but it is possible to set its pacetization wait time using the ATRO command. By using ATRO 0 setting, I think you could then use the transmitting SX to pace out the data a little bit:
    SEROUT XTX, Baud,5, [noparse][[/noparse]DEC2 space, DEC2 hpaid, DEC2 mpaid, CR]   ' Send vars
    


    Added*5 mS pacing*. I think that might work even without changing the ATRO setting, because the default is 3 milliseconds at 9600 baud.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • xanatosxanatos Posts: 1,120
    edited 2009-06-25 03:01
    This is great help, I am very grateful for your assistance. I am controlling multiple relays now from a web page on a PINK server, via XBees, from about 800 feet away. This is SO cool! And I've only been working with Parallax's products about 2 months now. I'm just amazed at the level of knowledge and willingness to help I've found here. I'm truly grateful. I hope I can contribute as much over time.

    You've made my night! I can actually SLEEP tonight!!! smile.gif

    Thanks again,

    Dave X
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-06-25 04:53
    Good progress! If the transmitter is a BS2sx, try 13 instead of 5 for the transmitter pacing. The unit of pacing on the BS2sx is 0.4 ms, so to get 5 milliseconds between characters you need 13*0.4 = 5.2 ms.

    SEROUT XTX, Baud,13, [noparse][[/noparse]DEC2 space, DEC2 hpaid, DEC2 mpaid, CR]   ' Send vars
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.