Mercurial > ~darius > hgwebdir.cgi > ZigBee
comparison zb.py @ 10:4c91fdfc862e
- Further changes to make things cleaner (ie RX packets are "read only" etc).
- Add helper routine to poll the serial port and print any packets seen.
- Make opening the serial port optional.
author | darius@inchoate.localdomain |
---|---|
date | Wed, 07 Nov 2007 17:10:08 +1030 |
parents | d147529ad2db |
children | 75f785a09e2e |
comparison
equal
deleted
inserted
replaced
9:d147529ad2db | 10:4c91fdfc862e |
---|---|
1 import serial, inspect | 1 import serial, inspect, time |
2 | 2 |
3 def easyord(i): | 3 def easyord(i): |
4 if (type(i) != type(str())): | 4 if (type(i) != type(str())): |
5 return i | 5 return i |
6 else: | 6 else: |
7 return ord(i) | 7 return ord(i) |
8 | 8 |
9 class PktBase(object): | 9 class PktBase(object): |
10 PKT_MAXLEN = 2 ** 16 | 10 PKT_MAXLEN = 2 ** 16 |
11 | 11 |
12 def __init__(self, data = []): | 12 def __init__(self): |
13 self._data = data | |
14 print "Constructing " + self.__class__.__name__ | 13 print "Constructing " + self.__class__.__name__ |
15 | 14 |
16 def Encapsulate(self): | 15 def Encapsulate(self): |
17 return Packets.Encapsulate([self.PKT_TYPE] + self.data) | 16 return Packets.Encapsulate([self.PKT_TYPE] + self.data) |
18 | 17 |
31 (i, ord(list[i]), min, max)) | 30 (i, ord(list[i]), min, max)) |
32 _checklist = staticmethod(_checklist) | 31 _checklist = staticmethod(_checklist) |
33 | 32 |
34 class TXPkts(PktBase): | 33 class TXPkts(PktBase): |
35 """Base class for all packets that go to the module""" | 34 """Base class for all packets that go to the module""" |
35 | |
36 def setframeid(self, value): | 36 def setframeid(self, value): |
37 if (value < 0 or value > 255): | 37 if (value < 0 or value > 255): |
38 raise ValueError("FrameID must be 0-255") | 38 raise ValueError("FrameID must be 0-255") |
39 self._frameid = value | 39 self._frameid = value |
40 frameid = property(lambda s: s._frameid, setframeid) | 40 frameid = property(lambda s: s._frameid, setframeid) |
42 | 42 |
43 class AT_Cmd(TXPkts): | 43 class AT_Cmd(TXPkts): |
44 PKT_TYPE = 0x08 | 44 PKT_TYPE = 0x08 |
45 PKT_DESC = "AT Command" | 45 PKT_DESC = "AT Command" |
46 | 46 |
47 def __init__(self, *args): | 47 def __init__(self, cmd = None, cmdarg = None): |
48 if (len(args) == 1): | 48 super(AT_Cmd, self).__init__() |
49 if (type(args[0]) == type(str())): | 49 if (cmd != None): |
50 super(AT_Cmd, self).__init__([]) | 50 self.cmd = cmd |
51 self.cmd = args[0] | 51 if (cmdarg != None): |
52 else: | 52 self.cmdarg = cmdarg |
53 super(AT_Cmd, self).__init__(args[0]) | 53 |
54 elif (len(args) == 2): | |
55 super(AT_Cmd, self).__init__([]) | |
56 self.cmd = args[0] | |
57 self.cmdarg = args[1] | |
58 else: | |
59 raise TypeError("__init__ takes 1 list of ordinals, 1 string, or 2 strings") | |
60 self.frameid = 0 | 54 self.frameid = 0 |
61 self.cmdarg = [] | 55 self.cmdarg = [] |
62 | 56 |
63 def setcmd(self, value): | 57 def setcmd(self, value): |
64 if (len(value) != 2): | 58 if (len(value) != 2): |
95 class RX_16_Bit(PktBase): | 89 class RX_16_Bit(PktBase): |
96 PKT_TYPE = 0x81 | 90 PKT_TYPE = 0x81 |
97 PKT_DESC = "RX Packet: 16 bit address" | 91 PKT_DESC = "RX Packet: 16 bit address" |
98 ADDR_SIZE = 2 | 92 ADDR_SIZE = 2 |
99 | 93 |
94 def __init__(self, data = []): | |
95 super(RX_16_Bit, self).__init__() | |
96 self._data = data | |
97 | |
100 def __str__(self): | 98 def __str__(self): |
101 return "0x%0*x (%ddBm) -> %s" % (self.ADDR_SIZE * 2, self.sender, | 99 return "0x%0*x (%ddBm) -> %s" % (self.ADDR_SIZE * 2, self.sender, |
102 self.rssi, str(self.payload)) | 100 self.rssi, str(self.payload)) |
103 | 101 |
104 def getsender(self): | 102 def getsender(self): |
242 pkt = [0x7e] + [len(data) >> 8] + [len(data) & 0xff] + data + [0xff - pktsum] | 240 pkt = [0x7e] + [len(data) >> 8] + [len(data) & 0xff] + data + [0xff - pktsum] |
243 return(map(chr, pkt)) | 241 return(map(chr, pkt)) |
244 | 242 |
245 Encapsulate = staticmethod(Encapsulate) | 243 Encapsulate = staticmethod(Encapsulate) |
246 | 244 |
247 def __init__(self, s): | 245 def __init__(self, s = None): |
248 print str(inspect.getmodule(self)) | 246 print str(inspect.getmodule(self)) |
249 self.buffer = [] | 247 self.buffer = [] |
250 self.state = 'init' | 248 self.state = 'init' |
251 self.packets = [] | 249 self.packets = [] |
252 | 250 |
314 print "Invalid state %s! Resetting" % (self.state) | 312 print "Invalid state %s! Resetting" % (self.state) |
315 self.state = 'init' | 313 self.state = 'init' |
316 | 314 |
317 return pktcount | 315 return pktcount |
318 | 316 |
317 def polldev(): | |
318 while (1): | |
319 foo = up.getdata() | |
320 for p in up.pktq: | |
321 print p | |
322 | |
323 if (foo == 0): | |
324 time.sleep(0.1) | |
325 | |
319 #for c in dir(): | 326 #for c in dir(): |
320 # if (issubclass(c, PktBase)): | 327 # if (issubclass(c, PktBase)): |
321 # print .. | 328 # print .. |
322 | 329 |
323 s = serial.Serial(port='/dev/cuad0', baudrate=9600, bytesize=8, parity='N', \ | 330 try: |
324 stopbits=1, rtscts=0) | 331 s = serial.Serial(port='/dev/cuaU0', baudrate=9600, bytesize=8, parity='N', \ |
325 # Non-blocking | 332 stopbits=1, rtscts=0) |
326 s.timeout = 0 | 333 # Non-blocking |
327 #s.write('+++') | 334 s.timeout = 0 |
328 #s.readline(eol='\r') | 335 #s.write('+++') |
329 | 336 #s.readline(eol='\r') |
330 | 337 except serial.serialutil.SerialException, e: |
338 print "Can't open serial port - " + str(e) | |
339 s = None | |
340 | |
331 # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f | 341 # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f |
332 goodtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 15, 56] | 342 goodtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 15, 56] |
333 | 343 |
334 # Checksum error | 344 # Checksum error |
335 badtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 14, 56] | 345 badtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 14, 56] |