-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHAMEG_HM1507.py
114 lines (102 loc) · 2.43 KB
/
HAMEG_HM1507.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
import serial
import time
import logging
class HAMEG_HM1507:
TIME_DIVS = [
"50ns",
"100ns",
"200ns",
"500ns",
"1us",
"2us",
"5us",
"10us",
"20us",
"50us",
"100us",
"200us",
"500us",
"1ms",
"2ms",
"5ms",
"10ms",
"20ms",
"50ms",
"100ms",
"200ms",
"500ms",
"1s",
"2s",
"5s",
"10s",
"20s",
"50s",
"100s"
]
V_DIVS = [
"1mV",
"2mV",
"5mV",
"10mV",
"20mV",
"50mV",
"100mV",
"200mV",
"500mV",
"1V",
"2V",
"5V",
"10V",
"20V"
]
def __init__(self):
self._ser = None
self._logger = logging.getLogger("HAMEG_HM1507")
def __del__(self):
self.disconnect()
def connect(self, port):
self.disconnect()
self._logger.info("connecting")
self._ser = serial.Serial(port=port, baudrate=19200, timeout=1,
rtscts=True, stopbits=serial.STOPBITS_TWO,
parity=serial.PARITY_NONE)
self._command(" ")
def disconnect(self):
if self._ser is not None:
self._logger.info("disconnecing")
self._command("rm0")
self._ser.close()
self._ser = None
def _command(self, command):
self._logger.debug("sending command: "+command)
if self._ser is not None:
for ch in command:
self._ser.write(ch.encode("utf-8"))
self._ser.write(b"\r")
# print(ser.readline())
else:
self._logger.warning("Not connected!")
def autoset(self):
self._command("AUTOSET")
def timeBase(self, base, single, timeDiv):
if (base is not "A") and (base is not "B"):
raise IndexError("Only time base A and B exist")
settings = 0
if single:
settings += 32
if timeDiv not in self.TIME_DIVS:
raise IndexError("Unknown timeDiv!")
settings += self.TIME_DIVS.index(timeDiv)
self._command("TB"+str(base)+"="+chr(settings))
def channel(self, channel, inv, modeDc, vDiv):
if (channel is not 1) and (channel is not 2):
raise IndexError("Only channel 1 and 2 exist")
settings = 16
if inv:
settings += 32
if not modeDc:
settings += 64
if vDiv not in self.V_DIVS:
raise IndexError("Unknown vDiv!")
settings += self.V_DIVS.index(vDiv)
self._command("CH"+str(channel)+"="+chr(settings))