def test_send(self): """ Test send() with AT command. """ device = Serial() xbee = ZigBee(device) xbee.send('at', command='MY') result = device.get_data_written() expected = b'~\x00\x04\x08\x01MYP' self.assertEqual(result, expected)
def reconnect_xbee(self): #search for available ports port_to_connect = '' while port_to_connect == '': #detect platform and format port names if _platform.startswith('win'): ports = ['COM%s' % (i + 1) for i in range(256)] elif _platform.startswith('linux'): # this excludes your current terminal "/dev/tty" ports = glob.glob('/dev/ttyUSB*') else: raise EnvironmentError('Unsupported platform: ' + _platform) ports_avail = [] #loop through all possible ports and try to connect for port in ports: try: s = serial.Serial(port) s.close() ports_avail.append(port) except (OSError, serial.SerialException): pass if len(ports_avail) ==1: port_to_connect = ports_avail[0] elif len(ports_avail)==0: #No Serial port found, continue looping. print( "No serial port detected. Trying again...") time.sleep(1) elif len(ports_avail)>1: #Multiple serial ports detected. Get user input to decide which one to connect to #com_input = raw_input("Multiple serial ports available. Which serial port do you want? \n"+str(self.ports_avail)+":").upper(); if self.default_serial == None: raise EnvironmentError('Incorrect command line parameters. If there are multiple serial devices, indicate what port you want to be used using --serialport') elif self.default_serial.upper() in ports_avail: port_to_connect = self.default_serial.upper() else: raise EnvironmentError('Incorrect command line parameters. Serial port is not known as a valid port. Valid ports are:'+ str(ports_avail)) #connect to xbee self.xbee = ZigBee(serial.Serial(port_to_connect, 115200)) print('xbee connected to port ' + port_to_connect) return self
def setUp(self): self.zigbee = ZigBee(None)
logging.info('Reading nodes configuration "%s"', nodefilename) nodeconfig = configparser.ConfigParser() try: nodeconfig.read(nodefilename) except: logging.critical('Could not read the node configuration file "%s".', nodefilename) raise # Open serial port with serial.Serial(args.port, args.rate) as ser: # Create an xbee ZigBee communication object dispatch = Dispatch(ser) logging.debug('Creating xbee object.') xbee = ZigBee(ser, callback=dispatch.dispatch) try: if args.command == 'listen': listen(xbee, dispatch, config, nodeconfig) elif args.command == 'configure': config_client(xbee, dispatch, config, nodeconfig) else: logging.critical('Unknown command "%s", terminating.', args.command) finally: # halt() must be called before closing the serial port in order to ensure proper thread shutdown logging.info('Halting xbee.') xbee.halt() ser.close() logging.info('Closed serial port.')