trunet

XBee Series 2 Twisted Protocol

Aug 12th, 2011
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. from xbee.zigbee import ZigBee
  2. from xbee.base import XBeeBase
  3. from xbee.frame import APIFrame
  4.  
  5. from twisted.protocols import basic
  6.  
  7. class ZigBeeProtocol(ZigBee, basic.LineReceiver):
  8.     def __init__(self, shorthand=True, escaped=True):
  9.         super(XBeeBase, self).__init__()
  10.         self.shorthand = shorthand
  11.         self._thread_continue = False
  12.         self._escaped = escaped
  13.        
  14.         self.frame = APIFrame(escaped=self._escaped)
  15.        
  16.         self.setRawMode()
  17.  
  18.     def rawDataReceived(self, data):
  19.         if data[0] == APIFrame.START_BYTE:
  20.             self.frame = APIFrame(escaped=self._escaped)
  21.         for i in range(0, len(data)):
  22.             self.frame.fill(data[i])
  23.         if (not (self.frame.remaining_bytes() > 0)):
  24.             try:
  25.                 # Try to parse and return result
  26.                 self.frame.parse()
  27.                 return getattr(self, "handle_packet", None)(self._split_response(self.frame.data))
  28.             except ValueError:
  29.                 # Bad frame, so restart
  30.                 self.frame = APIFrame(escaped=self._escaped)
Advertisement
Add Comment
Please, Sign In to add comment