-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFluke_8846A_class.py
357 lines (278 loc) · 15.3 KB
/
Fluke_8846A_class.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat 04. June CET 2022
@author: Bjoern Kasper (urmel79)
Wrapper class to communicate with the DMM Fluke 8846A via LAN interface and SCPI commands using TCP sockets
"""
import socket
import time, sys
class Fluke_8846A():
def __init__(self, tcp_ip, tcp_port):
self._ip = tcp_ip
self._port = tcp_port
self._delay = 0.005 # delay for writing the commands in seconds (5 ms)
self._bytes2read = 40
self._sock_timeout = 0.1 # timeout for reading TCP sockets in [s]
self._measurement_configuration = ''
self._measurement_configured = False
self.conf_measurement_dict = { "00_RES": 'CONF:RES DEF', # resistor 2-wire
"01_FRES": 'CONF:FRES DEF', # resistor 4-wire
"02_RTD": 'CONF:TEMP:RTD', # PT100, 2-wire,
"03_FRTD": 'CONF:TEMP:FRTD', # PT100, 4-wire,
"04_RTD_RES": 'FUNC1 "TEMP:RTD"; FUNC2 "RES"', # PT100, 2-wire, resistor 2-wire (secondary display)
"05_FRTD_RES": 'FUNC1 "TEMP:FRTD"; FUNC2 "FRES"', # PT100, 4-wire, resistor 4-wire (secondary display)
"06_VOLT_AC": 'CONF:VOLT:AC DEF', # voltage AC
"07_VOLT_AC_FREQ": 'FUNC1 "VOLT:AC"; FUNC2 "FREQ"', # voltage AC, frequency (secondary display)
"08_VOLT_DC": 'CONF:VOLT:DC DEF', # voltage DC
"09_CURR_AC": 'CONF:CURR:AC DEF', # current AC
"10_CURR_AC_FREQ": 'FUNC1 "CURR:AC"; FUNC2 "FREQ"', # current AC, frequency (secondary display)
"11_CURR_DC": 'CONF:CURR:DC DEF', # current DC
"12_CONT": 'CONF:CONT', # continuity
"13_CAP": 'CONF:CAP DEF' # capacitance
}
try:
if (self._ip == [] or self._port == []):
self.status = "No IP address or port provided"
else:
self.dmm_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.dmm_sock.connect((self._ip, self._port))
# set timeout on blocking socket operations in [s]
self.dmm_sock.settimeout(self._sock_timeout)
self.status = "Connected"
self.list_dev_infos = self.getDevInfos()
self.connected_with = '%s %s over LAN on %s, port %s' %(self.list_dev_infos[0], self.list_dev_infos[1], self._ip, self._port)
self._measurement_configured = False
except Exception as e:
self.status = "Disconnected"
self.connected_with = 'Nothing'
print("Something's went wrong while opening %s:%d. Exception is %s" % (self._ip, self._port, e))
# define an internal CLEAR INPUT BUFFER function
def _clearInputBuffer(self):
try:
# read answer with buffer size of 40 bytes and drop it
while self.dmm_sock.recv(self._bytes2read):
#time.sleep(self._delay)
pass
except:
pass
# define an internal READ SOCKET function
def _readSocket(self, int_bytes):
self.dmm_sock.settimeout(0.01)
# define a knock out counter to break while loop
self.knock_out_counter = 10
while self.knock_out_counter >= 0:
# decrease knock out counter
self.knock_out_counter -= 1
#print("Knock out counter: {}".format(self.knock_out_counter)) # output only for debugging
try:
self.ret_val = self.dmm_sock.recv(int_bytes)
except socket.timeout as e:
self._err = e.args[0]
# this next if/else is a bit redundant, but illustrates how the
# timeout exception is setup
if self._err == 'timed out':
time.sleep(0.3)
#print('recv() timed out, retry later') # output only for debugging
continue
else:
print(e)
return -1
except socket.error as e:
# Something else happened, handle error, exit funktion, etc.
print(e)
return -1
else:
if len(self.ret_val) == 0:
print('orderly shutdown on server end')
return -1
else:
# got a message: return it :)
return self.ret_val
# define a OPEN CONNECTION function
def openConnection(self, tcp_ip, tcp_port):
try:
if self.status == "Disconnected":
if (tcp_ip == [] or tcp_port == []):
self.status = "No IP address or port provided"
else:
self.dmm_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.dmm_sock.connect((tcp_ip, tcp_port))
# set timeout on blocking socket operations in [s]
self.dmm_sock.settimeout(self._sock_timeout)
self.status = "Connected"
self.list_dev_infos = self.getDevInfos()
self.connected_with = '%s %s over LAN on %s, port %s' %(self.list_dev_infos[0], self.list_dev_infos[1], self._ip, self._port)
self._measurement_configured = False
except Exception as e:
self.status = "Disconnected"
self.connected_with = 'Nothing'
print("Something's went wrong while opening %s:%d. Exception is %s" % (tcp_ip, tcp_port, e))
# define a CLOSE CONNECTION function
def closeConnection(self):
try:
if self.status == "Connected":
self.dmm_sock.close()
self.status = "Disconnected"
self.connected_with = "Nothing"
except Exception as e:
self.status = "Error"
print("Something's went wrong while closing %s:%d. Exception is %s" % (self._ip, self._port, e))
# define a GET DEVice INFOrmation function
def getDevInfos(self):
if (self.status != "Connected"):
print("Device is not connected")
self._measurement_configured = False
return -1
# clear input buffer before reading
self._clearInputBuffer()
# get current measurement configuration
self.cmd = '*IDN?\n'
self.dmm_sock.sendall(self.cmd.encode('utf-8'))
time.sleep(self._delay)
# read answer with buffer size of 64 bytes
self.ret_val = self._readSocket(int_bytes=self._bytes2read)
# strip whitespaces and newline characters from string
self.ret_val = self.ret_val.decode().strip()
# split string into list
self.ret_list = self.ret_val.split(',')
return self.ret_list
# define a CONFigure MEASUREMENT function
def confMeasurement(self, measConf_str):
if (self.status != "Connected"):
print("Device is not connected")
self._measurement_configured = False
return -1
self._measurement_configuration = measConf_str
# check valid measurement type
if self._measurement_configuration not in self.conf_measurement_dict:
self._measurement_configured = False
raise TypeError("Configuration {} is NOT a valid one".format(self._measurement_configuration))
# reset device
self.cmd = '*RST\n'
self.dmm_sock.sendall(self.cmd.encode('utf-8'))
time.sleep(self._delay)
# get device into remote mode
self.cmd = "SYST:REM\n"
self.dmm_sock.sendall(self.cmd.encode('utf-8'))
time.sleep(self._delay)
self.cmd = "%s\n" %self.conf_measurement_dict[self._measurement_configuration]
self.dmm_sock.sendall(self.cmd.encode('utf-8'))
time.sleep(self._delay)
self._measurement_configured = True
self._dict_dmm_measurement = {}
if ((self._measurement_configuration == '00_RES') or
(self._measurement_configuration == '01_FRES')):
self._dict_dmm_measurement['resistance_value'] = 0
self._dict_dmm_measurement['resistance_unit'] = 'Ohm'
elif ((self._measurement_configuration == '02_RTD') or
(self._measurement_configuration == '03_FRTD')):
self._dict_dmm_measurement['temperature_value'] = 0
self._dict_dmm_measurement['temperature_unit'] = '°C'
elif ((self._measurement_configuration == '04_RTD_RES') or
(self._measurement_configuration == '05_FRTD_RES')):
self._dict_dmm_measurement['temperature_value'] = 0
self._dict_dmm_measurement['temperature_unit'] = '°C'
self._dict_dmm_measurement['resistance_value'] = 0
self._dict_dmm_measurement['resistance_unit'] = 'Ohm'
elif (self._measurement_configuration == '06_VOLT_AC'):
self._dict_dmm_measurement['voltage_value'] = 0
self._dict_dmm_measurement['voltage_unit'] = 'V AC'
elif self._measurement_configuration == '07_VOLT_AC_FREQ':
self._dict_dmm_measurement['voltage_value'] = 0
self._dict_dmm_measurement['voltage_unit'] = 'V AC'
self._dict_dmm_measurement['frequency_value'] = 0
self._dict_dmm_measurement['frequency_unit'] = 'Hz'
elif self._measurement_configuration == '08_VOLT_DC':
self._dict_dmm_measurement['voltage_value'] = 0
self._dict_dmm_measurement['voltage_unit'] = 'V DC'
elif (self._measurement_configuration == '09_CURR_AC'):
self._dict_dmm_measurement['current_value'] = 0
self._dict_dmm_measurement['current_unit'] = 'A AC'
elif self._measurement_configuration == '10_CURR_AC_FREQ':
self._dict_dmm_measurement['current_value'] = 0
self._dict_dmm_measurement['current_unit'] = 'A AC'
self._dict_dmm_measurement['frequency_value'] = 0
self._dict_dmm_measurement['frequency_unit'] = 'Hz'
elif self._measurement_configuration == '11_CURR_DC':
self._dict_dmm_measurement['current_value'] = 0
self._dict_dmm_measurement['current_unit'] = 'A DC'
elif self._measurement_configuration == '12_CONT':
self._dict_dmm_measurement['continuity_value'] = 0
self._dict_dmm_measurement['continuity_unit'] = 'Ohm'
elif self._measurement_configuration == '13_CAP':
self._dict_dmm_measurement['capacitancy_value'] = 0
self._dict_dmm_measurement['capacitancy_unit'] = 'F'
# define a GET CONFIG function
def getConfig(self):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if not self._measurement_configured:
print("Measurement is not configured")
return -1
# clear input buffer before reading
self._clearInputBuffer()
# get current measurement configuration
self.cmd = 'CONF?\n'
self.dmm_sock.sendall(self.cmd.encode('utf-8'))
time.sleep(self._delay)
# read answer with buffer size of 64 bytes
self.ret_val = self._readSocket(int_bytes=self._bytes2read)
# strip whitespaces and newline characters from string
self.ret_val = self.ret_val.decode().strip()
return self.ret_val
# define a GET MEASUREMENT function
def getMeasurement(self):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if not self._measurement_configured:
print("Measurement is not configured")
return -1
# # clear input buffer before reading
# self._clearInputBuffer()
#print(self._measurement_configuration)
if ((self._measurement_configuration == '04_RTD_RES') or
(self._measurement_configuration == '05_FRTD_RES') or
(self._measurement_configuration == '07_VOLT_AC_FREQ') or
(self._measurement_configuration == '10_CURR_AC_FREQ')):
self.cmd = 'READ?; FETCH2?\n'
self.dmm_sock.sendall(self.cmd.encode('utf-8'))
time.sleep(self._delay)
# read answer with buffer size of 64 bytes
self.ret_val = self._readSocket(int_bytes=self._bytes2read)
# strip whitespaces and newline characters from string and cast to float
self.ret_val_list = self.ret_val.decode().strip().split(';')
# print(self.ret_val_list)
# cast list elements to float
for self._idx, self._val in enumerate(self.ret_val_list):
self.ret_val_list[self._idx] = float(self.ret_val_list[self._idx])
# write value at the primary value key
self.prim_val_key = list(self._dict_dmm_measurement.keys())[0]
self._dict_dmm_measurement[self.prim_val_key] = self.ret_val_list[0]
if ((self._measurement_configuration == '04_RTD_RES') or
(self._measurement_configuration == '05_FRTD_RES')):
self._dict_dmm_measurement['resistance_value'] = self.ret_val_list[1]
elif ((self._measurement_configuration == '07_VOLT_AC_FREQ') or
(self._measurement_configuration == '10_CURR_AC_FREQ')):
self._dict_dmm_measurement['frequency_value'] = self.ret_val_list[1]
else:
self.cmd = 'READ?\n'
self.dmm_sock.sendall(self.cmd.encode('utf-8'))
time.sleep(self._delay)
# read answer with buffer size of 64 bytes
self.ret_val = self._readSocket(int_bytes=self._bytes2read)
# strip whitespaces and newline characters from string and cast to float
self.ret_val = self.ret_val.decode().strip()
#print(self.ret_val)
if (self.ret_val != ''):
self.ret_val = float(self.ret_val)
# write value at the primary value key
self.prim_val_key = list(self._dict_dmm_measurement.keys())[0]
self._dict_dmm_measurement[self.prim_val_key] = self.ret_val
else:
# write value at the primary value key
self.prim_val_key = list(self._dict_dmm_measurement.keys())[0]
self._dict_dmm_measurement[self.prim_val_key] = 'NULL'
return self._dict_dmm_measurement