-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisa_device.py
More file actions
55 lines (44 loc) · 1.63 KB
/
Copy pathvisa_device.py
File metadata and controls
55 lines (44 loc) · 1.63 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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 30 02:02:40 2022
General Visa Device base class.
@author: Yonathan
"""
import time
import pyvisa as visa
PRINT = False
SLEEP_INTERVAL = 1
class VisaDevice:
def __init__(self, visa_address, timeout=1000, encoding='latin_1',
read_termination='\n', write_termination=None):
self.visa_address = visa_address
self.rm = visa.ResourceManager()
self.connect()
self.device.timeout = timeout # in ms
self.device.encoding = encoding
self.device.read_termination = read_termination
self.device.write_termination = write_termination
self.device.write('*cls') # clear ESR
def __del__(self):
self.device.close()
self.rm.close()
def connect(self):
self.device = self.rm.open_resource(self.visa_address)
self.connected = True
def reconnect_method(func):
def inner(self, *args, **kwargs):
while True:
try:
return func(self, *args, **kwargs)
except Exception as ex:
self.connected = False
if PRINT:
print(f"Caught Exception {ex}, reconnecting.")
while not self.connected:
try:
time.sleep(SLEEP_INTERVAL)
self.connect()
except Exception as ex2:
if PRINT:
print(f"Caught Exception {ex2} while reconnection, trying again.")
return inner