forked from biomimetics/imageproc_py
-
Notifications
You must be signed in to change notification settings - Fork 1
/
basestation.py
67 lines (47 loc) · 1.69 KB
/
basestation.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
#!/usr/bin/env python
import time, os, sys
from serial import *
from xbee import XBee
from payload import Payload
class BaseStation(object):
def __init__(self, port, baud, dest_addr = None, call_back = None):
self.ser = Serial(port, baud, timeout = 1)
self.ser.writeTimeout = 5
if call_back == None:
self.xb = XBee(self.ser)
else:
self.xb = XBee(self.ser, callback = call_back)
self.dest_addr = dest_addr
def close(self):
try:
self.ser.close()
except SerialException:
print "SerialException"
def send(self, status, type, data ):
pld = Payload( ''.join(data), status, type )
self.xb.tx(dest_addr = self.dest_addr, data = str(pld))
def write(self, data):
status = 0x00
type = 0x00
data_length = len(data)
start = 0
while(data_length > 0):
if data_length > 80:
self.send( status, type, data[start:start+80] )
data_length -= 80
start += 80
else:
self.send( status, type, data[start:len(data)] )
data_length = 0
time.sleep(0.05)
def read(self):
packet = self.xb.wait_read_frame()
pld = Payload(packet.get('rf_data'))
#rssi = ord(packet.get('rssi'))
#(src_addr, ) = unpack('H', packet.get('source_addr'))
#id = packet.get('id')
#options = ord(packet.get('options'))
status = pld.status
type = pld.type
data = pld.data
return data