AxsunOCTControl_LW 2.0.0
A light-weight & cross-platform alternative to the .NET-based AxsunOCTControl.dll.
Loading...
Searching...
No Matches
aoclw_wrapper.py
1
3
4
5# This Python file uses the following encoding: utf-8
6
7from ctypes import CDLL, create_string_buffer, c_uint, c_int, \
8 byref, py_object, CFUNCTYPE, c_void_p, \
9 POINTER, c_ushort, c_char_p
10import platform, os
11
12# derived exception class to be caught and handled in calling application
13class AxErr(Exception):
14 pass
15
16
17# types of devices
18device_t = {0: "unknown",
19 40: "laser ",
20 41: "CLDAQ ",
21 42: "EDAQ "
22 }
23
24# types of connection interfaces
25connection_t = {0: "none",
26 1: "USB",
27 2: "RS232_PASSTHRU",
28 3: "RS232",
29 4: "ETHERNET"
30 }
31
32
33class AOCLW_wrapper_class:
34
35 def __init__(self, interfaces = 0):
36 # load the AxsunOCTControl_LW shared library into 'AOCLW' (adjust path to dll as needed)
37 if platform.system() == "Windows":
38 self.AOCLW = CDLL("../x64/Release/AxsunOCTControl_LW.dll")
39 if platform.system() == "Darwin" or platform.system() == "Linux":
40 lib_path = os.path.join(os.getcwd(), "build/lib/libaxsunoctcontrol_lw.dylib")
41 self.AOCLW = CDLL(lib_path)
42 retval = self.AOCLW.axOpenAxsunOCTControl(c_uint(interfaces))
43 if retval:
44 raise AxErr(self.GetAPIErrorString(retval))
45
46 def __del__(self):
47 self.AOCLW.axCloseAxsunOCTControl()
48
49 # get an error string from AxsunOCTControl_LW API
50 def GetAPIErrorString(self, errorno):
51 the_string = create_string_buffer(512)
52 self.AOCLW.axGetErrorExplained(errorno, the_string)
53 return ("*** AxErr " + str(errorno) + ": "
54 + the_string.value.decode() + " ***")
55
56 # get the AxsunOCTControl_LW library version (numeric tuple version)
57 def LibraryVersion(self):
58 # to be used as pointer args to AxsunOCTControl_LW calls:
59 major = c_uint(0)
60 minor = c_uint(0)
61 build = c_uint(0)
62 patch = c_uint(0)
63 self.AOCLW.axLibraryVersion(byref(major), byref(minor),
64 byref(build), byref(patch))
65 return (major.value, minor.value, build.value, patch.value)
66
67 # get the AxsunOCTControl_LW library version (string version)
68 def LWLibVersion(self):
69 ReadStr = self.AOCLW.axLWLibVersion
70 ReadStr.restype = c_char_p
71 return str(ReadStr(), 'UTF-8')
72
73 # get the AxsunOCTControl_LW library build date and time (string version)
74 def axLWBuildDateTime(self):
75 ReadStr = self.AOCLW.axLWBuildDateTime
76 ReadStr.restype = c_char_p
77 return ReadStr().decode()
78
79 # get device type
80 def GetDeviceType(self, which_dev):
81 this_device = c_int(0)
82 retval = self.AOCLW.axDeviceType(byref(this_device), c_uint(which_dev))
83 if retval:
84 raise AxErr(self.GetAPIErrorString(retval))
85 return device_t[this_device.value]
86
87 # get device serial number
88 def GetSerialNum(self, which_dev):
89 serialnum = create_string_buffer(40)
90 retval = self.AOCLW.axSerialNumber(serialnum, c_uint(which_dev))
91 if retval:
92 raise AxErr(self.GetAPIErrorString(retval))
93 return serialnum.value.decode()
94
95 # get device firmware version
96 def GetFirmwareVersion(self, which_dev):
97 # to be used as pointer args to AxsunOCTControl_LW calls:
98 major = c_uint(0)
99 minor = c_uint(0)
100 patch = c_uint(0)
101
102 # get the library version
103 retval = self.AOCLW.axFirmwareVersion(byref(major), byref(minor),
104 byref(patch), c_uint(which_dev))
105 if retval:
106 raise AxErr(self.GetAPIErrorString(retval))
107 return (str(major.value) + "."
108 + str(minor.value) + "."
109 + str(patch.value))
110
111 # get device connection type
112 def GetConnectionType(self, which_dev):
113 this_device = c_int(0)
114 retval = self.AOCLW.axConnectionType(byref(this_device), c_uint(which_dev))
115 if retval:
116 raise AxErr(self.GetAPIErrorString(retval))
117 return connection_t[this_device.value]
118
119 # count connected Axsun devices
120 def CountDevices(self):
121 return self.AOCLW.axCountConnectedDevices()
122
123 def RegisterCallback(self, func, obj):
124 # define the callback function C type
125 CALLBACK_TYPE = CFUNCTYPE(None, c_void_p)
126
127 # create C-callable instance of the python callback function
128 obj.callback_fn = CALLBACK_TYPE(func)
129
130 # 'load' axRegisterConnectCallback function
131 RegisterConnectCallback = self.AOCLW.axRegisterConnectCallback
132
133 # define its argument types and result type
134 RegisterConnectCallback.argtypes = [CALLBACK_TYPE, c_void_p]
135 RegisterConnectCallback.restype = c_int
136
137 # obj is the userdata argument, 'cast' to a c_void_p type
138 retval = RegisterConnectCallback(obj.callback_fn, c_void_p.from_buffer(py_object(obj)))
139 if retval:
140 raise AxErr(self.GetAPIErrorString(retval))
141
142 def OpenUSBInterface(self, open):
143 retval = self.AOCLW.axUSBInterfaceOpen(c_uint(open))
144 if retval:
145 raise AxErr(self.GetAPIErrorString(retval))
146
147 def OpenNetworkInterface(self, open):
148 retval = self.AOCLW.axNetworkInterfaceOpen(c_uint(open))
149 if retval:
150 raise AxErr(self.GetAPIErrorString(retval))
151
152 def LaserEmission(self, state, which_laser = 0):
153 retval = self.AOCLW.axSetLaserEmission(c_uint(state), c_uint(which_laser))
154 if retval:
155 raise AxErr(self.GetAPIErrorString(retval))
156
157 def GetFPGARegisterRange(self, start_reg, end_reg, which_daq = 0):
158 allocatedwords = end_reg - start_reg + 1
159 regvals_t = c_ushort * allocatedwords
160 regvals = regvals_t()
161 ReadRegs = self.AOCLW.axGetFPGARegisterRange
162 ReadRegs.argtypes = [c_uint, c_uint, POINTER(c_ushort), c_uint, c_uint]
163 ReadRegs.restype = c_int
164 retval = ReadRegs(start_reg,
165 end_reg,
166 regvals,
167 allocatedwords * 2,
168 which_daq)
169 if retval:
170 raise AxErr(self.GetAPIErrorString(retval))
171
172 # create dictionary
173 this_reg = start_reg
174 regnums_vals = {}
175 for x in regvals:
176 regnums_vals[this_reg] = x
177 this_reg += 1
178
179 # return register num/value pairs
180 return regnums_vals
181
182 def SetFPGARegister(self, reg, value, which_daq = 0):
183 retval = self.AOCLW.axSetFPGARegister(c_uint(reg), c_ushort(value), c_uint(which_daq))
184 if retval:
185 raise AxErr(self.GetAPIErrorString(retval))
186
187 def GetFPGARegister(self, reg, which_daq = 0):
188 regvalue = c_ushort(0)
189 retval = self.AOCLW.axGetFPGARegister(reg, byref(regvalue), c_uint(which_daq))
190 if retval:
191 raise AxErr(self.GetAPIErrorString(retval))
192 return regvalue.value
const char * axLWBuildDateTime()
Get a string describing the time and date that the AxsunOCTControl_LW library was built.
AxErr __cdecl axGetFPGARegister(const uint32_t regnum, uint16_t *regval, uint32_t which_DAQ)
Gets (i.e. reads) the current value in a FPGA register.
AxErr __cdecl axSetFPGARegister(uint32_t regnum, uint16_t regval, uint32_t which_DAQ)
Sets (i.e. writes) a FPGA register with a single value.
AxErr __cdecl axOpenAxsunOCTControl(uint32_t open_all_interfaces)
Opens the AxsunOCTControl_LW session.
AxErr __cdecl axDeviceType(AxDevType *device_type, uint32_t which_device)
Gets the device type (e.g. Laser, EDAQ).
uint32_t __cdecl axCountConnectedDevices()
Counts the number of devices successfully connected and enumerated by AxsunOCTControl_LW.
void __cdecl axGetErrorExplained(AxErr errornum, char *error_string)
Gets a string which explains an error code in a more verbose fashion.
AxErr __cdecl axNetworkInterfaceOpen(uint32_t interface_status)
Opens, resets, or closes the Ethernet network interface.
AxErr __cdecl axCloseAxsunOCTControl()
Closes an AxsunOCTControl_LW session previously opened with axOpenAxsunOCTControl().
AxErr __cdecl axSerialNumber(char *serial_number, uint32_t which_device)
Gets the device serial number string.
AxErr __cdecl axSetLaserEmission(uint32_t emission_state, uint32_t which_laser)
Enables or disables swept laser emission.
AxErr __cdecl axUSBInterfaceOpen(uint32_t interface_status)
Opens, resets, or closes the USB interface.
AxErr __cdecl axConnectionType(AxConnectionType *connection_type, uint32_t which_device)
Gets the device connection interface (e.g. USB, Ethernet, RS-232).
AxErr __cdecl axLibraryVersion(uint32_t *major, uint32_t *minor, uint32_t *patch, uint32_t *build)
Gets the AxsunOCTControl_LW library version as numeric elements.
AxErr __cdecl axFirmwareVersion(uint32_t *major, uint32_t *minor, uint32_t *patch, uint32_t which_device)
Gets the device firmware version.