# HG changeset patch # User darius@inchoate.localdomain # Date 1194417608 -37800 # Node ID 4c91fdfc862e97d2f8700c65ff12170b5d88b452 # Parent d147529ad2db4f4b71ed0e1b8c4729c570001f1a - 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. diff -r d147529ad2db -r 4c91fdfc862e zb.py --- a/zb.py Mon Nov 05 23:35:52 2007 +1030 +++ b/zb.py Wed Nov 07 17:10:08 2007 +1030 @@ -1,4 +1,4 @@ -import serial, inspect +import serial, inspect, time def easyord(i): if (type(i) != type(str())): @@ -9,8 +9,7 @@ class PktBase(object): PKT_MAXLEN = 2 ** 16 - def __init__(self, data = []): - self._data = data + def __init__(self): print "Constructing " + self.__class__.__name__ def Encapsulate(self): @@ -33,6 +32,7 @@ class TXPkts(PktBase): """Base class for all packets that go to the module""" + def setframeid(self, value): if (value < 0 or value > 255): raise ValueError("FrameID must be 0-255") @@ -44,19 +44,13 @@ PKT_TYPE = 0x08 PKT_DESC = "AT Command" - def __init__(self, *args): - if (len(args) == 1): - if (type(args[0]) == type(str())): - super(AT_Cmd, self).__init__([]) - self.cmd = args[0] - else: - super(AT_Cmd, self).__init__(args[0]) - elif (len(args) == 2): - super(AT_Cmd, self).__init__([]) - self.cmd = args[0] - self.cmdarg = args[1] - else: - raise TypeError("__init__ takes 1 list of ordinals, 1 string, or 2 strings") + def __init__(self, cmd = None, cmdarg = None): + super(AT_Cmd, self).__init__() + if (cmd != None): + self.cmd = cmd + if (cmdarg != None): + self.cmdarg = cmdarg + self.frameid = 0 self.cmdarg = [] @@ -97,6 +91,10 @@ PKT_DESC = "RX Packet: 16 bit address" ADDR_SIZE = 2 + def __init__(self, data = []): + super(RX_16_Bit, self).__init__() + self._data = data + def __str__(self): return "0x%0*x (%ddBm) -> %s" % (self.ADDR_SIZE * 2, self.sender, self.rssi, str(self.payload)) @@ -244,7 +242,7 @@ Encapsulate = staticmethod(Encapsulate) - def __init__(self, s): + def __init__(self, s = None): print str(inspect.getmodule(self)) self.buffer = [] self.state = 'init' @@ -316,18 +314,30 @@ return pktcount +def polldev(): + while (1): + foo = up.getdata() + for p in up.pktq: + print p + + if (foo == 0): + time.sleep(0.1) + #for c in dir(): # if (issubclass(c, PktBase)): # print .. -s = serial.Serial(port='/dev/cuad0', baudrate=9600, bytesize=8, parity='N', \ - stopbits=1, rtscts=0) -# Non-blocking -s.timeout = 0 -#s.write('+++') -#s.readline(eol='\r') - - +try: + s = serial.Serial(port='/dev/cuaU0', baudrate=9600, bytesize=8, parity='N', \ + stopbits=1, rtscts=0) + # Non-blocking + s.timeout = 0 + #s.write('+++') + #s.readline(eol='\r') +except serial.serialutil.SerialException, e: + print "Can't open serial port - " + str(e) + s = None + # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f goodtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 15, 56]