mirror of
https://github.com/jonathanhogg/scopething
synced 2025-07-14 11:12:09 +01:00
Initial test stuff
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.*.swp
|
||||||
|
__pycache__/
|
32
scope.py
Normal file
32
scope.py
Normal file
@ -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())
|
||||||
|
|
68
streams.py
Normal file
68
streams.py
Normal file
@ -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)
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user