-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalon_synth9.py
More file actions
397 lines (335 loc) · 10.5 KB
/
valon_synth9.py
File metadata and controls
397 lines (335 loc) · 10.5 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
Provides a serial interface to the Valon 5009.
"""
# Python modules
import serial
from time import sleep, time
__author__ = "Brad Dober"
__license__ = "GPL"
__version__ = "1.0"
# Handy aliases
SYNTH_A = 1
SYNTH_B = 2
INT_REF = 0
EXT_REF = 1
class Synthesizer:
"""A simple interface to the Valon 5009 synthesizer."""
def __init__(self, port):
self.conn = serial.Serial(None, 9600, serial.EIGHTBITS,
serial.PARITY_NONE, serial.STOPBITS_ONE, timeout = 0.500)
self.conn.setPort(port)
def get_frequency(self, synth):
"""
Returns the current output frequency for the selected synthesizer.
@param synth : synthesizer this command affects (1 or 2).
@type synth : int
@return: the frequency in MHz (float)
"""
self.conn.open()
data = 'f'+str(synth)+'?\r'
self.conn.write(data)
self.conn.flush()
t = time()
data = ''
while (len(data) < 40 and time()-t < 1): data += self.conn.read(40)
self.conn.close()
data = data.split(' Act ')[1]
data = data.split(' ')[0]
return float(data) #in MHz#
def set_frequency(self, synth, freq, chan_spacing = 10.):
"""
Sets the synthesizer to the desired frequency
Sets to the closest possible frequency, depending on the channel spacing.
Range is determined by the minimum and maximum VCO frequency.
@param synth : synthesizer this command affects (1 or 2).
@type synth : int
@param freq : output frequency
@type freq : float
@param chan_spacing : deprecated
@type chan_spacing : float
@return: True if success (bool)
"""
self.conn.open()
data = 's'+str(synth)+';f'+str(freq)+'\r'
self.conn.write(data)
self.conn.flush()
t = time()
data = ''
while (len(data) < 40 and time()-t < 1): data += self.conn.read(40)
self.conn.close()
data = data.split(' Act ')[1]
data = data.split(' ')[0]
return float(data)==float(freq)
def get_reference(self):
"""
Get reference frequency in MHz
"""
self.conn.open()
data = 'REF?\r'
self.conn.write(data)
self.conn.flush()
data = self.conn.read(100)
self.conn.close()
#print data
freq=data.split(' ')[1]
return float(freq) #in MHz
def set_reference(self, freq):
"""
Set reference frequency in MHz
@param freq : frequency in MHz
@type freq : float
@return: True if success (bool)
"""
self.conn.open()
data = 'REF '+str(freq)+'M\r'
self.conn.write(data)
self.conn.flush()
t = time()
data = ''
while (len(data) < 40 and time()-t < 1): data += self.conn.read(40)
self.conn.close()
#print data
ack=data.split(' ')[2]
#print ack
return ack == str(freq)
def set_refdoubler(self, synth, enable):
"""
Set reference doubler
@param synth : synthesizer this command affects (1 or 2).
@type synth : int
@param enable : turn on or off the reference doubler
@type enable : bool
@return: True if success (bool)
"""
if enable:
self.conn.open()
data = 's'+str(synth)+';REFDB E\r'
self.conn.write(data)
self.conn.flush()
else:
self.conn.open()
data = 's'+str(synth)+';REFDB D\r'
self.conn.write(data)
self.conn.flush()
data = self.conn.read(100)
self.conn.close()
#print data
ack=data.split(' ')[2]
ack=ack.split(';')[0]
return int(ack) == 0
def get_refdoubler(self, synth):
"""
Get reference doubler
@param synth : synthesizer this command affects (1 or 2).
@type synth : int
@return: True if on, False if off (bool)
"""
self.conn.open()
data = 'REFDB'+str(synth)+'?\r'
self.conn.write(data)
self.conn.flush()
data = self.conn.read(100)
self.conn.close()
#print data
ack=data.split('REFDB')[2]
ack=ack.split(';')[0]
return int(ack)
def get_rf_level(self, synth):
"""
Returns RF level in dBm
@param synth : synthesizer address, 1 or 2
@type synth : int
@return: dBm (int)
"""
self.conn.open()
data = 's'+str(synth)+';Att?\r'
self.conn.write(data)
self.conn.flush()
data = self.conn.read(100)
self.conn.close()
#print data
data=data.split('ATT ')[1]
data=data.split(';')[0]
rf_level=float(data)-15
return rf_level
def set_rf_level(self, synth, rf_level):
"""
Set RF level
@param synth : synthesizer address, 1 or 2
@type synth : int
@param rf_level : RF power in dBm
@type rf_level : float
@return: True if success (bool)
"""
"15 dB is equal to 0 dB ouput power"
"can be set from 0 (+15) to 31.5 (-16.5)"
# Writing 0 dBm results in ~0.7 dBm output
rf_level -= 1.
if -16.5<=rf_level<=15:
atten=-rf_level+15
#print atten
data='s'+str(synth)+';att'+str(atten)+'\r'
self.conn.open()
self.conn.write(data)
self.conn.flush()
data = self.conn.read(1000)
self.conn.close()
ack=data.split('ATT ')[1]
ack=ack.split(';')[0]
#print ack
return float(ack) == float(atten)
else:
return False
def set_pfd(self, synth, freq):
"""
Sets the synthesizer's phase/frequency detector to the desired frequency
@param synth : synthesizer this command affects (1 or 2).
@type synth : int
@param freq : pfd frequency
@type freq : float
@return: True if success (bool)
"""
self.conn.open()
data = 's'+str(synth)+';PFD'+str(freq)+'M\r'
self.conn.write(data)
self.conn.flush()
data=self.conn.read(100)
self.conn.close()
#print data
data = data.split('PFD ')[1]
#print data
data = data.split(' MHz')[0]
#print data
return int(data)==int(freq)
def get_pfd(self, synth):
"""
Gets the synthesizer's phase/frequency detector to the desired frequency
@param synth : synthesizer this command affects (1 or 2).
@type synth : int
@return: True if success (bool)
"""
self.conn.open()
data = 'PFD'+str(synth)+'?\r'
self.conn.write(data)
self.conn.flush()
data=self.conn.read(100)
self.conn.close()
#print data
data = data.split('PFD ')[1]
data = data.split(' MHz')[0]
return float(data)
def get_ref_select(self):
"""
Returns the currently selected reference clock.
Returns 1 if the external reference is selected, 0 otherwise.
"""
self.conn.open()
data = 'REFS?\r'
self.conn.write(data)
self.conn.flush()
t = time()
data = ''
while (len(data) < 40 and time()-t < 1): data += self.conn.read(40)
self.conn.close()
data=data.split(' ')[1]
data=data.split(';')[0]
return int(data)
def set_ref_select(self, e_not_i = 1):
"""
Selects either internal or external reference clock.
@param e_not_i : 1 (external) or 0 (internal); default 1
@type e_not_i : int
@return: True if success (bool)
"""
data='REFS'+str(e_not_i)+'\r'
self.conn.open()
self.conn.write(data)
self.conn.flush()
t = time()
data = ''
while (len(data) < 40 and time()-t < 1): data += self.conn.read(40)
self.conn.close()
ack=data.split(' ')[1]
ack=ack.split(';')[0]
return ack == str(e_not_i)
def get_vco_range(self, synth):
"""
Returns (min, max) VCO range tuple.
@param synth : synthesizer base address
@type synth : int
@return: min,max in MHz
"""
self.conn.open()
data = struct.pack('>B', 0x83 | synth)
self.conn.write(data)
self.conn.flush()
data = self.conn.read(4)
checksum = self.conn.read(1)
self.conn.close()
#_verify_checksum(data, checksum)
return struct.unpack('>HH', data)
def set_vco_range(self, synth, low, high):
"""
Sets VCO range.
@param synth : synthesizer base address
@type synth : int
@param min : minimum VCO frequency
@type min : int
@param max : maximum VCO frequency
@type max : int
@return: True if success (bool)
"""
self.conn.open()
data = struct.pack('>BHH', 0x03 | synth, low, high)
checksum = _generate_checksum(data)
self.conn.write(data + checksum)
self.conn.flush()
data = self.conn.read(1)
self.conn.close()
ack = struct.unpack('>B', data)[0]
return ack == ACK
def get_phase_lock(self, synth):
"""
Get phase lock status
@param synth : synthesizer base address
@type synth : int
@return: True if locked (bool)
"""
self.conn.open()
data = "LOCK"+str(synth)+"\r"
self.conn.write(data)
self.conn.flush()
data = self.conn.read(100)
self.conn.close()
if "locked" in data:
return True
else:
return False
def flash(self):
"""
Flash current settings for both synthesizers into non-volatile memory.
@return: True if success (bool)
"""
self.conn.open()
data = 'SAV\r'
self.conn.write(data)
self.conn.flush()
ack = self.conn.read(100)
self.conn.close()
if "No" in ack:
return False
else:
return True
def reset(self):
"""
Resets the Valon to factory settings
@return: True if success (bool)
"""
self.conn.open()
data = 'RST\r'
self.conn.write(data)
self.conn.flush()
data = self.conn.read(100)
self.conn.close()
print data
return True