-
Notifications
You must be signed in to change notification settings - Fork 1
/
TF930.py
66 lines (51 loc) · 1.94 KB
/
TF930.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
'''
A wrapper class for using a TF930 frequency counter
via a serial port.
Created on 7 May 2016
@author: Tom Barrett
'''
import serial
import time
import re
import threading
class TF930(serial.Serial):
def __init__(self, port='COM5', timeout=3, **kwargs):
print 'Opening serial connection to TF930 on port {0}...'.format(port)
serial.Serial.__init__(
self,
port=port,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = timeout,
)
print '...connection {0}'.format('successful' if self.is_open else 'failed')
def query_frequency(self, read_delay=0.5):
output = self.query('N?', delay=0.5)
# Parse the output, once for units and once for values
r = r'([\d|\.|e|\+]+)([a-zA-Z]*)\r\n'
freq, units = r'N/A', r'N/A'
match = re.match(r, output)
if match:
freq, units = float(match.group(1)), match.group(2)
# Just a hack to convert Hz to MHz as it's nicer.
if units == 'Hz':
freq = freq/10**6
units = 'MHz'
return freq, units
def write(self, string):
return serial.Serial.write(self, string + '\n')
def read(self, size=1):
out=''
while self.inWaiting() > 0:
out += serial.Serial.read(self, size=size)
return out
def query(self, string, delay=1):
'''Write a query and return the result after a designated delay (1s by default).'''
self.write(string)
time.sleep(delay)
return self.read()
def close(self):
serial.Serial.close(self)
print 'Serial connection to TF930 closed'