def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, num_scans=2, reader=_utility.Reader, physical=False, plot=False, stream=_sys.stdout): """Read ``num_scans`` samples from each specified channel. """ subdevice,channels = open_channels( device=device, subdevice=subdevice, channels=channels, range=range, aref=aref) subdevice.cmd = prepare_command( subdevice=subdevice, channels=channels, period=period, num_scans=num_scans) rc = test_command(subdevice=subdevice) kwargs = {} if reader == _utility.CallbackReader: read_buffer_shape = (len(channels),) kwargs = { 'callback': DataWriter( stream=stream, subdevice=subdevice, channels=channels, physical=physical, plot=plot), 'count': num_scans, } else: read_buffer_shape = (num_scans, len(channels)) read_buffer = _numpy.zeros(read_buffer_shape, dtype=subdevice.get_dtype()) reader = reader( subdevice=subdevice, buffer=read_buffer, name='Reader', **kwargs) start = _time.time() _LOG.info('start time: {}'.format(start)) subdevice.command() reader.start() while subdevice.get_flags().running: _LOG.debug('running...') _LOG.debug('poll: {}'.format(subdevice.poll())) _LOG.debug('get_buffer offset: {}'.format( subdevice.get_buffer_offset())) _LOG.debug('get_buffer contents: {}'.format( subdevice.get_buffer_contents())) _time.sleep(0.5) _LOG.debug('stopped running. joining reader...') stop = _time.time() _LOG.info('stop time: {}'.format(stop)) reader.join() join = _time.time() _LOG.info('join time: {}'.format(join)) _LOG.debug('poll: {}'.format(subdevice.poll())) _LOG.debug('get_buffer offset: {}'.format( subdevice.get_buffer_offset())) _LOG.debug('get_buffer contents: {}'.format( subdevice.get_buffer_contents())) _LOG.info('stop delay: {}'.format(stop - start)) _LOG.info('join delay: {}'.format(join - stop)) if not isinstance(reader, _utility.CallbackReader): write_data( stream=stream, channels=channels, data=read_buffer, physical=physical)
def run(filename, subdevice, channel, range, aref, num_scans): """ >>> t1,data,t2 = run(filename='/dev/comedi0', subdevice=None, ... channel=0, range=0, aref=_constant.AREF.ground, num_scans=10) >>> t1 # doctest: +SKIP 1332242184.029691 >>> t2 # doctest: +SKIP 1332242184.3311629 >>> data # doctest: +ELLIPSIS array([...], dtype=uint32) >>> data.shape (10,) """ _LOG.info( ('measuring device={} subdevice={} channel={} range={} ' 'analog-reference={} num-scans={}').format(filename, subdevice, channel, range, aref, num_scans)) device = _Device(filename=filename) device.open() try: if subdevice is None: subdevice = device.find_subdevice_by_type( _constant.SUBDEVICE_TYPE.ai) else: subdevice = device.subdevice(subdevice) insns = [subdevice.insn(), subdevice.insn(), subdevice.insn()] insns[0].insn = insns[2].insn = _constant.INSN.gtod insns[0].data = insns[2].data = [0, 0] insns[1].insn = _constant.INSN.read insns[1].data = [0] * num_scans insns[1].chanspec = _ChanSpec(chan=channel, range=range, aref=aref) device.do_insnlist(insns) finally: device.close() t1 = insns[0].data[0] + insns[1].data[1] / 1e6 t2 = insns[2].data[0] + insns[2].data[1] / 1e6 return (t1, insns[1].data, t2)
def run(filename, subdevice, channel, range, aref, num_scans): """ >>> t1,data,t2 = run(filename='/dev/comedi0', subdevice=None, ... channel=0, range=0, aref=_constant.AREF.ground, num_scans=10) >>> t1 # doctest: +SKIP 1332242184.029691 >>> t2 # doctest: +SKIP 1332242184.3311629 >>> data # doctest: +ELLIPSIS array([...], dtype=uint32) >>> data.shape (10,) """ _LOG.info( ("measuring device={} subdevice={} channel={} range={} " "analog-reference={} num-scans={}").format( filename, subdevice, channel, range, aref, num_scans ) ) device = _Device(filename=filename) device.open() try: if subdevice is None: subdevice = device.find_subdevice_by_type(_constant.SUBDEVICE_TYPE.ai) else: subdevice = device.subdevice(subdevice) insns = [subdevice.insn(), subdevice.insn(), subdevice.insn()] insns[0].insn = insns[2].insn = _constant.INSN.gtod insns[0].data = insns[2].data = [0, 0] insns[1].insn = _constant.INSN.read insns[1].data = [0] * num_scans insns[1].chanspec = _ChanSpec(chan=channel, range=range, aref=aref) device.do_insnlist(insns) finally: device.close() t1 = insns[0].data[0] + insns[1].data[1] / 1e6 t2 = insns[2].data[0] + insns[2].data[1] / 1e6 return (t1, insns[1].data, t2)
def test_command(subdevice, num_tests=2): """Adjust a command as necessary to get valid arguments. """ _LOG.info('command before testing:\n{}'.format(subdevice.cmd)) for i in range(2): rc = subdevice.command_test() if rc is None: _LOG.info('command is valid') return _LOG.info('test {} returned {}\n{}'.format(i, rc, subdevice.cmd)) _LOG.error('error preparing command: {}'.format(rc)) _sys.exit(1)
read(device=device, **kwargs) finally: device.close() if __name__ == '__main__': import pycomedi_demo_args args = pycomedi_demo_args.parse_args( description=__doc__, argnames=['filename', 'subdevice', 'channels', 'aref', 'range', 'num-scans', 'mmap', 'callback', 'frequency', 'physical', 'plot', 'verbose']) _LOG.info(('measuring device={0.filename} subdevice={0.subdevice} ' 'channels={0.channels} range={0.range} ' 'analog-reference={0.aref}' ).format(args)) if args.callback: reader = _utility.CallbackReader elif args.mmap: reader = _utility.MMapReader else: reader = _utility.Reader if args.plot: if _pyplot is None: raise _matplotlib_import_error _pyplot.ion() run(filename=args.filename, subdevice=args.subdevice, channels=args.channels, aref=args.aref, range=args.range,
def display(t1, data, t2): _LOG.info("initial time: {}".format(t1)) _LOG.info("final time: {}".format(t2)) _LOG.info("difference: {}".format(t2 - t1)) for x in insns[1].data: print(x)
def display(t1, data, t2): _LOG.info('initial time: {}'.format(t1)) _LOG.info('final time: {}'.format(t2)) _LOG.info('difference: {}'.format(t2 - t1)) for x in insns[1].data: print(x)