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
|
import socket
import threading
import atexit
import re
IP = "0.0.0.0"
PORT = 5025
# Bind server socket when this module is included
_serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
_serverSocket.bind((IP, PORT))
_serverSocket.listen(1)
# Close it when the program exits
def _cleanUp():
_serverSocket.close()
atexit.register(_cleanUp)
class MockSDS1000XEDevice:
def __init__(self):
self._stopFlag = False
self._clientSocket = None
self._mainThread = threading.Thread(target=self._mainLoop)
self._mainThread.start()
# Mock internal values
self._channels = [{"AMPL" : None, "VDIV" : None} for i in range(0, 4)]
def _mainLoop(self) -> None:
self._clientSocket, _ = _serverSocket.accept()
self._clientSocket.settimeout(0.1)
try:
while not self._stopFlag:
try:
request = self._clientSocket.recv(4096).decode()
response = self._handleRequest(request.strip())
if response is not None:
self._clientSocket.send(response.encode())
except TimeoutError as e:
pass
finally:
self._clientSocket.close()
def _handleRequest(self, request: str) -> str:
m = re.search(r"C(?P<channel>\d):(?P<opcode>\w+)\??\s(?P<arg>.+)", request)
if not m:
return None
channelIndex = int(m.group("channel")) - 1
opcode = m.group("opcode")
if opcode == "PAVA":
arg = m.group("arg")
value = self._channels[channelIndex].get(arg)
unit = "Hz" if arg == "FREQ" else "V"
if value is None:
return None
else:
response = f"C{m.group('channel')}:PAVA {arg},{value:.6E}{unit}"
return response
elif opcode == "VDIV":
arg = float(m.group("arg").rstrip("V"))
self._channels[channelIndex]["VDIV"] = arg
return None
def stop(self) -> None:
self._stopFlag = True
self._mainThread.join()
def setAmplitude(self, channel: int, value: float) -> None:
self._channels[channel - 1]["AMPL"] = value
def setPeakToPeak(self, channel: int, value: float) -> None:
self._channels[channel - 1]["PKPK"] = value
def setRMS(self, channel: int, value: float) -> None:
self._channels[channel - 1]["RMS"] = value
def setFrequency(self, channel: int, value: float) -> None:
self._channels[channel - 1]["FREQ"] = value
def getVoltsPerDivision(self, channel: int) -> float:
return self._channels[channel - 1]["VDIV"]
|