-
Notifications
You must be signed in to change notification settings - Fork 0
/
WF_Device.py
51 lines (44 loc) · 1.67 KB
/
WF_Device.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
from ctypes import *
import sys
import logging
class WF_Device:
""" A wrapper for any waveform supported device.
Do not forget to call connect()
"""
def __init__(self):
try:
if sys.platform.startswith("win"):
self._dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
self._dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
self._dwf = cdll.LoadLibrary("libdwf.so")
except OSError:
logging.fatal("Could not load library, is WaveForms installed?")
raise
self._hdwf = c_int(0)
def connect(self, device_index=-1):
"""Attempts to connect to a waveform device.
Specifying the device index allows to connect to a specific one;
otherwise the first one that is detected is used.
"""
logging.debug("Aquiring device...")
self._dwf.FDwfDeviceOpen(c_int(device_index), byref(self._hdwf))
self.check_for_error()
def check_for_error(self):
error_code = c_int()
self._dwf.FDwfGetLastError(byref(error_code))
if error_code.value != 0:
szerr = create_string_buffer(512)
self._dwf.FDwfGetLastErrorMsg(szerr)
logging.error(str(szerr.value))
raise ConnectionError()
def reset(self):
if self._hdwf.value == 0:
logging.error("No device connected to reset")
return
self._dwf.FDwfDeviceReset(self._hdwf)
def close(self):
self._dwf.FDwfDeviceCloseAll()
def __del__(self):
self.close()