Shop OBEX P1 Docs P2 Docs Learn Events
simple xbee rx question... — Parallax Forums

simple xbee rx question...

MrJohnnyCanuckMrJohnnyCanuck Posts: 4
edited 2021-10-26 23:23 in Propeller 1

I had a working sample of code for two Xbee modules to communicate back and forth with propellers, but I've lost it all. I managed to get the propeller to send out data, but I've been unable to get it to receive data let alone parse it. I've looked all over without any luck for a simple sample to start from. Here's what I have:
Basically, three bytes of data arrive; it's parsed into a three digit decimal number, and if it's 138 then output 15 should turn on.
I hope you can understand what I'm trying to acheive.

if sio.rxcheck < 0
  DataIn[0] := SIO.Rx
  DataIn[1] := SIO.Rx
  DataIn[2] := SIO.Rx
  sio.rxtime(2)
  sio.rxflush

if DataIn[0] <> 0   
  if DataIn[0] == 48
    DataValOne := 0
  if DataIn[0] == 49
    DataValOne := 1
  if DataIn[0] == 50
    DataValOne := 2
  if DataIn[0] == 51
    DataValOne := 3
  if DataIn[0] == 52
    DataValOne := 4
  if DataIn[0] == 53
    DataValOne := 5
  if DataIn[0] == 54
    DataValOne := 6
  if DataIn[0] == 55
    DataValOne := 7                
  if DataIn[0] == 56
    DataValOne := 8
  if DataIn[0] == 57
    DataValOne := 9

if DataIn[1] <> 0
  if DataIn[1] == 48
    DataValTwo := 0
  if DataIn[1] == 49
    DataValTwo := 1
  if DataIn[1] == 50
    DataValTwo := 2
  if DataIn[1] == 51
    DataValTwo := 3
  if DataIn[1] == 52
    DataValTwo := 4
  if DataIn[1] == 53
    DataValTwo := 5
  if DataIn[1] == 54
    DataValTwo := 6
  if DataIn[1] == 55
    DataValTwo := 7                
  if DataIn[1] == 56
    DataValTwo := 8
  if DataIn[1] == 57
    DataValTwo := 9

if DataIn[2] <> 0
  if DataIn[2] == 48
    DataValThree := 0
  if DataIn[2] == 49
    DataValThree := 1
  if DataIn[2] == 50
    DataValThree := 2
  if DataIn[2] == 51
    DataValThree := 3
  if DataIn[2] == 52
    DataValThree := 4
  if DataIn[2] == 53
    DataValThree := 5
  if DataIn[2] == 54
    DataValThree := 6
  if DataIn[2] == 55
    DataValThree := 7                
  if DataIn[2] == 56
    DataValThree := 8
  if DataIn[2] == 57
    DataValThree := 9

DataOutOne := (100DataValOne)+(10DataValTwo)+DataValThree

if DataOutOne == 138
outa[15]~~

Regards,
JohnnyCanuck

