Skip to content

Commit f8cbcfd

Browse files
authored
Merge pull request #51 from nioinnovation/issue50-at-fix
Fix for Issue #50 Python3 Fix in _build_command()
2 parents 5d31305 + c5d9015 commit f8cbcfd

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

xbee/base.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""
1414
import struct, threading, time
1515
from xbee.frame import APIFrame
16-
from xbee.python2to3 import byteToInt, intToByte
16+
from xbee.python2to3 import byteToInt, intToByte, stringToBytes
1717

1818
class ThreadQuitException(Exception):
1919
pass
@@ -181,6 +181,9 @@ def _build_command(self, cmd, **kwargs):
181181
try:
182182
# Read this field's name from the function arguments dict
183183
data = kwargs[field['name']]
184+
if isinstance(data, str):
185+
data = stringToBytes(data)
186+
184187
except KeyError:
185188
# Data wasn't given
186189
# Only a problem if the field has a specific length
@@ -205,9 +208,8 @@ def _build_command(self, cmd, **kwargs):
205208
"The data provided for '%s' was not %d bytes long"\
206209
% (field['name'], field['len']))
207210

208-
# Add the data to the packet, if it has been specified
209-
# Otherwise, the parameter was of variable length, and not
210-
# given
211+
# Add the data to the packet, if it has been specified.
212+
# Otherwise, the parameter was of variable length, and not given.
211213
if data:
212214
packet += data
213215

@@ -393,7 +395,6 @@ def send(self, cmd, **kwargs):
393395
# Pass through the keyword arguments
394396
self._write(self._build_command(cmd, **kwargs))
395397

396-
397398
def wait_read_frame(self):
398399
"""
399400
wait_read_frame: None -> frame info dictionary

xbee/python2to3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
Helper functions for handling Python 2 and Python 3 datatype shenanigans.
77
"""
8+
import sys
89

910
def byteToInt(byte):
1011
"""
@@ -31,4 +32,4 @@ def stringToBytes(s):
3132
3233
Converts a string into an appropriate bytes object
3334
"""
34-
return s.encode('ascii')
35+
return s.encode('ascii') if sys.version_info >= (3, 0) else s

xbee/tests/test_zigbee.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
import unittest
1010
from xbee.zigbee import ZigBee
11+
from xbee.tests.Fake import Serial
1112

1213
class TestZigBee(unittest.TestCase):
1314
"""
@@ -17,6 +18,17 @@ class TestZigBee(unittest.TestCase):
1718
def setUp(self):
1819
self.zigbee = ZigBee(None)
1920

21+
def test_send(self):
22+
"""
23+
Test send() with AT command.
24+
"""
25+
device = Serial()
26+
xbee = ZigBee(device)
27+
xbee.send('at', command='MY')
28+
result = device.get_data_written()
29+
expected = b'~\x00\x04\x08\x01MYP'
30+
self.assertEqual(result, expected)
31+
2032
def test_null_terminated_field(self):
2133
"""
2234
Packets with null-terminated fields

0 commit comments

Comments
 (0)