AxsunOCTControl_LW 1.6.0
A light-weight & cross-platform alternative to the .NET-based AxsunOCTControl.dll.
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
10
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):
36 # load the AxsunOCTControl_LW shared library into 'AOCLW'
37 # macOS: self.AOCLW = CDLL("/usr/local/lib/libAxsunOCTControl_LW.dylib")
38 self.AOCLW = CDLL("../x64/Release/AxsunOCTControl_LW.dll")
39 self.AOCLW.axCloseAxsunOCTControl()
40 retval = self.AOCLW.axOpenAxsunOCTControl(c_uint(interfaces))
41 if retval:
42 raise AxErr(self.GetAPIErrorString(retval))
43
44 def __del__(self):
45 self.AOCLW.axCloseAxsunOCTControl()
46
47 # get an error string from AxsunOCTControl_LW API
48 def GetAPIErrorString(self, errorno):
49 the_string = create_string_buffer(512)
50 self.AOCLW.axGetErrorExplained(errorno, the_string)
51 return ("*** AxErr " + str(errorno) + ": "
52 + str(the_string.value.decode()) + " ***")
53
54 # get the AxsunOCTControl_LW library version
55 def LibraryVersion(self):
56 # to be used as pointer args to AxsunOCTControl_LW calls:
57 major = c_uint(0)
58 minor = c_uint(0)
59 build = c_uint(0)
60 patch = c_uint(0)
61
62 # get the library version
63 retval = self.AOCLW.axLibraryVersion(byref(major), byref(minor),
64 byref(build), byref(patch))
65 if retval:
66 raise AxErr(self.GetAPIErrorString(retval))
67 return (str(major.value) + "." + str(minor.value) + "."
68 + str(build.value) + "." + str(patch.value))
69
70 # get device type
71 def GetDeviceType(self, which):
72 this_device = c_int(0)
73 retval = self.AOCLW.axDeviceType(byref(this_device), c_uint(which))
74 if retval:
75 raise AxErr(self.GetAPIErrorString(retval))
76 return device_t[this_device.value]
77
78 # get device serial number
79 def GetSerialNum(self, which):
80 serialnum = create_string_buffer(40)
81 retval = self.AOCLW.axSerialNumber(serialnum, c_uint(which))
82 if retval:
83 raise AxErr(self.GetAPIErrorString(retval))
84 return str(serialnum.value.decode())
85
86 # get device firmware version
87 def GetFirmwareVersion(self, which):
88 # to be used as pointer args to AxsunOCTControl_LW calls:
89 major = c_uint(0)
90 minor = c_uint(0)
91 patch = c_uint(0)
92
93 # get the library version
94 retval = self.AOCLW.axFirmwareVersion(byref(major), byref(minor),
95 byref(patch), c_uint(which))
96 if retval:
97 raise AxErr(self.GetAPIErrorString(retval))
98 return (str(major.value) + "."
99 + str(minor.value) + "."
100 + str(patch.value))
101
102 # get device connection type
103 def GetConnectionType(self, which):
104 this_device = c_int(0)
105 retval = self.AOCLW.axConnectionType(byref(this_device), c_uint(which))
106 if retval:
107 raise AxErr(self.GetAPIErrorString(retval))
108 return connection_t[this_device.value]
109
110 # count connected Axsun devices
111 def CountDevices(self):
112 return self.AOCLW.axCountConnectedDevices()
113
114 def RegisterCallback(self, func, obj):
115 # define the callback function C type
116 CALLBACK_TYPE = CFUNCTYPE(None, c_void_p)
117
118 # create C-callable instance of the python callback function
119 obj.callback_fn = CALLBACK_TYPE(func)
120
121 # 'load' axRegisterConnectCallback function
122 RegisterConnectCallback = self.AOCLW.axRegisterConnectCallback
123
124 # define its argument types and result type
125 RegisterConnectCallback.argtypes = [CALLBACK_TYPE, c_void_p]
126 RegisterConnectCallback.restype = c_int
127
128 # obj is the userdata argument, 'cast' to a c_void_p type
129 retval = RegisterConnectCallback(obj.callback_fn,
130 c_void_p.from_buffer(py_object(obj)))
131 if retval:
132 raise AxErr(self.GetAPIErrorString(retval))
133
134 def OpenUSBInterface(self, open):
135 retval = self.AOCLW.axUSBInterfaceOpen(c_uint(open))
136 if retval:
137 raise AxErr(self.GetAPIErrorString(retval))
138
139 def OpenNetworkInterface(self, open):
140 retval = self.AOCLW.axNetworkInterfaceOpen(c_uint(open))
141 if retval:
142 raise AxErr(self.GetAPIErrorString(retval))
143
144 def LaserEmission(self, state, which):
145 retval = self.AOCLW.axSetLaserEmission(c_uint(state), c_uint(which))
146 if retval:
147 raise AxErr(self.GetAPIErrorString(retval))
148
149 def GetFPGARegisterRange(self, start_reg, end_reg, which):
150 allocatedwords = end_reg - start_reg + 1
151 regvals_t = c_ushort * allocatedwords
152 regvals = regvals_t()
153 ReadRegs = self.AOCLW.axGetFPGARegisterRange
154 ReadRegs.argtypes = [c_uint, c_uint, POINTER(c_ushort), c_uint, c_uint]
155 ReadRegs.restype = c_int
156 retval = ReadRegs(start_reg,
157 end_reg,
158 regvals,
159 allocatedwords * 2,
160 which)
161 if retval:
162 raise AxErr(self.GetAPIErrorString(retval))
163
164 # create dictionary
165 this_reg = start_reg
166 regnums_vals = {}
167 for x in regvals:
168 regnums_vals[this_reg] = x
169 this_reg += 1
170
171 # return register num/value pairs
172 return regnums_vals
173
174 def SetFPGARegister(self, reg, value, which):
175 retval = self.AOCLW.axSetFPGARegister(c_uint(reg), c_ushort(value), c_uint(which))
176 if retval:
177 raise AxErr(self.GetAPIErrorString(retval))
178
179 def GetFPGARegister(self, reg, which):
180 regvalue = c_ushort(0)
181 retval = self.AOCLW.axGetFPGARegister(reg, byref(regvalue), c_uint(which))
182 if retval:
183 raise AxErr(self.GetAPIErrorString(retval))
184 return regvalue.value
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.
AxErr __cdecl axFirmwareVersion(uint32_t *major, uint32_t *minor, uint32_t *patch, uint32_t which_device)
Gets the device firmware version.