Comments

  • Had this example in my library written by Tracey Allen

  • Thanks DigitalBob, there's alot to unpack here, hopefully I will have an epiphany - fingers crossed.

  • JonnyMacJonnyMac Posts: 9,159
    edited 2021-10-27 03:15

    I think you'll find this method cleaner and easier than what you're doing.

    pub get_command : value | c
    
      repeat                                                        ' clear trash from buffer
        c := xb.rxtime(2)
        if (c < 0)
          quit
    
      ' get 3-digit command
    
      repeat 3
        c := xb.rx
        if (c => "0") and (c =< "9")                                ' digit?
          value := (value * 10) + (c - "0")
        else
          quit                                                      ' early exit if illegal char
    

    Now you can do something like this

      if (get_command = 138)
        outa[15] := 1
    

    Two notes: 1) Give your pins names -- don't use magic numbers as in out[15], and 2) The use of := 1 is more obvious that the post ~~, and is a tad faster, too.

    By the way, if you want to send a 2-digit command, just use a non-digit third character. If you send "78." the return value will be 78.

  • JohnnyMac, thankyou for your example and explanation (and advice too)! Really appreciated!

  • If you want a really comprehensive Xbee guide, download Parallax 122-32450 Xbee tutorial by Martin Hebel.
    Full title "Getting started with Xbee RF modules".

  • Hi

    I thought that would be interesting so I did a search on both "122-32450 Xbee tutorial" and "Getting started with Xbee RF modules" on the main website and was unable to find it!!!
    Chance of a link please?

    Dave

  • PublisonPublison Posts: 12,366
    edited 2021-10-28 12:07
  • Hi

    Thanks for that.
    Why could I not find it?

    Dave

  • I sometimes go back to the original site and do searches there. Sometimes I get more hits for older products.

  • Checking the basics. That demo of mine that Digital Bob linked in post #2 assumes that the two xBees are already configured so that they can talk to one another, that is, the receiver's (destination) address matches the address transmitted (source) address. When you receive a 802.15.4 xBee out of the box, they are usually configured in "transparent" mode with address zero for both source and destination. Two such xBees should be able to talk to one another, out of the box. Other configurations with exclusive addresses are set either via the XCTU program on the PC, or via your firmware (e.g. Martin Hebel's tutorial). Are your xBees new? If not, the addresses might not match. Also, there are xBee protocols that go beyond 802.15.4, for example, xBees loaded with Zigbee and Digimesh firmware. Martin's tutorial applies specifically to 802.115.4 xBees.

    My terminal program or one like it is useful for verifying communication. It just sends out whatever you type and displays whatever comes in character by character.

  • @MrJohnnyCanuck said:
    I had a working sample of code for two Xbee modules to communicate back and forth with propellers, but I've lost it all. I managed to get the propeller to send out data, but I've been unable to get it to receive data let alone parse it. I've looked all over without any luck for a simple sample to start from. Here's what I have:
    Basically, three bytes of data arrive; it's parsed into a three digit decimal number, and if it's 138 then output 15 should turn on.
    I hope you can understand what I'm trying to acheive.

    if sio.rxcheck < 0
      DataIn[0] := SIO.Rx
      DataIn[1] := SIO.Rx
      DataIn[2] := SIO.Rx
      sio.rxtime(2)
      sio.rxflush
    
    if DataIn[0] <> 0   
      if DataIn[0] == 48
        DataValOne := 0
      if DataIn[0] == 49
        DataValOne := 1
      if DataIn[0] == 50
        DataValOne := 2
      if DataIn[0] == 51
        DataValOne := 3
      if DataIn[0] == 52
        DataValOne := 4
      if DataIn[0] == 53
        DataValOne := 5
      if DataIn[0] == 54
        DataValOne := 6
      if DataIn[0] == 55
        DataValOne := 7                
      if DataIn[0] == 56
        DataValOne := 8
      if DataIn[0] == 57
        DataValOne := 9
                
    if DataIn[1] <> 0
      if DataIn[1] == 48
        DataValTwo := 0
      if DataIn[1] == 49
        DataValTwo := 1
      if DataIn[1] == 50
        DataValTwo := 2
      if DataIn[1] == 51
        DataValTwo := 3
      if DataIn[1] == 52
        DataValTwo := 4
      if DataIn[1] == 53
        DataValTwo := 5
      if DataIn[1] == 54
        DataValTwo := 6
      if DataIn[1] == 55
        DataValTwo := 7                
      if DataIn[1] == 56
        DataValTwo := 8
      if DataIn[1] == 57
        DataValTwo := 9
        
    if DataIn[2] <> 0
      if DataIn[2] == 48
        DataValThree := 0
      if DataIn[2] == 49
        DataValThree := 1
      if DataIn[2] == 50
        DataValThree := 2
      if DataIn[2] == 51
        DataValThree := 3
      if DataIn[2] == 52
        DataValThree := 4
      if DataIn[2] == 53
        DataValThree := 5
      if DataIn[2] == 54
        DataValThree := 6
      if DataIn[2] == 55
        DataValThree := 7                
      if DataIn[2] == 56
        DataValThree := 8
      if DataIn[2] == 57
        DataValThree := 9
                                                                     
    

    DataOutOne := (100DataValOne)+(10DataValTwo)+DataValThree

    if DataOutOne == 138
    outa[15]~~

    Regards,
    JohnnyCanuck

    @JonnyMac said:
    I think you'll find this method cleaner and easier than what you're doing.

    pub get_command : value | c
    
      repeat                                                        ' clear trash from buffer
        c := xb.rxtime(2)
        if (c < 0)
          quit
    
      ' get 3-digit command
    
      repeat 3
        c := xb.rx
        if (c => "0") and (c =< "9")                                ' digit?
          value := (value * 10) + (c - "0")
        else
          quit                                                      ' early exit if illegal char
    

    Now you can do something like this

      if (get_command = 138)
        outa[15] := 1
    

    Two notes: 1) Give your pins names -- don't use magic numbers as in out[15], and 2) The use of := 1 is more obvious that the post ~~, and is a tad faster, too.

    By the way, if you want to send a 2-digit command, just use a non-digit third character. If you send "78." the return value will be 78.

    @JonnyMac said:
    I think you'll find this method cleaner and easier than what you're doing.

    pub get_command : value | c
    
      repeat                                                        ' clear trash from buffer
        c := xb.rxtime(2)
        if (c < 0)
          quit
    
      ' get 3-digit command
    
      repeat 3
        c := xb.rx
        if (c => "0") and (c =< "9")                                ' digit?
          value := (value * 10) + (c - "0")
        else
          quit                                                      ' early exit if illegal char
    

    Now you can do something like this

      if (get_command = 138)
        outa[15] := 1
    

    Two notes: 1) Give your pins names -- don't use magic numbers as in out[15], and 2) The use of := 1 is more obvious that the post ~~, and is a tad faster, too.

    By the way, if you want to send a 2-digit command, just use a non-digit third character. If you send "78." the return value will be 78.

Sign In or Register to comment.