-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaq.py
More file actions
157 lines (115 loc) · 4.8 KB
/
daq.py
File metadata and controls
157 lines (115 loc) · 4.8 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
# vim: tabstop=4 shiftwidth=4 expandtab
"""
hmsubs subclass for superlogics daq units
"""
import time
import hmsubs
class daq(hmsubs.hmsub):
""" subclass for rs-485 daq units
Methods:
read_number
Read back a value from the daq
measure_channel
An alias for read_number
"""
def __init__(self, configfile=None):
super().__init__(configfile=configfile)
self.my_procs = {
"ReadNumber" : self.read_number,
"ReadChannel" : self.read_number,
"MeasureChannel" : self.read_number,
"CloseRelay" : self.close_relay,
"OpenRelay" : self.open_relay,
}
self.action_procs.update(self.my_procs)
#------------------------------------------------------------------------
def read_number(self, device, option):
""" Read a single floating point value back from a device
Has alias names ReadChannel, MeasureChannel
Action line syntax:
0 ReadData device channelnum
0 ReadChannel device channelnum
0 MeasureChannel device channelnum
Assumes device is already configured for the desired data type.
option is a string containing 'AAN' where AA is the hex id number of the device,
and N is the channel number, eg. 011 is id 1, channel 1
"""
cmd = "#%s" % (option)
self.logger.debug("ReadNumber command = %s", cmd)
answer = device.send_read(cmd)
if not answer:
self.logger.error("ReadNumber failed on device %s.", device.name)
return 0
self.logger.debug("ReadNumber answer = %s", answer)
try:
if answer.startswith(">"):
val = float(answer[1:])
elif answer.startswith("?"):
self.logger.error("ReadNumber: Error response from daq '%s'.", answer)
return 0
except ValueError as err:
self.logger.error("ReadNumber: Cannot scan value from string '%s'.", answer)
self.logger.error(err)
return 0
self.last_time = time.time()
self.last_data = val
self.last_sdev = 0.0
self.last_n = 1
self.logger.info("ReadNumber %s %s %f at %s", device.name, option, val, self.last_time)
return val
#------------------------------------------------------------------------
def close_relay(self, device, option):
""" Close a relay on a switch device.
Action line syntax:
0 CloseRelay device 207
option is a string containing 'AAN' where AA is the hex id number of the device,
and N is the channel number, eg. 011 is id 1, channel 1
e.g. 0 CloseRelay daq 060 # close relay 0 on device id 06
"""
val = int(option)
card = val/10
relay = val % 10
cmd = "#%02d1%d01" % (card, relay)
self.logger.debug("CloseRelay command = %s", cmd)
answer = device.send_read(cmd)
if not answer:
self.logger.error("CloseRelay failed on device %s.", device.name)
self.logger.debug("CloseRelay answer = %s", answer)
try:
if answer.startswith(">"):
pass
elif answer.startswith("?"):
self.logger.error("CloseRelay: Error response from daq '%s'.", answer)
return 0
except ValueError as err:
self.logger.error("CloseRelay: Cannot check reply from string '%s'.", answer)
self.logger.error(err)
return 0
#------------------------------------------------------------------------
def open_relay(self, device, option):
""" Open a relay on a switch device.
Action line syntax:
0 OpenRelay device 207
option is a string containing 'AAN' where AA is the hex id number of the device,
and N is the channel number, eg. 011 is id 1, channel 1
e.g. 0 OpenRelay daq 060 # open relay 0 on device id 06
"""
val = int(option)
card = val/10
relay = val % 10
cmd = "#%02d1%d00" % (card, relay)
self.logger.debug("OpenRelay command = %s", cmd)
answer = device.send_read(cmd)
if not answer:
self.logger.error("OpenRelay failed on device %s.", device.name)
self.logger.debug("OpenRelay answer = %s", answer)
try:
if answer.startswith(">"):
pass
elif answer.startswith("?"):
self.logger.error("OpenRelay: Error response from daq '%s'.", answer)
return 0
except ValueError as err:
self.logger.error("OpenRelay: Cannot check reply from string '%s'.", answer)
self.logger.error(err)
return 0