Exemplo n.º 1
0
    def __init__(self, dev, backends=None):
        if backends is None:
            backends = ['python_usbtmc']
            if "linux" in platform:
                # this is a way better api to use than python_usbtmc, but but it only works on linux
                backends.insert(0, 'linux_kernel')
        elif not isinstance(backends, Iterable) or isinstance(
                backends, StringTypes):
            backends = [backends]
        self.__backends__ = backends

        for be_name in backends:
            try:
                be = import_backend(be_name)
                instr = be.Instrument(dev)
                idn = instr.query('*IDN?')
                self.instr = instr
                self.backend = be
                self.backend_name = be_name
                break
            except Exception as e:
                # I hate this generic error handling too, but the many backends
                # can throw so many different exception types its just easier
                # to try and if anything goes wrong then try the next backend
                print('Exceptional {}'.format(e))
                pass
        else:
            raise UsbtmcError(
                'no matching backends in {} connected using {}'.format(
                    ','.join(backends), dev))

        self.channels = [DG1022Channel(ch + 1, self) for ch in range(2)]
Exemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description=__doc__.split('\n\n\n')[0], formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('--loglevel', default='INFO', help='log level', choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'])
    parser.add_argument('--port', type=int, default=5025, help='TCP port to listen to.')
    parser.add_argument('--host', default='::', help='The host / IP address to listen at.')
    parser.add_argument('--line-ending', default='', help="The line ending to add to commands you're sending")
    parser.add_argument('--backend', default='linux_kernel', help='The backend to use')
    parser.add_argument('device', help='The device string (format depends on the backend)')
    args = parser.parse_args()

    args.line_ending = args.line_ending.replace("\\n", "\n").replace("\\r","\r")
    try:
        backend = import_backend(args.backend)
        Instrument = backend.Instrument
    except ImportError:
        parser.error('choose a valid backend')

    logging.basicConfig(format='%(message)s', level=args.loglevel.upper())

    scpi_server = UsbtmcServerExample((args.host, args.port))
    instr = Instrument(args.device)
    logger.info('Connected to USBTMC device {}.'.format(instr.idn))
    scpi_server.instr = instr

    if ':' in args.host:
        logger.info("Starting server at tcp://[{host}]:{port}".format(host=args.host, port=args.port))
    else:
        logger.info("Starting server at tcp://{host}:{port}".format(host=args.host, port=args.port))
    try:
        scpi_server.serve_forever()
    except KeyboardInterrupt:
        logger.info('Ctrl-C pressed. Shutting down...')
    scpi_server.server_close()
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(
        description=__doc__.split('\n\n\n')[0],
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        '--loglevel',
        default='INFO',
        help='log level',
        choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'])
    parser.add_argument('--port',
                        type=int,
                        default=5025,
                        help='TCP port to listen to.')
    parser.add_argument('--host',
                        default='::',
                        help='The host / IP address to listen at.')
    parser.add_argument(
        '--line-ending',
        default='',
        help="The line ending to add to commands you're sending")
    parser.add_argument('--backend',
                        default='linux_kernel',
                        help='The backend to use')
    parser.add_argument(
        'device', help='The device string (format depends on the backend)')
    args = parser.parse_args()

    args.line_ending = args.line_ending.replace("\\n",
                                                "\n").replace("\\r", "\r")
    try:
        backend = import_backend(args.backend)
        Instrument = backend.Instrument
    except ImportError:
        parser.error('choose a valid backend')

    logging.basicConfig(format='%(message)s', level=args.loglevel.upper())

    scpi_server = UsbtmcServerExample((args.host, args.port))
    instr = Instrument(args.device)
    logger.info('Connected to USBTMC device {}.'.format(instr.idn))
    scpi_server.instr = instr

    if ':' in args.host:
        logger.info("Starting server at tcp://[{host}]:{port}".format(
            host=args.host, port=args.port))
    else:
        logger.info("Starting server at tcp://{host}:{port}".format(
            host=args.host, port=args.port))
    try:
        scpi_server.serve_forever()
    except KeyboardInterrupt:
        logger.info('Ctrl-C pressed. Shutting down...')
    scpi_server.server_close()
Exemplo n.º 4
0
 def setup(self, app):
     backend = import_backend(self.backend)
     try:
         device = backend.Instrument(self.device_name)
         self.scope = RigolScope(device)
     except (UsbtmcError, RigolError) as e:
         raise PluginError("Couldn't connect to the scope: {0} {1}".format(e.__class__.__name__, e))
     for other in app.plugins:
         if not isinstance(other, RigolPlugin): continue
         if other.keyword == self.keyword:
             raise PluginError("Found another Rigol plugin with "\
                   "conflicting settings (non-unique keyword).")
Exemplo n.º 5
0
    def __init__(self):
        """ Connect and make sure the device is there"""
        backend = universal_usbtmc.import_backend("linux_kernel")
        try:
            self.be = backend.Instrument("/dev/usbtmc0")
        except universal_usbtmc.exceptions.UsbtmcNoSuchFileError:
            raise Exception("RigolDG1022 USB is not connected")
        except universal_usbtmc.exceptions.UsbtmcPermissionError:
            raise Exception("RigolDG1022: run this:  sudo chmod a+rw /dev/usbtmc0")
        except OSError:
            raise Exception("RigolDG1022: run this:  cp 97-RigolUSBTMC.rules /etc/udev/rules.d/")

        response = self.be.query("*IDN?")
        name = "RIGOL TECHNOLOGIES,DG1022"
        if not response.startswith(name):
            raise Exception("USB device is not %s"%name)
Exemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--backend', '-b', #choices=('linux_kernel', 'python_usbtmc'),
        default='linux_kernel',
        help='The backend to use')
    parser.add_argument('--line-ending', default='',
        help="The line ending to add to commands you're sending")
    parser.add_argument('device', help='')
    args = parser.parse_args()

    args.line_ending = args.line_ending.replace("\\n", "\n").replace("\\r","\r")
    try:
        backend = import_backend(args.backend)
    except UsbtmcNoSuchBackend:
        parser.error('Unknown backend {}.'.format(args.backend))
    except UsbtmcMissingDependency as md:
        parser.error('The backend could not be loaded, ' + str(md))
    be = backend.Instrument(args.device)
    print(HOWTO)
    print('> *IDN?')
    print(be.query("*IDN?"))
    try:
        while True:
            cmd = input('> ')
            cmd = cmd.strip()
            if cmd in ('quit', 'exit'):
                break
            cmd = cmd + args.line_ending
            try:
                if '?' in cmd:
                    ret = be.query_raw(cmd)
                    try:
                        print(ret.decode('utf-8').rstrip(args.line_ending))
                    except UnicodeDecodeError:
                        print('binary message:', ret)
                else:
                    be.write(cmd)
            except UsbtmcReadTimeoutError:
                print('Timeout occured')
    except KeyboardInterrupt as e:
        print('\nCtrl-C pressed.')
    except EOFError:
        pass
    print('Exiting...')
Exemplo n.º 7
0
    def __init__(self):

        backend = import_backend('linux_kernel')
        self.instrument = backend.Instrument("/dev/usbtmc0")
        print("Connected to power supply:")
        self.identify()