Python xbee library no output for incoming frames

大憨熊 提交于 2019-12-08 04:41:20

问题


I am using 2 XBee pro S1, I want to read the packets received by the co-ordinator on my PC , it is enabled with API_2 and all other connections are done properly, I can see the packets with XCTU, I am using the python xbee library , but it gives no output :

The Code :

import serial.tools.list_ports
from xbee import XBee
import serial

ports = list(serial.tools.list_ports.comports())

for p in ports:  #print the list of ports
    print p

def toHex(s):
    lst = []
    for ch in s:
        hv = hex(ord(ch)).replace('0x', '')
        if len(hv) == 1:
            hv = '0'+hv
        hv = '0x' + hv
        lst.append(hv)

def decodeReceivedFrame(data):
        source_addr_long = toHex(data['source_addr_long'])
        source_addr = toHex(data['source_addr'])
        id = data['id']
        samples = data['samples']
        options = toHex(data['options'])
        return [source_addr_long, source_addr, id, samples]

PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600

ser = serial.Serial(PORT, BAUD_RATE)
print "Serial ports initialised...."

xbee = XBee(ser,escaped=True)

print "XBee object created"

while True:
  try:
    response = xbee.wait_read_frame()
    sleep(0.5)
    decodedData = decodeReceivedFrame(response)
    print decodedData
    print "data decoded"
  except KeyboardInterrupt:
    break

ser.close()

The port number and baudrate are connect, I change it to the appropriate portnumber every time I replug the coordinator to my PC. My output looks like :

Serial ports initialised....
XBee object created

It stays like that and gives no output, even if I see the RX led blinking. Below is the code written with only pyserial :

import serial
from time import sleep

port = '/dev/ttyUSB0'
baud = 9600

ser = serial.Serial(port, baud) 

data = ""

while True:
 try:
   while ser.in_waiting:
    sleep(1)
    data = ser.read()
    print data

except KeyboardInterrupt:
    break

ser.close()

It gives the following output.

Could someone kindly help.


回答1:


Are you sure you have the correct serial port and baud rate? Does the xbee package support API mode 2? It might only work with API mode 1.

Does that package have methods for accessing the raw byte stream instead of trying to read frames? Can you configure it to throw exceptions on parsing errors?

I would start with just printing response until you see that you're receiving data. And why include the sleep() call in that loop?

I'm not sure what you're trying to accomplish in toHex() but you might want to look at the Python method struct.unpack() or replace all of the work you do on hv with '0x%02X' % ord(ch).



来源:https://stackoverflow.com/questions/44839542/python-xbee-library-no-output-for-incoming-frames

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!