How can I send strings of data to an XBee with a python library?

Valkyrie Savage picture Valkyrie Savage · Nov 18, 2012 · Viewed 18.7k times · Source

Which library should I be using, and how?

Python XBee seems to be only able to send commands in API mode, and I can't find an example of anyone using it to send a string. Maybe I'm misunderstanding what API mode is, but I can't find a payload in the documentation...

Are Digi's Python Socket extensions baked into Python? I can't seem to get any of the constants they claim to have defined in my Python (2.7.3rc2), nor can I find a mention of how to get these extensions on their site. It seems like that could be a way to pass around strings, but how do I use it?

Answer

Tim picture Tim · Nov 18, 2012

If the Xbee is connected to the computer as a serial device you can just use a serial library such as pySerial. Here are some code snippets from a project I just finished.

# Connect to Xbee
self.ser = serial.Serial(port, baud, timeout=timeout)

# Send data (a string)
self.ser.write(packet)

# Read data
self.data += self.ser.read()

We were using the Xbees in transparent mode - each byte you write on one end is visible on the other end with a read. There was no need for a special Xbee library.