-
Notifications
You must be signed in to change notification settings - Fork 0
/
SG386.py
266 lines (240 loc) · 7.38 KB
/
SG386.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import time
import serial
class SG386(object):
"""
Stanford Research Systems Model SG386 DC to 6.075 GHz Signal Generator.
https://www.thinksrs.com/downloads/pdfs/manuals/SG380m.pdf
"""
def __init__(self, serial_port):
self.open_connection(serial_port)
self.max_amp = 0.5000 # Volt Vpp
def open_connection(self, serial_port):
"""
e.g. On Linux: /dev/ttyUSB0
"""
try:
self.ser = serial.Serial(serial_port, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, rtscts=True, timeout=1)
if self.ser.isOpen():
print('\nConnection OPEN\n')
self.ser.write(b'*RST\n')
self.ser.write(b'ENBL 0\n')
self.ser.write(b'*CLS\n')
self.ser.write(b'REMT\n')
self.ser.write(b'DISP 6\n')
except serial.SerialException as e:
print(e)
raise
def close_connection(self):
try:
self.ser.write(b'ENBL 0\n')
self.ser.write(b'ENBR 0\n')
self.ser.write(b'LCAL\n')
self.ser.close()
print('\nConnection CLOSED\n')
except Exception as e:
print(e)
raise
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close_connection()
def get_identity(self):
"""
b'Stanford Research Systems,SG386,s/n001671,ver1.21.26\r\n'
"""
try:
self.ser.write(b'*IDN?\n')
r = self.ser.readline()
return r.decode('utf-8').strip()
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def get_modulation_type(self):
try:
self.ser.write(b'TYPE?\n')
r = self.ser.readline()
return int(r)
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def set_modulation_type(self, mtype):
try:
self.ser.write('TYPE {}\n'.format(mtype).encode('utf-8'))
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def get_modulation_function(self):
try:
self.ser.write(b'PFNC?\n')
r = self.ser.readline()
return int(r)
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def set_modulation_function(self, mfunc):
try:
self.ser.write('PFNC {}\n'.format(mfunc).encode('utf-8'))
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def get_frequency(self):
try:
self.ser.write(b'FREQ?\n')
r = self.ser.readline()
return float(r)
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def set_frequency(self, freq):
try:
self.ser.write('FREQ {}\n'.format(freq).encode('utf-8'))
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def get_pulse_period(self):
try:
self.ser.write(b'PPER?\n')
r = self.ser.readline()
return float(r)
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def set_pulse_period(self, bit_per):
try:
self.ser.write('PPER {}\n'.format(bit_per).encode('utf-8'))
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def get_pulse_duty_factor(self):
try:
self.ser.write(b'PDTY?\n')
r = self.ser.readline()
return float(r)
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def set_pulse_duty_factor(self, bit_len):
try:
self.ser.write('PDTY {}\n'.format(bit_len).encode('utf-8'))
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def get_modulation_state(self):
try:
self.ser.write(b'MODL?\n')
r = self.ser.readline()
return int(r)
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def set_modulation_state(self, mstate):
try:
self.ser.write('MODL {}\n'.format(mstate).encode('utf-8'))
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def get_RF_amplitude(self):
try:
self.ser.write(b'AMPR? Vpp\n')
r = self.ser.readline()
return float(r)
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def set_RF_amplitude(self, amp):
amp = float(amp)
try:
if abs(amp) <= self.max_amp:
self.ser.write('AMPR {} Vpp\n'.format(amp).encode('utf-8'))
# self.ser.write(b'DISP 6\n')
else:
print('Output amplitude too high!')
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def get_RF_state(self):
try:
self.ser.write(b'ENBR?\n')
r = self.ser.readline()
return int(r)
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def set_RF_state(self, rfstate):
try:
self.ser.write('ENBR {}\n'.format(rfstate).encode('utf-8'))
except serial.SerialException as e:
print(e)
raise
except Exception as e:
print(e)
raise
def display_status(self):
status = {
'Device Identity': self.get_identity(),
'Modulation Type': self.get_modulation_type(),
'Modulation Function': self.get_modulation_function(),
'Signal Frequency': self.get_frequency(),
'Pulse Period': self.get_pulse_period(),
'Pulse Duty Factor': self.get_pulse_duty_factor(),
'Modulation State': self.get_modulation_state(),
'RF Amplitude': self.get_RF_amplitude(),
'RF State': self.get_RF_state()
}
print("{:<25} {:<10}".format('Parameter','Value'))
print('='*80)
for k, v in status.items():
# print(k, v)
print("{:<25} {:<10}".format(k, v))
if __name__ == '__main__':
serial_port = '/dev/ttyUSB0'
with SG386(serial_port) as dev:
dev.display_status()