-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscpi.py
183 lines (138 loc) · 4.64 KB
/
scpi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
__all__ = [
"connect",
"send_commands",
"send_commands_in",
]
# standard library
from logging import getLogger
from socket import socket, AF_INET, SOCK_STREAM
from typing import Optional, Sequence, Union
from pathlib import Path
# constants
AUTORECV: bool = True
BUFSIZE: int = 4096
ENCODING: str = "ascii"
END: str = "\n"
FLAGS: int = 0
TIMEOUT: Optional[float] = None
# module logger
logger = getLogger(__name__)
# type aliases
PathLike = Union[Path, str]
# main feature
def send_commands(
commands: Union[Sequence[str], str],
host: str,
port: int,
timeout: Optional[float] = TIMEOUT,
encoding: str = ENCODING,
autorecv: bool = AUTORECV,
bufsize: int = BUFSIZE,
) -> None:
"""Send SCPI command(s) to a server.
Args:
commands: Sequence of SCPI commands.
host: IP address or host name of the server.
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 '?',
receive a message and record it to a logger.
bufsize: Maximum byte size for receiving a message.
Returns:
This function returns nothing.
Examples:
To send an SCPI command to the server::
send_commands('*CLS', '192.168.1.3', 5000)
To send SCPI commands to the server::
send_commands(['*RST', '*CLS'], '192.168.1.3', 5000)
"""
if isinstance(commands, str):
commands = (commands,)
with connect(host, port, timeout) as sock:
for command in commands:
if not command or command.startswith("#"):
continue
sock.send(command.strip(), encoding=encoding)
if autorecv and command.endswith("?"):
sock.recv(bufsize)
def send_commands_in(
path: PathLike,
host: str,
port: int,
timeout: Optional[float] = TIMEOUT,
encoding: str = ENCODING,
autorecv: bool = AUTORECV,
bufsize: int = BUFSIZE,
) -> None:
"""Send SCPI command(s) written in a file to a server.
Args:
path: Path of the file.
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 '?',
receive a message and record it to a logger.
bufsize: Maximum byte size for receiving a message.
Returns:
This function returns nothing.
Examples:
If a text file, commands.txt, has SCPI commands::
*RST
*CLS
then the following two commands are equivalent::
send_commands(['*RST', '*CLS'], '192.168.1.3', 5000)
send_commands_in('commands.txt', '192.168.1.3', 5000)
"""
with open(path, encoding=encoding) as f:
send_commands(f, host, port, timeout, encoding, autorecv, bufsize)
# helper features
class CustomSocket(socket):
"""Custom socket class to send/recv string with logging."""
def send(
self,
string: str,
flags: int = FLAGS,
end: str = END,
encoding: str = ENCODING,
) -> int:
"""Same as socket.send(), but accepts string, not bytes."""
encoded = (string + end).encode(encoding)
n_bytes = super().send(encoded, flags)
host, port = self.getpeername()
logger.info(f"{host}:{port} <- {string}")
return n_bytes
def recv(
self,
bufsize: int = BUFSIZE,
flags: int = FLAGS,
end: str = END,
encoding: str = ENCODING,
) -> str:
"""Same as socket.recv(), but returns string, not bytes."""
received = super().recv(bufsize, flags)
string = received.decode(encoding).rstrip(end)
host, port = self.getpeername()
logger.info(f"{host}:{port} -> {string}")
return string
def connect(host: str, port: int, timeout: Optional[float] = TIMEOUT) -> CustomSocket:
"""Connect to an SCPI server and returns a custom socket object.
Args:
host: IP address or host name of the server.
port: Port of the server.
timeout: Timeout value in units of seconds.
Returns:
Custom socket object.
Examples:
To send an SCPI command to a server::
with connect('192.168.1.3', 5000) as sock:
sock.send('*CLS')
To receive a message from a server::
with connect('192.168.1.3', 5000) as sock:
sock.send('SYST:ERR?')
print(sock.recv())
"""
sock = CustomSocket(AF_INET, SOCK_STREAM)
sock.settimeout(timeout)
sock.connect((host, port))
return sock