1

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Pratik Kumar
  • 2,211
  • 1
  • 17
  • 41

1 Answers1

0

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).

tomlogic
  • 11,489
  • 3
  • 33
  • 59
  • By only using the pyserial library I am getting some output(not the desired output) ,it is not readable . – Pratik Kumar Jun 30 '17 at 21:21
  • The serial port and baud rate are correct, I check the serial port every time I connect it to the computer ,baud rate is the default, I also tried it with API_1 mode but no success, I added the sleep because I assumed that before the serial buffer was full I was trying to read (I just tried this randomly) . – Pratik Kumar Jun 30 '17 at 21:26
  • Are the packets coming through as API frames in XCTU? Meaning they start with 0x7E (~), two-byte length, checksum, etc.? What does it look like in the XCTU terminal in hex mode? (And I assume when we're saying "API_1" you're setting `ATAP=1` in XCTU.) – tomlogic Jun 30 '17 at 21:46
  • Yes, they come out as hexadecimal – Pratik Kumar Jul 01 '17 at 02:47
  • No need for the `sleep()`, you will only read if there are bytes (`ser.in_waiting`). Try printing out `hex(data)` so you can see what the bytestream looks like. You can recognize the API frames that way. They start with `0x7E`. – tomlogic Jul 01 '17 at 05:51
  • I updated the firmware(reninstalled the same version) and set to API_1 , now the xbee library works fine and I can see the data. – Pratik Kumar Jul 01 '17 at 05:57
  • print hex(data) , gives me `TypeError: hex() argument can't be converted to hex` – Pratik Kumar Jul 01 '17 at 05:59
  • Sorry, should be `hex(ord(data))`. Glad you got it working. – tomlogic Jul 01 '17 at 06:23