From f41e11c336c1c87c87cfe3217a978d314ddd2895 Mon Sep 17 00:00:00 2001 From: Jonathan Hogg Date: Sun, 23 Oct 2016 17:50:56 +0000 Subject: [PATCH] Work in progress --- scope.py | 27 ++++++++++++++++++++------- streams.py | 9 ++------- vm.py | 28 +++++++++++++++++++++------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/scope.py b/scope.py index 0510d3e..a13e564 100755 --- a/scope.py +++ b/scope.py @@ -23,19 +23,22 @@ class Scope(vm.VirtualMachine): scope = cls(streams.SerialStream()) elif os.path.exists(device): scope = cls(streams.SerialStream(device=device)) + elif ':' in device: + host, port = device.split(':', 1) + Log.info("Connecting to remote scope at {}:{}".format(host, port)) + reader, writer = await asyncio.open_connection(host, int(port)) + scope = cls(reader, writer) else: raise ValueError("Don't know what to do with '{}'".format(device)) await scope.setup() return scope - def __init__(self, stream): - super(Scope, self).__init__(stream) - @staticmethod def _analog_map_func(ks, low, high): return ks[0] + ks[1]*low + ks[2]*high async def setup(self): + Log.info("Resetting scope") await self.reset() await self.issue_get_revision() revision = ((await self.read_replies(2))[1]).decode('ascii') @@ -58,6 +61,14 @@ class Scope(vm.VirtualMachine): self._generator_running = False Log.info("Initialised scope, revision: {}".format(revision)) + def close(self): + if self._writer is not None: + self._writer.close() + self._writer = None + self._reader = None + + __del__ = close + async def load_params(self): params = [] for i in range(struct.calcsize('{}h'.format(nsamples), data)] @@ -323,7 +334,6 @@ class Scope(vm.VirtualMachine): import numpy as np -import pandas as pd async def main(): global s, x, y, data @@ -338,10 +348,13 @@ async def main(): # await s.save_params() def capture(*args, **kwargs): - return pd.DataFrame(asyncio.get_event_loop().run_until_complete(s.capture(*args, **kwargs))) + return asyncio.get_event_loop().run_until_complete(s.capture(*args, **kwargs)) + +def calibrate(*args, **kwargs): + return asyncio.get_event_loop().run_until_complete(s.calibrate(*args, **kwargs)) if __name__ == '__main__': import sys - #logging.basicConfig(level=logging.DEBUG, stream=sys.stderr) + logging.basicConfig(level=logging.DEBUG, stream=sys.stderr) asyncio.get_event_loop().run_until_complete(main()) diff --git a/streams.py b/streams.py index 2d97400..e6833a0 100644 --- a/streams.py +++ b/streams.py @@ -3,7 +3,6 @@ import asyncio import logging import os import serial -import serial.tools.list_ports Log = logging.getLogger('streams') @@ -11,12 +10,8 @@ Log = logging.getLogger('streams') class SerialStream: - @staticmethod - def available_ports(): - return [port.device for port in serial.tools.list_ports.comports() if port.usb_description() == 'FT245R USB FIFO'] - - def __init__(self, port=0, device=None, loop=None, **kwargs): - self._device = self.available_ports()[port] if device is None else device + def __init__(self, device, loop=None, **kwargs): + self._device = device self._connection = serial.Serial(self._device, timeout=0, write_timeout=0, **kwargs) self._loop = loop if loop is not None else asyncio.get_event_loop() self._input_buffer = bytes() diff --git a/vm.py b/vm.py index 288fb13..6f6f33f 100644 --- a/vm.py +++ b/vm.py @@ -194,8 +194,9 @@ class VirtualMachine: await self._vm.issue(self._data) return False - def __init__(self, stream): - self._stream = stream + def __init__(self, reader, writer): + self._reader = reader + self._writer = writer self._transactions = [] def transaction(self): @@ -205,26 +206,39 @@ class VirtualMachine: if isinstance(cmd, str): cmd = cmd.encode('ascii') if not self._transactions: - await self._stream.write(cmd) - echo = await self._stream.readexactly(len(cmd)) + Log.debug("Issue: {}".format(repr(cmd))) + self._writer.write(cmd) + await self._writer.drain() + echo = await self._reader.readexactly(len(cmd)) if echo != cmd: raise RuntimeError("Mismatched response") else: self._transactions[-1].append(cmd) + async def readuntil(self, separator): + data = b'' + while not data.endswith(separator): + data += await self._reader.read(1) + return data + async def read_replies(self, n): if self._transactions: raise TypeError("Command transaction in progress") replies = [] for i in range(n): - replies.append((await self._stream.readuntil(b'\r'))[:-1]) + reply = (await self.readuntil(b'\r'))[:-1] + Log.debug("Read reply: {}".format(repr(reply))) + replies.append(reply) return replies async def reset(self): if self._transactions: raise TypeError("Command transaction in progress") - await self._stream.write(b'!') - await self._stream.readuntil(b'!') + Log.debug("Issue reset") + self._writer.write(b'!') + await self._writer.drain() + await self.readuntil(b'!') + Log.debug("Reset complete") async def set_registers(self, **kwargs): cmd = ''