Read data from an xbee using sockets on ConnectPort

After browsing the examples on the Digi cd that comes packaged with the ConnectPort, I decided to start rolling some code examples that actually combine some fundemental concepts. The code here, finds an xbee by it’s label or NI, then reads data from it.

import zigbee
from socket import *

################################################################################
def getZigbeeAddressByLabel( strLabel ):
    '''
    given the label of a zigbee/xbee device, find it's address. The label is
    often set using X-CTU and setting the "Node Identifier" or NI attribute.
    
    '''
    
    zigbeeNodeList = zigbee.getnodelist()
    
    for oNode in zigbeeNodeList:
        if strLabel == oNode.label:
            return (True, oNode.addr_extended)
        
    return (False, None)
    


################################################################################
def readDataFromZigbee( strAddr, iLengthToRead ):
        
    # The Format of the tuple is:
    #  (address_string, endpoint, profile_id, cluster_id)
    #
    # The values for the endpoint, profile_id, and
    # cluster_id given below are the values used to write
    # to the serial port on an Ember-based XBee module.
    DESTINATION=(strAddr, 0xe8, 0xc105, 0x11)
    
    # Create the socket, datagram mode, proprietary transport:
    sd = socket(AF_ZIGBEE, SOCK_DGRAM, ZBS_PROT_TRANSPORT)
    
    # Bind to endpoint 0xe8 (232):
    sd.bind(("", 0xe8, 0, 0))
    
    # Block until a some amount of data is read
    (strData, src_addr) = sd.recvfrom( iLengthToRead )
    
    sd.close()
    
    return strData


################################################################################
if "__main__" == __name__:
    (ok, strAddr) = getZigbeeAddressByLabel( "_GH1" )
    
    data = readDataFromZigbee( strAddr, 16 )
    
    print data