如何使用变量而不是数据包[“source_addr_long”]

时间:2015-03-07 17:08:47

标签: python xbee

我是bhushan。我是python编程的新手。

我已经在API模式下编写了xbee。

终端设备1 - > Router1 - >协调员。
终端设备2 - > Router2 - >协调员。

协调员需要选择路由器1或路由器2来回复

  xbee1.tx(frame='0x1'
           , dest_addr=packet["source_addr"] 
           , data='Hi' 
           , dest_addr_long=packet["source_addr_long"]
          )  # It Works.

我想使用router1或router2的地址而不是数据包[" source_addr_long"]。必须在运行时做出决定。

我如何存储和检索地址?

 print source_addr_long1

输出 - \ x00 \ x13 \ xa2 \ x00 \ x40 \ xb5 \ xad \ x6e

 print  b"".join( byte for byte  in  source_addr_long1 )

输出 - \ x00 \ x13 \ xa2 \ x00 \ x40 \ xb5 \ xad \ x6e

 print  b"".join( byte for byte  in  "\x00\x13\xa2\x00\x40\xb5\xad\x6e") 

输出 - 不可打印代码

 destaddr = "\x00\x13\xa2\x00\x40\xb5\xad\x6e"
 xbee1.tx(frame='0x1'
           , dest_addr=packet["source_addr"] 
           , data='Hi' 
           , dest_addr_long=destaddr 
          )  # It Work.

当我使用字符串时,它会给我错误

 xbee1.tx(frame='0x1' ,
          dest_addr=source_addr1 ,
          data='Hi' , 
          dest_addr_long=source_addr_long1
          ) # Not Works



     xbee1.tx(frame='0x1' , dest_addr=source_addr1 , data='Hi' ,   dest_addr_long=source_addr_long1) # Not Works
   File "I:\Python27\lib\site-packages\xbee\base.py", line 418, in <lambda>
return lambda **kwargs: self.send(name, **kwargs)
   File "I:\Python27\lib\site-packages\xbee\base.py", line 386, in send
self._write(self._build_command(cmd, **kwargs))
   File "I:\Python27\lib\site-packages\xbee\base.py", line 195, in _build_command
% (field['name'], field['len']))
 ValueError: The data provided for 'dest_addr_long' was not 8 bytes long

1 个答案:

答案 0 :(得分:0)

最后我得到了答案。

我有以下情况。

  source_addr  = ['0x8f', '0x18']

  source_addr_long =  ['0x00', '0x13', '0xa2', '0x00', '0x40', '0xb5', '0xad', '0x6e']


 # Coordinator transmit packet to any router .
 # Decision  can be taken at run time by  coordinator.
 # One to many nodes transmission is thus possible.

 xbee1.tx(
   frame='0x1' 
 , dest_addr=bytearray( int(x,16) for  x in source_addr)
 , data='Hi' 
 , dest_addr_long= bytearray( int(x,16) for  x in source_addr_long)

# It Works .
#  hex(19) => '0x13'
#  int('0x13',16)  => 19

#  x = '0x13' 
# then int(x,16) will be 19