async def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( '-d', '--device', type=str, default='/dev/ttyUSB0', help='serial device') parser.add_argument( '-b', '--baud', type=int, default=3000000, help='baud rate') args = parser.parse_args() serial = aioserial.AioSerial(port=args.device, baudrate=args.baud) set_serial_low_latency(serial.fd) manager = mp.MultiplexManager(serial) clients = { key: mp.MultiplexClient( manager, timeout=0.02, destination_id=key, channel=1) for key in [1, 2, 3] } for key, client in clients.items(): client.write(b'tel stop\n') await manager.drain() await asyncio.sleep(2.0)
async def async_test_multiplex_client_write(self): pipe = sh.PipeStream() mm = mp.MultiplexManager(pipe.side_a) mc1 = mp.MultiplexClient(mm, 5) async def server(): data = await pipe.side_b.read(2 + # magic 1 + # source id 1 + # dest id 1 + # payload size 1 + # subframe type 1 + # channel 1 + # number of bytes 4 + # data 2) # crc return data async def client(): mc1.write(b'test') await mc1.drain() results = await asyncio.gather(server(), client()) self.assertEqual( results[0], bytes([ 0x54, 0xab, 0x00, 0x05, 0x07, 0x40, 0x01, 0x04, ord('t'), ord('e'), ord('s'), ord('t'), 0x01, 0xa0 ]))
def __init__(self, aio_serial, ids): self.aio_serial = aio_serial self.manager = mp.MultiplexManager(self.aio_serial) self.channels = { key: mp.MultiplexClient(self.manager, timeout=0.02, destination_id=key, channel=1) for key in ids }
async def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('data', type=str, help='file containing configuration commands') parser.add_argument('-d', '--device', type=str, default='/dev/ttyUSB0', help='serial device') parser.add_argument('-b', '--baud', type=int, default=3000000, help='baud rate') parser.add_argument('-t', '--target', type=int, default=1, help='destination multiplex address') parser.add_argument('-c', '--channel', type=int, default=1, help='destination multiples channel') args = parser.parse_args() serial = aioserial.AioSerial(port=args.device, baudrate=args.baud) manager = mp.MultiplexManager(serial) mc = mp.MultiplexClient( manager, timeout=0.3, # "conf write" can take a long time destination_id=args.target, channel=args.channel) with open(args.data, 'rb') as data: for line in data.readlines(): if line.strip() == '': continue print(line.strip().decode('latin1')) mc.write(line) await mc.drain() result = (await readline(mc)).strip() print('>', result.decode('latin1')) if result != b'OK': raise RuntimeError('Unknown response: ' + result)
async def async_test_multiplex_client_read(self): pipe = sh.PipeStream() mm = mp.MultiplexManager(pipe.side_a) mc1 = mp.MultiplexClient(mm, 5) async def server(): # First read one polling packet. poll = await pipe.side_b.read(2 + # magic 1 + # source id 1 + # dest id 1 + # payload size 1 + # subframe type 1 + # channel 1 + # number of bytes (0) 2) # crc # Then write a response. pipe.side_b.write( bytes([ 0x54, 0xab, 0x05, 0x00, 0x07, 0x41, 0x01, 0x04, ord('t'), ord('e'), ord('s'), ord('t'), 0x00, 0x00, ])) await pipe.side_b.drain() async def client(): return await mc1.read(4) results = await asyncio.gather(server(), client()) self.assertEqual(results[1], b'test')
async def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( '-t', '--target', type=str, action='append', default=None, help='destination address(es) (default: autodiscover)') parser.add_argument( '-d', '--device', type=str, default='/dev/ttyUSB0', help='serial device') parser.add_argument( '-b', '--baud', type=int, default=3000000, help='baud rate') parser.add_argument( '--skip-stop', action='store_true', help='omit initial "tel stop"') parser.add_argument('-v', '--verbose', action='store_true') parser.add_argument('--show-plots', action='store_true', help='only valid with --calibrate') parser.add_argument('--calibration-power', default=0.35, help='voltage to use during calibration') # The different commands that we can do. No more than one can be # specified at a time. group = parser.add_mutually_exclusive_group() group.add_argument( '--stop', action='store_true', help='command the servos to stop') group.add_argument( '--dump-config', action='store_true', help='emit all configuration to the console') group.add_argument( '--set-id', type=int, default=None, help='configure the device to use the given multiplex ID') group.add_argument( '--calibrate', action='store_true', help='calibrate the motor, requires full freedom of motion') group.add_argument( '--read-int8', action='append', type=str, help='read the given registers as int8s') group.add_argument( '--read-int16', action='append', type=str, help='read registers as int16s') group.add_argument( '--read-int32', action='append', type=str, help='read registers as int32s') group.add_argument( '--read-float', action='append', type=str, help='read registers as floats') group.add_argument( '--write-int8', action='append', type=str, help='write registers as int8') group.add_argument( '--write-int16', action='append', type=str, help='write registers as int16') group.add_argument( '--write-int32', action='append', type=str, help='write_registers as int32') group.add_argument( '--write-float', action='append', type=str, help='write_registers as float') group.add_argument( '--flash', type=str, help='write the given elf file to flash') args = parser.parse_args() args.target = expand_targets(args.target) if args.verbose: global G_VERBOSE G_VERBOSE = True serial = aioserial.AioSerial(port=args.device, baudrate=args.baud) set_serial_low_latency(serial.fd) try: _ = await asyncio.wait_for(serial.read(8192), 0.1) except asyncio.TimeoutError: pass manager = mp.MultiplexManager(serial) # If we don't know which target to use, then we search to see who # is on the bus. if args.target is None: args.target = await find_online_targets(manager) if len(args.target) == 0: print('No devices found.') sys.exit(1) print('Auto-detected device ids: ', args.target) clients = { key: mp.MultiplexClient( manager, timeout=0.02, destination_id=key, channel=1) for key in args.target } for key, client in clients.items(): if len(clients) > 0: print('*** ID: {}'.format(key)) # Read anything that might have been sitting on the channel # first. if not args.skip_stop: try: await write_command(client, b'tel stop') _ = await(asyncio.wait_for(readbytes(client), 0.2)) except asyncio.TimeoutError: pass if args.stop: await command(client, b'd stop') if args.dump_config: await write_command(client, b'conf enumerate') while True: line = (await readline(client)).strip() if line.startswith(b'OK'): break print(line.decode('utf8')) if args.calibrate: await do_calibrate(client, args) if args.read_int8: print(await _register_query(client, args.read_int8, 0)) if args.read_int16: print(await _register_query(client, args.read_int16, 1)) if args.read_int32: print(await _register_query(client, args.read_int32, 2)) if args.read_float: print(await _register_query(client, args.read_float, 3)) if args.write_int8: await _write_registers(client, args.write_int8, 0) if args.write_int16: await _write_registers(client, args.write_int16, 1) if args.write_int32: await _write_registers(client, args.write_int32, 2) if args.write_float: await _write_registers(client, args.write_float, 3) if args.flash: await _write_flash(client, args.flash)