Skip to content

Commit

Permalink
Initial scaffolding for SCPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaughn Kottler authored and Vaughn Kottler committed Jun 10, 2024
1 parent 5b3d4d9 commit 622dc50
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tasks/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ includes_left:
- package://runtimepy/server.yaml
- package://runtimepy/server_dev.yaml

factories:
- {name: tasks.scpi.ScpiConn}
clients:
- factory: scpi_conn
name: siglent
kwargs: {host: siglent_psu1, port: 5025}

app:
- runtimepy.net.apps.wait_for_stop
58 changes: 58 additions & 0 deletions tasks/scpi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
A module implementing an SCPI interface.
"""

# built-in
import asyncio

# internal
from runtimepy.net.arbiter.tcp import TcpConnectionFactory
from runtimepy.net.tcp import TcpConnection


class ScpiConnection(TcpConnection):
"""A simple SCPI connection class."""

def init(self) -> None:
"""Initialize this instance."""

self.command_lock = asyncio.Lock()
self.message_queue: asyncio.Queue[str] = asyncio.Queue(maxsize=1)

async def async_init(self) -> bool:
"""Initialize this instance."""

self.logger.info(await self.send_command("*IDN?"))

return True

async def process_text(self, data: str) -> bool:
"""Process a text frame."""

for item in data.split("\r\n"):
if item:
await self.message_queue.put(item)

return True

async def process_binary(self, data: bytes) -> bool:
"""Process a binary frame."""
return await self.process_text(data.decode())

async def send_command(self, command: str, response: bool = True) -> str:
"""Send a command."""

async with self.command_lock:
self.send_text(command + "\n")

result = ""
if response:
result = await self.message_queue.get()

return result


class ScpiConn(TcpConnectionFactory[ScpiConnection]):
"""A connection factory for SCPI devices."""

kind = ScpiConnection

0 comments on commit 622dc50

Please sign in to comment.