-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
186 lines (139 loc) · 4.43 KB
/
control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
Links:
https://groups.google.com/forum/#!topic/python-xbee/hC9EiZ9L1qU
https://stackoverflow.com/questions/14136053/what-are-the-frame-id-and-frame-fields-in-ser-send-from-python-xbee
http://docs.digi.com/pages/viewpage.action?pageId=2626044
https://cdn.sparkfun.com/learn/materials/29/22AT%20Commands.pdf
"""
# Standard Library
import argparse
import cmd
from datetime import datetime
import signal
import struct
import time
# Project
import utils
# Constants
BAUDS = 9600
# Xbee addresses
BROADCAST = b'\x00\x00\x00\x00\x00\x00\xff\xff'
CS_PI = b'\x00\x13\xa2\x00\x41\x25\x39\xd3'
CS_V12 = b'\x00\x13\xa2\x00A%9m'
CS_V15 = b'\x00\x13\xa2\x00A\x05\xd8\xcf'
CS = [CS_V12, CS_V15]
FINSE = [
5526146532103365,
5526146532103350,
5526146534160844,
]
FINSE = [struct.pack(">Q", x) for x in FINSE]
ADDRESSES = {
'broadcast': BROADCAST,
'v12': CS_V12,
'v15': CS_V15,
}
def timeout(signum, frame):
print('Timeout')
raise TimeoutError
def test_simple():
xbee.at(command="DB", frame_id="\x01") # Pi
xbee.remote_at(command="DB", frame_id="\x01", dest_addr_long=CS_V12)
xbee.remote_at(command="DB", frame_id="\x01", dest_addr_long=CS_V15)
# Broadcast
xbee.remote_at(command="DB", frame_id="\x01")
xbee.remote_at(command="DB", frame_id="\x01", dest_addr_long=BROADCAST)
def remote_at(xbee, address, command, frame_ids={}):
# frame_id is required
frame_id = frame_ids.setdefault(address, 1)
frame_ids[address] = 1 if (frame_id == 255) else (frame_id + 1) # Next
frame_id = bytearray([frame_id])
# Send AT command
xbee.remote_at(dest_addr_long=address, command=command, frame_id=frame_id)
return frame_id
def tx(device, remote, data, frame_ids={}):
address = utils.get_address(remote)
# frame_id is required
frame_id = frame_ids.setdefault(address, 1)
frame_ids[address] = 1 if (frame_id == 255) else (frame_id + 1) # Next
frame_id = bytearray([frame_id])
# Send AT command
utils.send_data_async(device, remote, data)
#device.tx(dest_addr=address, data=data, frame_id=frame_id)
return frame_id
def remote_at_wait(xbee, address, command='DB', timeout=5):
frame_id = remote_at(xbee, address, command)
signal.alarm(timeout)
try:
while True:
frame = xbee.wait_read_frame()
if frame.get('frame_id') == frame_id:
if address == BROADCAST:
print(frame)
return frame
if frame['source_addr'] == address:
print(frame)
return frame
except TimeoutError:
pass
return None
def retry(address, command='DB'):
seconds = (60 - datetime.now().second) + 3
time.sleep(seconds)
for i in range(5):
print('Try %s', datetime.now())
frame = remote_at_wait(address, command)
if frame['status'] == b'\x00':
break
return frame
def command(f):
def wrapper(self, arg):
args = arg.split()
return f(self, *args)
wrapper.__doc__ = f.__doc__
return wrapper
class Control(cmd.Cmd):
prompt = '() '
address = None
def preloop(self):
self.xbee = utils.get_device(BAUDS)
def set_address(self, address):
if address not in ADDRESSES:
address = ''
self.address = address
self.prompt = '(%s) ' % self.address
@command
def do_ls(self):
"""List motes."""
for name in sorted(ADDRESSES):
print(name)
@command
def do_set(self, address):
"""Change XBee address"""
self.set_address(address)
@command
def do_at(self, command):
"""Send AT command"""
remote_at_wait(self.xbee, ADDRESSES[self.address], command=command.upper())
@command
def do_tx(self, data):
"""Send AT command"""
self.xbee.tx_long_addr(det_addr=ADDRESSES[self.address], data=data)
@command
def do_q(self):
"""Quit"""
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('address', default='')
args = parser.parse_args()
signal.signal(signal.SIGALRM, timeout)
control = Control()
control.set_address(args.address)
control.cmdloop()
# xbee = utils.get_device(PORT)
# data = 'time %d' % int(time.time())
# data = b'hello'
# xbee.tx_long_addr(dest_addr=CS_V15, data=data)
#remote_at(xbee, CS_V15, command='DB')
#print(xbee.wait_read_frame())