Shop OBEX P1 Docs P2 Docs Learn Events
help with xbee and propeller — Parallax Forums

help with xbee and propeller

tdahburatdahbura Posts: 6
edited 2011-06-23 09:34 in Accessories
I am trying to get some code that is written in C to run using xbee communications. I am using the ICC c compiler. is there any c code available similar to the propeller xbee object that supports using xbee communcations.

I asked on the prop forum about merging C code with spin code but in experimenting and based on suggestion that does not seem possible-although I tried a great utility to cross convert c code to spin (which did not due to the complexity of this existing c code).

Was hoping someone might have some libraries to support XBee communications in C?

I need to send blocks of numbers (like long values) and pass data back and forth between two propeller xbee units.

Thanks,
Tony

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-06-22 08:52
    I would start by drawing up a flowchart of what you want the program to do then break it down into sections and code them one at a time. If you could explain what it is you are having trouble with that might help.
  • johnfosjohnfos Posts: 59
    edited 2011-06-23 03:54
    Since you're only using two XBees, the simplest way to do it would be to configure them to send to each other in transparent mode. That way, they act as a simple two-way serial channel. Whatever data you write at one end comes out at the other, and you don't need any special C code at all.

    The default configuration is almost all you need: just then set each XBee's destination address to be the source address of the other, and it should work.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-06-23 09:34
    I've noticed there are two different ways of sending information from Prop to Prop. One way is to have an end of transmission character. The other is to state the size of the transmission somewhere in the preamble.

    I've used the first method myself. The problem with an end of transmission character is you need to make sure none of the data includes such a character.

    In order to make sure the data doesn't include the end of transmission character you need to convert numeric data to ASCII characters. Recently I've decide to encode my data as ASCII hexadecimal character. This method requires two characters be transmitted for every byte of data.

    Here's some of the methods I use to convert the data.

    This first method lists the variables to add to the tx buffer.
    PUB SendData(localController)         
      if localController == Header#_NoController
        Tbug(string(13, "No Controller Found"))
        Tbug(string(13, "Robot is in autonomous mode."))
        Tbug(string(13, "Ready to receive data."))
        return                
      setCount++  '  I add a count byte to each tx so I know if all txs get through.
      if setCount == 13 or setCount == 0  ' I use 13 for my end of tx character.  
                                          ' I also exclude 0 in case I'm using strings.  
                                          ' I probably don't need to exclude it.  
                                          ' The rx side know not to expect these numbers.
        setCount++
      nordicTxBuffer[4] := setCount
      bytefill(@nordicTxBuffer + 7, 0, payloadSize - 8)  ' clear the tx buffer
      nordicTxBuffer[7] := localController      ' additional information include in my preable
      if localController == Header#_WiiNunchuck
        Nunchuck.readNunchuck
        Hex(jx, 2, @nordicTxBuffer + 8)   ' add word variables to tx buffer
        Hex(jy, 2, @nordicTxBuffer + 10)
        Hex(bc, 2, @nordicTxBuffer + 12)
        Hex(bz, 2, @nordicTxBuffer + 14)
        Hex(axRaw, 4, @nordicTxBuffer + 16)   ' add long variables to tx buffer
        Hex(ayRaw, 4, @nordicTxBuffer + 20)
        Hex(azRaw, 4, @nordicTxBuffer + 24)
        nordicTxBuffer[28] := 13
      elseif localController == Header#_PlayStation2
        PsData1 := PS2.get_Data1  
        LoadHex(PsData1, @nordicTxBuffer + 8)
        PsData2 := PS2.get_Data2 
        LoadHex(PsData2, @nordicTxBuffer + 16)
        nordicTxBuffer[24] := 13
      else
        Tbug(string(13, "Error, No Controller Found"))
        Tbug(string(13, "Something has gone wrong."))
        Tbug(string(13, "The program needs to be fixed."))
      Tbug(string(13, "Before TransmitString"))
      SafeBug(@nordicTxBuffer, payloadSize)
     
      Nordic.TransmitString(@nordicTxBuffer)
    

    Below is a method just to add longs which are always eight characters long.
    PUB LoadHex(valueToChop, localPtr)
     
      hex(valueToChop, 8, localPtr)
    

    The above methods called this next method which does the actual conversion.
    PUB hex(value, digits, localPtr)
      value <<= (8 - digits) << 2
      repeat digits
        byte[localPtr++] := lookupz((value <-= 4) & $F : "0".."9", "A".."F")
     
    

    The above adds the appropriate characters to the tx buffer.

    Here's code to retreive the information from the ASCII characters.
    PUB FromAsciiHex(digits, localPtr) | localCharacter  
      repeat digits
        localCharacter := byte[localPtr++]
        if localCharacter => "0" and localCharacter =< "9"
          localCharacter -= "0"
        elseif localCharacter => "A" and localCharacter =< "F"
          localCharacter -= "A"
          localCharacter += $0A
        else
          localCharacter := 0
        result <<= 4
        result += localCharacter
     
    

    The maximum value for digits in the above method is eight since there are eight ASCII characters per long and the above method returns a long.

    I'm not using XBees in this particular project but the information applies to any Prop to Prop transmission.

    Hopefully some of this will be useful.

    Duane
Sign In or Register to comment.