-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vaughn Kottler
authored and
Vaughn Kottler
committed
Jun 10, 2024
1 parent
5b3d4d9
commit 622dc50
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
Submodule config
updated
7 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |