commit d5075de09fa499f6c6edc866eeaa62bf4e5cd5de Author: Jonathan Hogg Date: Mon Oct 10 13:10:16 2016 +0100 Initial test stuff diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee8335b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.*.swp +__pycache__/ diff --git a/scope.py b/scope.py new file mode 100644 index 0000000..d171c94 --- /dev/null +++ b/scope.py @@ -0,0 +1,32 @@ + +import asyncio +from streams import SerialStream + + +class Scope(object): + + def __init__(self, stream=None): + if stream is None: + stream = SerialStream() + self._stream = stream + + async def reset(self): + await self._stream.write(b'!') + await self._stream.readuntil(b'!') + + async def get_revision(self): + await self._stream.write(b'?') + assert await self._stream.readuntil(b'\r') == b'?\r' + revision = await self._stream.readuntil(b'\r') + return revision.decode('ascii').strip() + + + +async def main(): + s = Scope() + await s.reset() + print(await s.get_revision()) + +if __name__ == '__main__': + asyncio.get_event_loop().run_until_complete(main()) + diff --git a/streams.py b/streams.py new file mode 100644 index 0000000..9fb4f3a --- /dev/null +++ b/streams.py @@ -0,0 +1,68 @@ + +import asyncio +import glob +import serial + + +class SerialStream(object): + + def __init__(self, device=None, loop=None, **kwargs): + if device is None: + device = (glob.glob('/dev/tty.usb*') + glob.glob('/dev/ttyUSB*'))[0] + self._connection = serial.Serial(device, timeout=0, write_timeout=0, **kwargs) + self._loop = loop if loop is not None else asyncio.get_event_loop() + self._input_buffer = b'' + + async def write(self, data): + while data: + n = await self._write(data) + data = data[n:] + + def _write(self, data): + future = asyncio.Future() + self._loop.add_writer(self._connection, self._feed_data, data, future) + return future + + def _feed_data(self, data, future): + future.set_result(self._connection.write(data)) + self._loop.remove_writer(self._connection) + + async def read(self, n=None): + while True: + if self._input_buffer: + if n is None: + data, self._input_buffer = self._input__buffer, b'' + else: + data, self._input_buffer = self._input_buffer[:n], self._input_buffer[n:] + return data + if n is None: + self._input_buffer += await self._read() + else: + self._input_buffer += await self._read(n - len(self._input_buffer)) + + async def readexactly(self, n): + while True: + if len(self._input_buffer) >= n: + data, self._input_buffer = self._input_buffer[:n], self._input_buffer[n:] + return data + self._input_buffer += await self._read(n - len(self._input_buffer)) + + async def readuntil(self, separator): + while True: + index = self._input_buffer.find(separator) + if index >= 0: + index += len(separator) + data, self._input_buffer = self._input_buffer[:index], self._input_buffer[index:] + return data + self._input_buffer += await self._read() + + def _read(self, n=None): + future = asyncio.Future() + self._loop.add_reader(self._connection, self._handle_data, n, future) + return future + + def _handle_data(self, n, future): + future.set_result(self._connection.read(n if n is not None else self._connection.in_waiting)) + self._loop.remove_reader(self._connection) + +