/
lockin_controller.py
78 lines (68 loc) · 2.38 KB
/
lockin_controller.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
import serial
import time
class lockinController():
### Initialization ###
def __init__(self, serial_port='/dev/ttyUSB2', terminator='\n'):
# For linux, theself.serial port will be something like the default address.
# However, for windows it will be something like 'COM6'.
self.address=serial_port
self.baudrate=19200
self.terminator=terminator
self.ser=serial.Serial(self.address, baudrate=self.baudrate,timeout=1,rtscts=True)
#self.ser.close()
self.setup_rs232_output()
def setup_rs232_output(self):
#ser=serial.Serial(self.address, baudrate=self.baudrate)
#self.ser.open()
try:
self.ser.write('OUTX 0'+self.terminator)
except Exception as e:
print e
raise e
finally:
pass
# self.ser.close()
### Basic send and receive methods ###
def send(self,msg):
#self.ser=serial.Serial(self.address, baudrate=self.baudrate)
# self.ser.open()
time.sleep(.1)
try:
self.ser.write(msg+self.terminator)
except Exception as e:
print e
raise e
finally:
pass
# self.ser.close()
def send_and_receive(self,msg):
#self.ser=serial.Serial(self.address, baudrate=self.baudrate, timeout=2)
# self.ser.open()
time.sleep(.1)
# This delay is necessary... for some reason. The system behaves very poorly otherwise.
# Perhaps theself.serial port takes some time to initialize?
try:
self.ser.write(msg+self.terminator)
return self.read_until_terminator()
except Exception as e:
print e
raise e
finally:
pass
# self.ser.close()
def read_until_terminator(self):
message=''
new_char=None
while new_char!='\r':
new_char= self.ser.read(1)
if new_char=='':
# This meansself.ser has timed out. We don't want an unending loop if the terminator has somehow been lost.
print 'Serial port timed out while reading.'
break
message+=new_char
return message
### Stub methods ###
def get_data(self):
return self.send_and_receive('SNAP? 1,2,3,4')
def get_idn(self):
return self.send_and_receive('*IDN?')