Skip to content

Commit

Permalink
Add serial connection
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelldls committed May 13, 2024
1 parent f88f4d3 commit bdf405d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
]
description = "Control system agnostic framework for building Device support in Python that will work for both EPICS and Tango"
dependencies = [
"numpy",
"pydantic",
"pvi~=0.8.1",
"softioc",
]
dependencies = ["aioserial", "numpy", "pydantic", "pvi~=0.8.1", "softioc"]
dynamic = ["version"]
license.file = "LICENSE"
readme = "README.md"
Expand Down
54 changes: 54 additions & 0 deletions src/fastcs/connections/serial_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.aioserial_instance = 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.aioserial_instance = aioserial.AioSerial(

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
port=settings.port, baudrate=settings.baud
)

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

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

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L28-L29

Added lines #L28 - L29 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 36 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L34 - L36 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 42 in src/fastcs/connections/serial_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L39-L42

Added lines #L39 - L42 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L45-L48

Added lines #L45 - L48 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L51

Added line #L51 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/serial_connection.py#L54

Added line #L54 was not covered by tests

0 comments on commit bdf405d

Please sign in to comment.