def do_watch(self, p): """Wait for the arrival of a XBee data packet, print it when it arrives, wait for the next""" if not self.serial.isOpen(): print "You must set a serial port first." else: print "Entering watch mode..." switchColor(WATCH_OUTPUT_TEXT) while 1: packet = xbee.find_packet(self.serial) if packet: xb = xbee(packet) print xb
def do_watch(self, p): """Assuming you have set the XBee to data mode, wait for the arrival of a XBee data packet, print it when it arrives, and wait for the next packet.""" if not self.serial.isOpen(): print "You must set a serial port first." else: print "Entering \"watch\" mode (Ctrl-C to abort)..." ser = serial.Serial(self.serial.port, self.serial.baudrate) # Open XBee serial port xbee = XBee(ser) # Create XBee object to manage packets dispatch = Dispatch(xbee=xbee) # Start the dispatcher that will call packet handlers # switchColor(WATCH_OUTPUT_TEXT) try: dispatch.run() # run() will loop infinitely while waiting for and processing packets which arrive. except KeyboardInterrupt: printc("\n*** Ctrl-C keyboard interrupt ***", ERROR_TEXT) ser.close() switchColor(WATCH_OUTPUT_TEXT)
def do_watch(self, p): """Assuming you have set the XBee to data mode, wait for the arrival of a XBee data packet, print it when it arrives, and wait for the next packet.""" if not self.serial.isOpen(): print "You must set a serial port first." else: print "Entering \"watch\" mode (Ctrl-C to abort)..." ser = serial.Serial(self.serial.port, self.serial.baudrate) # Open XBee serial port xbee = XBee(ser) # Create XBee object to manage packets dispatch = Dispatch( xbee=xbee ) # Start the dispatcher that will call packet handlers # switchColor(WATCH_OUTPUT_TEXT) try: dispatch.run( ) # run() will loop infinitely while waiting for and processing packets which arrive. except KeyboardInterrupt: printc("\n*** Ctrl-C keyboard interrupt ***", ERROR_TEXT) ser.close() switchColor(WATCH_OUTPUT_TEXT)
def default(self, p): """Command is assumed to be an AT Commands for the XBee radio""" if not self.serial.isOpen(): print "You must set a serial port first." else: if p == '+++': self.serial.write('+++') time.sleep(2) else: self.serial.write('%s\r' % p) time.sleep(0.5) output = '' while self.serial.inWaiting(): output += self.serial.read() if output == '': print 'XBee timed out, so reissue "+++". (Or maybe XBee doesn\'t understand "%s".)' % p else: switchColor(XBEE_OUTPUT_TEXT) print output.replace('\r', '\n').rstrip()
def do_term(self, p): """Assuming you have setup XBee modems to communicate in a point-to-point mode, the interpretor will establish a serial terminal interface to the XBee. Data arriving at XBee will be printed and characters entered at the keyboard will be sent via the XBee.""" if not self.serial.isOpen(): print "You must set a serial port first." else: print "Entering \"term\" mode (Ctrl-C to abort)..." try: #self.serial.timeout = 0 while True: # read a line from XBee and convert it from b'xxx\r\n' to xxx switchColor(TERM_OUTPUT_TEXT) line = self.serial.readline().decode('utf-8')[:-2] if line: print line # read data from the keyboard and send via the XBee modem switchColor(TERM_INPUT_TEXT) line = sys.stdin.readline() self.serial.writelines(line) self.serial.flush() """ output = '' while self.serial.inWaiting(): output += self.serial.read() if output == '': print 'XBee timed out, so reissue "+++". (Or maybe XBee doesn\'t understand "%s".)' % p else: print output.replace('\r', '\n').rstrip() """ except KeyboardInterrupt: printc("\n*** Ctrl-C keyboard interrupt ***", ERROR_TEXT) finally: switchColor(WATCH_OUTPUT_TEXT)
def do_shell(self, p): """Pause the interpreter and invoke command in Linux shell""" print "running shell command: ", p switchColor(SHELL_OUTPUT_TEXT) print os.popen(p).read()
def postcmd(self, stop, p): """executed just after a command dispatch is finished""" switchColor(CMD_INPUT_TEXT) return cmd.Cmd.postcmd(self, stop, p)
def precmd(self, p): """executed just before the command line line is interpreted""" switchColor(CMD_OUTPUT_TEXT) return cmd.Cmd.precmd(self, p)
if __name__ == '__main__': serial = serial.Serial() serial.port = '/dev/serial/by-id/usb-Digi_XStick-if00-port0' serial.baudrate = 9600 serial.timeout = 1 serial.writeTimeout = 1 serial.open() # make stdin a non-blocking file fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NONBLOCK) # post startup message to other XBee's and at stdout serial.writelines("RPi #1 is up and running.\r\n") print "RPi #1 is up and running." switchColor(CMD_OUTPUT_TEXT) print "Entering loop to read and print messages (Ctrl-C to abort)..." try: while True: # read a line from XBee and convert it from b'xxx\r\n' to xxx and print at stdout switchColor(TERM_OUTPUT_TEXT) line = serial.readline().decode('utf-8') if line: print line # read data from the keyboard (i.e. stdin) and send via the XBee modem switchColor(TERM_INPUT_TEXT) try: line = sys.stdin.readline() serial.writelines(line)
if __name__ == "__main__": serial = serial.Serial() serial.port = "/dev/serial/by-id/usb-Digi_XStick-if00-port0" serial.baudrate = 9600 serial.timeout = 1 serial.writeTimeout = 1 serial.open() # make stdin a non-blocking file fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NONBLOCK) # post startup message to other XBee's and at stdout serial.writelines("RPi #1 is up and running.\r\n") print "RPi #1 is up and running." switchColor(CMD_OUTPUT_TEXT) print "Entering loop to read and print messages (Ctrl-C to abort)..." try: while True: # read a line from XBee and convert it from b'xxx\r\n' to xxx and print at stdout switchColor(TERM_OUTPUT_TEXT) line = serial.readline().decode("utf-8") if line: print line # read data from the keyboard (i.e. stdin) and send via the XBee modem switchColor(TERM_INPUT_TEXT) try: line = sys.stdin.readline() serial.writelines(line)