Skip to content

Commit

Permalink
Add serial connection
Browse files Browse the repository at this point in the history
Expand dependancy list

rename aioserial_instance to stream
  • Loading branch information
marcelldls committed May 17, 2024
1 parent 2c46785 commit 7eb9da1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ classifiers = [
]
description = "Control system agnostic framework for building Device support in Python that will work for both EPICS and Tango"
dependencies = [
"aioserial",
"numpy",
"pydantic",
"pvi~=0.8.1",
Expand Down
52 changes: 52 additions & 0 deletions src/fastcs/connections/serial_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import asyncio
from dataclasses import dataclass

import aioserial


class NotOpenedError(Exception):
pass


@dataclass
class SerialConnectionSettings:
port: str
baud: int = 115200


class SerialConnection:
def __init__(self):
self.stream = None
self._lock = asyncio.Lock()

Check warning on line 20 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L19-L20

Added lines #L19 - L20 were not covered by tests

async def connect(self, settings: SerialConnectionSettings) -> None:
self.stream = aioserial.AioSerial(port=settings.port, baudrate=settings.baud)

Check warning on line 23 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L23

Added line #L23 was not covered by tests

def ensure_open(self):
if self.stream is None:
raise NotOpenedError(

Check warning on line 27 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L26-L27

Added lines #L26 - L27 were not covered by tests
"Need to call connect() before using SerialConnection."
)

async def send_command(self, message: bytes) -> None:
async with self._lock:
self.ensure_open()
await self._send_message(message)

Check warning on line 34 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L32-L34

Added lines #L32 - L34 were not covered by tests

async def send_query(self, message: bytes, response_size: int) -> bytes:
async with self._lock:
self.ensure_open()
await self._send_message(message)
return await self._receive_response(response_size)

Check warning on line 40 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L37-L40

Added lines #L37 - L40 were not covered by tests

async def close(self) -> None:
async with self._lock:
self.ensure_open()
self.stream.close()
self.stream = None

Check warning on line 46 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L43-L46

Added lines #L43 - L46 were not covered by tests

async def _send_message(self, message):
await self.stream.write_async(message)

Check warning on line 49 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L49

Added line #L49 was not covered by tests

async def _receive_response(self, size):
return await self.stream.read_async(size)

Check warning on line 52 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L52

Added line #L52 was not covered by tests

0 comments on commit 7eb9da1

Please sign in to comment.