Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions drs4/ctrl/scpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ def recv(
encoding: str = DEFAULT_ENCODING,
) -> str:
"""Same as socket.recv(), but returns string, not bytes."""
received = super().recv(bufsize, flags)
string = received.decode(encoding).rstrip(end)
bend = end.encode(encoding)
received = bytearray()

while True:
if not (packet := super().recv(bufsize, flags)):
raise ConnectionError("Unexpected disconnect.")

received.extend(packet)

if received.endswith(bend):
break

string = received.decode(encoding).removesuffix(end)
host, port = self.getpeername()
LOGGER.debug(f"{host}:{port} -> {string}")
return string
Expand Down Expand Up @@ -115,7 +125,7 @@ def send_commands(
port: Port of the server.
timeout: Timeout value in units of seconds.
encoding: Encoding format for the commands.
autorecv: If True and a command ends with '?',
autorecv: If True and a command contains '?',
receive a message and record it to a logger.
bufsize: Maximum byte size for receiving a message.

Expand Down Expand Up @@ -151,7 +161,7 @@ def send_commands(

sock.send(command.strip(), encoding=encoding)

if autorecv and command.endswith("?"):
if autorecv and "?" in command:
messages.append(sock.recv(bufsize))

return tuple(messages)
Expand All @@ -175,7 +185,7 @@ def send_commands_in(
port: Port of the server.
timeout: Timeout value in units of seconds.
encoding: Encoding format for the commands.
autorecv: If True and a command ends with '?',
autorecv: If True and a command contains '?',
receive a message and record it to a logger.
bufsize: Maximum byte size for receiving a message.

Expand Down
Loading