niolabs / python-xbee

Python tools for working with XBee radios
MIT License
101 stars 45 forks source link

Any example to transfer a string data? #44

Closed nejdetckenobi closed 7 years ago

nejdetckenobi commented 7 years ago

I couldn't find a complete example about transferring and reading a string data. I installed the package but I can not use it properly. I also checked this question, but this one is also not working.

Can you provide sender and receiver code? Your docs are really hard to read.

nejdetckenobi commented 7 years ago

I use the following two code samples.

# listen.py
from xbee import XBee                                                                                                                                                                                                                   from serial import Serial

s = Serial('/dev/ttyUSB0', 9600)

xbee = XBee(s)

r = xbee.wait_read_frame()

print(r)
# send_data.py
from xbee import XBee
from serial import Serial

PORT = '/dev/ttyUSB0'
BAUD = 9600

ser = Serial(PORT, BAUD)
xbee = XBee(ser)

xbee.tx(dest_addr=b'\xcf\x56', data=b'HEREISMYDATA')

I get following error on listen.py

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/xbee/base.py", line 233, in _split_response
    packet = self.api_responses[packet_id]
KeyError: b'\x01'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "listen.py", line 8, in <module>
    r = xbee.wait_read_frame()
  File "/usr/local/lib/python3.4/dist-packages/xbee/base.py", line 410, in wait_read_frame
    return self._split_response(frame.data)
  File "/usr/local/lib/python3.4/dist-packages/xbee/base.py", line 241, in _split_response
    % (data[0], cmd_name))
xbee.base.CommandFrameException: "Incoming frame with id 1 looks like a command frame of type 'tx' (these should not be received). Are you sure your devices are in API mode?"

Am I doing it right or? (This error has been fixed. One of my devices was in AT mode. Please see the comment below.)

jamesleesaunders commented 7 years ago

Hi @nejdetckenobi The first question is "Are you sure your devices are in API mode?". You are seeing this error because the receiving end listen.py is seeing a frame of type 'tx' (type 0x01) coming in. Although this is the frame you have requested to be sent from send_data.py I believe you would expect it to be received as type 'rx' (type 0x81) by listen.py.

If you look at the code in /xbee/ieee.py you will see two lists:

If the library detects one of the api_commands incoming it will throw the error you see: "Incoming frame with id 1 looks like a command frame of type 'tx' (these should not be received). Are you sure your devices are in API mode?"

I am not 100% sure of your situation but it looks like your outgoing 'tx' frame is not being translated to an incoming 'rx' frame at the other end - possibly API mode not enabled on all XBees?

nejdetckenobi commented 7 years ago

Hi again. 1- I created a PAN by using an XBee Controller API firmware 2- With the second XBee, I joined the network so I got a MY address.

Here is a screenshot to show my state. ss

Now listener receives nothing. Any ideas?

(Note: I updated the dest_addr parameter)

jamesleesaunders commented 7 years ago

Have you tried using: xbee = Zigbee(ser) On the listening XBee have you tried monitoring the incoming frames (if any) using XCTU console?

I am happy to try and help but really these sorts of questions should be posted on Stack Overflow and including the tags 'python' and 'xbee'. I don't think this is an issue with the library code so I will close this issue down, but please do feel free to respond to this comment on GitHub also (your eventual solution may help another user).

I also found this which may help: https://www.digi.com/support/forum/5656/xbee-2-5-network-without-coordinator