Skip to content

Commit 6cda055

Browse files
Fix for Issue #50.
1 parent 1793bb8 commit 6cda055

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

xbee/base.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = str.encode(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/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)