spcm_core.pyspcm

  1import os
  2import platform
  3import sys
  4from ctypes import *
  5from enum import Enum
  6
  7# load registers for easier access
  8from .regs import *
  9
 10# load registers for easier access
 11from .spcerr import *
 12
 13class Direction(Enum):
 14    Undefined = 0
 15    Acquisition = 1
 16    Generation = 2
 17
 18SPCM_DIR_PCTOCARD = 0
 19SPCM_DIR_CARDTOPC = 1
 20SPCM_DIR_CARDTOGPU = 2
 21SPCM_DIR_GPUTOCARD = 3
 22
 23SPCM_BUF_DATA      = 1000 # main data buffer for acquired or generated samples
 24SPCM_BUF_ABA       = 2000 # buffer for ABA data, holds the A-DATA (slow samples)
 25SPCM_BUF_TIMESTAMP = 3000 # buffer for timestamps
 26
 27TYPE_INT64 = 0
 28TYPE_DOUBLE = 1
 29
 30# determine bit width of os
 31oPlatform = platform.architecture()
 32if (oPlatform[0] == '64bit'):
 33    bIs64Bit = True
 34else:
 35    bIs64Bit = False
 36
 37# define pointer aliases
 38int8  = c_int8
 39int16 = c_int16
 40int32 = c_int32
 41int64 = c_int64
 42
 43ptr8  = POINTER (int8)
 44ptr16 = POINTER (int16)
 45ptr32 = POINTER (int32)
 46ptr64 = POINTER (int64)
 47
 48uint8  = c_uint8
 49uint16 = c_uint16
 50uint32 = c_uint32
 51uint64 = c_uint64
 52
 53uptr8  = POINTER (uint8)
 54uptr16 = POINTER (uint16)
 55uptr32 = POINTER (uint32)
 56uptr64 = POINTER (uint64)
 57
 58double = c_double
 59dptr64 = POINTER (double)
 60
 61class _U(Union):
 62    """Union for doubles and 64-bit integers"""
 63    _fields_ = [
 64        ("dValue", double),
 65        ("llValue", int64)
 66    ]
 67
 68class ST_LIST_PARAM(Structure):
 69    """Structure for lists of parameters"""
 70    _anonymous_ = ("Value",)
 71    _fields_ = [
 72        ("lReg", int32), # the register
 73        ("lType", int32), # the type of value written
 74        ("Value", _U), # the actual value
 75    ]
 76
 77###############################################################################
 78# open library
 79###############################################################################
 80# Check for driver versions
 81try:
 82    # Windows
 83    if os.name == 'nt':
 84        # use windll because all driver access functions use _stdcall calling convention under windows
 85        if (bIs64Bit):
 86            spcmDll = windll.LoadLibrary ("spcm_win64.dll")
 87        else:
 88            spcmDll = windll.LoadLibrary ("spcm_win32.dll")
 89
 90        bIsLinux = False
 91
 92    # Linux
 93    elif os.name == 'posix':
 94        # sys.stdout.write("Python Version: {0} on Linux\n\n".format (platform.python_version()))
 95
 96        # use cdll because all driver access functions use cdecl calling convention under linux
 97        spcmDll = cdll.LoadLibrary ("libspcm_linux.so")
 98
 99        bIsLinux = True
100
101    else:
102        raise Exception('Operating system not supported by pySpcm')
103
104
105    ###############################################################################
106    # read functions from library
107    ###############################################################################
108
109    # define card handle type
110    if (bIs64Bit or bIsLinux):
111        # for unknown reasons c_void_p gets messed up on Win7/64bit, but this works:
112        drv_handle = POINTER(c_uint64)
113    else:
114        drv_handle = c_void_p
115
116    # load spcm_hOpen
117    if (bIs64Bit or bIsLinux):
118        spcm_hOpen = getattr(spcmDll, "spcm_hOpen")
119    else:
120        spcm_hOpen = getattr(spcmDll, "_spcm_hOpen@4")
121    spcm_hOpen.argtype = [c_char_p]
122    spcm_hOpen.restype = drv_handle
123
124    # load spcm_vClose
125    if (bIs64Bit or bIsLinux):
126        spcm_vClose = getattr(spcmDll, "spcm_vClose")
127    else:
128        spcm_vClose = getattr(spcmDll, "_spcm_vClose@4")
129    spcm_vClose.argtype = [drv_handle]
130    spcm_vClose.restype = None
131
132    # load spcm_dwGetErrorInfo_i32
133    if (bIs64Bit or bIsLinux):
134        spcm_dwGetErrorInfo_i32 = getattr(spcmDll, "spcm_dwGetErrorInfo_i32")
135    else:
136        spcm_dwGetErrorInfo_i32 = getattr(spcmDll, "_spcm_dwGetErrorInfo_i32@16")
137    spcm_dwGetErrorInfo_i32.argtype = [drv_handle, uptr32, ptr32, c_char_p]
138    spcm_dwGetErrorInfo_i32.restype = uint32
139
140    # load spcm_dwGetErrorInfo_i64
141    if (bIs64Bit or bIsLinux):
142        spcm_dwGetErrorInfo_i64 = getattr(spcmDll, "spcm_dwGetErrorInfo_i64")
143    else:
144        spcm_dwGetErrorInfo_i64 = getattr(spcmDll, "_spcm_dwGetErrorInfo_i64@16")
145    spcm_dwGetErrorInfo_i64.argtype = [drv_handle, uptr32, ptr64, c_char_p]
146    spcm_dwGetErrorInfo_i64.restype = uint32
147
148    # load spcm_dwGetErrorInfo_d64
149    if (bIs64Bit or bIsLinux):
150        spcm_dwGetErrorInfo_d64 = getattr(spcmDll, "spcm_dwGetErrorInfo_d64")
151    else:
152        spcm_dwGetErrorInfo_d64 = getattr(spcmDll, "_spcm_dwGetErrorInfo_d64@16")
153    spcm_dwGetErrorInfo_d64.argtype = [drv_handle, uptr32, dptr64, c_char_p]
154    spcm_dwGetErrorInfo_d64.restype = uint32
155
156    # load spcm_dwGetParam_i32
157    if (bIs64Bit or bIsLinux):
158        spcm_dwGetParam_i32 = getattr(spcmDll, "spcm_dwGetParam_i32")
159    else:
160        spcm_dwGetParam_i32 = getattr(spcmDll, "_spcm_dwGetParam_i32@12")
161    spcm_dwGetParam_i32.argtype = [drv_handle, int32, ptr32]
162    spcm_dwGetParam_i32.restype = uint32
163
164    # load spcm_dwGetParam_i64
165    if (bIs64Bit or bIsLinux):
166        spcm_dwGetParam_i64 = getattr(spcmDll, "spcm_dwGetParam_i64")
167    else:
168        spcm_dwGetParam_i64 = getattr(spcmDll, "_spcm_dwGetParam_i64@12")
169    spcm_dwGetParam_i64.argtype = [drv_handle, int32, ptr64]
170    spcm_dwGetParam_i64.restype = uint32
171
172    # load spcm_dwGetParam_d64
173    if (bIs64Bit or bIsLinux):
174        spcm_dwGetParam_d64 = getattr(spcmDll, "spcm_dwGetParam_d64")
175    else:
176        spcm_dwGetParam_d64 = getattr(spcmDll, "_spcm_dwGetParam_d64@12")
177    spcm_dwGetParam_d64.argtype = [drv_handle, int32, dptr64]
178    spcm_dwGetParam_d64.restype = uint32
179
180    # load spcm_dwGetParam_ptr
181    if (bIs64Bit or bIsLinux):
182        spcm_dwGetParam_ptr = getattr(spcmDll, "spcm_dwGetParam_ptr")
183    else:
184        spcm_dwGetParam_ptr = getattr(spcmDll, "_spcm_dwGetParam_ptr@20")
185    spcm_dwGetParam_ptr.argtype = [drv_handle, int32, c_void_p, uint64]
186    spcm_dwGetParam_ptr.restype = uint32
187
188    # load spcm_dwSetParam_i32
189    if (bIs64Bit or bIsLinux):
190        spcm_dwSetParam_i32 = getattr(spcmDll, "spcm_dwSetParam_i32")
191    else:
192        spcm_dwSetParam_i32 = getattr(spcmDll, "_spcm_dwSetParam_i32@12")
193    spcm_dwSetParam_i32.argtype = [drv_handle, int32, int32]
194    spcm_dwSetParam_i32.restype = uint32
195
196    # load spcm_dwSetParam_i64
197    if (bIs64Bit or bIsLinux):
198        spcm_dwSetParam_i64_ = getattr(spcmDll, "spcm_dwSetParam_i64")
199    else:
200        spcm_dwSetParam_i64_ = getattr(spcmDll, "_spcm_dwSetParam_i64@16")
201    spcm_dwSetParam_i64_.argtype = [drv_handle, int32, int64]
202    spcm_dwSetParam_i64_.restype = uint32
203
204    # load spcm_dwSetParam_d64
205    if (bIs64Bit or bIsLinux):
206        spcm_dwSetParam_d64_ = getattr(spcmDll, "spcm_dwSetParam_d64")
207    else:
208        spcm_dwSetParam_d64_ = getattr(spcmDll, "_spcm_dwSetParam_d64@16")
209    spcm_dwSetParam_d64_.argtype = [drv_handle, int32, double]
210    spcm_dwSetParam_d64_.restype = uint32
211
212    # load spcm_dwSetParam_ptr
213    if (bIs64Bit or bIsLinux):
214        spcm_dwSetParam_ptr = getattr(spcmDll, "spcm_dwSetParam_ptr")
215    else:
216        spcm_dwSetParam_ptr = getattr(spcmDll, "_spcm_dwSetParam_ptr@20")
217    spcm_dwSetParam_ptr.argtype = [drv_handle, int32, c_void_p, uint64]
218    spcm_dwSetParam_ptr.restype = uint32
219
220    # load spcm_dwSetParam_i64m
221    if (bIs64Bit or bIsLinux):
222        spcm_dwSetParam_i64m = getattr(spcmDll, "spcm_dwSetParam_i64m")
223    else:
224        spcm_dwSetParam_i64m = getattr(spcmDll, "_spcm_dwSetParam_i64m@16")
225    spcm_dwSetParam_i64m.argtype = [drv_handle, int32, int32, int32]
226    spcm_dwSetParam_i64m.restype = uint32
227
228    # load spcm_dwDefTransfer_i64
229    if (bIs64Bit or bIsLinux):
230        spcm_dwDefTransfer_i64_ = getattr(spcmDll, "spcm_dwDefTransfer_i64")
231    else:
232        spcm_dwDefTransfer_i64_ = getattr(spcmDll, "_spcm_dwDefTransfer_i64@36")
233    spcm_dwDefTransfer_i64_.argtype = [drv_handle, uint32, uint32, uint32, c_void_p, uint64, uint64]
234    spcm_dwDefTransfer_i64_.restype = uint32
235
236    # load spcm_dwInvalidateBuf
237    if (bIs64Bit or bIsLinux):
238        spcm_dwInvalidateBuf = getattr(spcmDll, "spcm_dwInvalidateBuf")
239    else:
240        spcm_dwInvalidateBuf = getattr(spcmDll, "_spcm_dwInvalidateBuf@8")
241    spcm_dwInvalidateBuf.argtype = [drv_handle, uint32]
242    spcm_dwInvalidateBuf.restype = uint32
243
244    # load spcm_dwGetContBuf_i64
245    if (bIs64Bit or bIsLinux):
246        spcm_dwGetContBuf_i64 = getattr(spcmDll, "spcm_dwGetContBuf_i64")
247    else:
248        spcm_dwGetContBuf_i64 = getattr(spcmDll, "_spcm_dwGetContBuf_i64@16")
249    spcm_dwGetContBuf_i64.argtype = [drv_handle, uint32, POINTER(c_void_p), uptr64]
250    spcm_dwGetContBuf_i64.restype = uint32
251
252    # load spcm_dwDiscovery
253    if (bIs64Bit or bIsLinux):
254        spcm_dwDiscovery = getattr(spcmDll, "spcm_dwDiscovery")
255    else:
256        spcm_dwDiscovery = getattr(spcmDll, "_spcm_dwDiscovery@16")
257    spcm_dwDiscovery.argtype = [POINTER(c_char_p), uint32, uint32, uint32]
258    spcm_dwDiscovery.restype = uint32
259
260    # load spcm_dwSendIDNRequest
261    if (bIs64Bit or bIsLinux):
262        spcm_dwSendIDNRequest = getattr(spcmDll, "spcm_dwSendIDNRequest")
263    else:
264        spcm_dwSendIDNRequest = getattr(spcmDll, "_spcm_dwSendIDNRequest@12")
265    spcm_dwSendIDNRequest.argtype = [POINTER(c_char_p), uint32, uint32]
266    spcm_dwSendIDNRequest.restype = uint32
267
268
269except OSError as e:
270    raise Exception("The Spectrum Instrumentation device driver is not found. Please install the driver and try again.\nFor the newest drivers, see https://spectrum-instrumentation.com/support/downloads.php")
271
272except AttributeError as e:
273    minimum_driver_version = "7.0"
274    raise Exception("Driver version not supported. Minimum version required: {}.\n For the newest drivers, see https://spectrum-instrumentation.com/support/downloads.php".format(minimum_driver_version))
275
276def spcm_dwSetParam_i64(hDrv, lReg, Val):
277    try:
278        llVal = int64(Val.value)
279    except AttributeError:
280        llVal = int64(Val)
281    return spcm_dwSetParam_i64_ (hDrv, lReg, llVal)
282
283def spcm_dwSetParam_d64(hDrv, lReg, Val):
284    try:
285        dVal = double(Val.value)
286    except AttributeError:
287        dVal = double(Val)
288    return spcm_dwSetParam_d64_ (hDrv, lReg, dVal)
289
290
291def spcm_dwDefTransfer_i64(hDrv, dwBufferType, dwDirection, notify, pvBuffer, offs, buf_len):
292    try:
293        dwNotify = uint64(notify.value)
294    except AttributeError:
295        dwNotify = uint64(notify)
296    try:
297        qwOffs = uint64(offs.value)
298    except AttributeError:
299        qwOffs = uint64(offs)
300    try:
301        qwBufLen = uint64(buf_len.value)
302    except AttributeError:
303        qwBufLen = uint64(buf_len)
304    return spcm_dwDefTransfer_i64_ (hDrv, dwBufferType, dwDirection, dwNotify, pvBuffer, qwOffs, qwBufLen)
class Direction(enum.Enum):
14class Direction(Enum):
15    Undefined = 0
16    Acquisition = 1
17    Generation = 2

An enumeration.

Undefined = <Direction.Undefined: 0>
Acquisition = <Direction.Acquisition: 1>
Generation = <Direction.Generation: 2>
SPCM_DIR_PCTOCARD = 0
SPCM_DIR_CARDTOPC = 1
SPCM_DIR_CARDTOGPU = 2
SPCM_DIR_GPUTOCARD = 3
SPCM_BUF_DATA = 1000
SPCM_BUF_ABA = 2000
SPCM_BUF_TIMESTAMP = 3000
TYPE_INT64 = 0
TYPE_DOUBLE = 1
oPlatform = ('64bit', 'ELF')
int8 = <class 'ctypes.c_byte'>
int16 = <class 'ctypes.c_short'>
int32 = <class 'ctypes.c_int'>
int64 = <class 'ctypes.c_long'>
ptr8 = <class 'spcm_core.pyspcm.LP_c_byte'>
ptr16 = <class 'spcm_core.pyspcm.LP_c_short'>
ptr32 = <class 'spcm_core.pyspcm.LP_c_int'>
ptr64 = <class 'spcm_core.pyspcm.LP_c_long'>
uint8 = <class 'ctypes.c_ubyte'>
uint16 = <class 'ctypes.c_ushort'>
uint32 = <class 'ctypes.c_uint'>
uint64 = <class 'ctypes.c_ulong'>
uptr8 = <class 'spcm_core.pyspcm.LP_c_ubyte'>
uptr16 = <class 'spcm_core.pyspcm.LP_c_ushort'>
uptr32 = <class 'spcm_core.pyspcm.LP_c_uint'>
uptr64 = <class 'spcm_core.pyspcm.LP_c_ulong'>
double = <class 'ctypes.c_double'>
dptr64 = <class 'spcm_core.pyspcm.LP_c_double'>
class ST_LIST_PARAM(_ctypes.Structure):
69class ST_LIST_PARAM(Structure):
70    """Structure for lists of parameters"""
71    _anonymous_ = ("Value",)
72    _fields_ = [
73        ("lReg", int32), # the register
74        ("lType", int32), # the type of value written
75        ("Value", _U), # the actual value
76    ]

Structure for lists of parameters

lReg

Structure/Union member

lType

Structure/Union member

Value

Structure/Union member

dValue

Structure/Union member

llValue

Structure/Union member

def spcm_dwSetParam_i64(hDrv, lReg, Val):
277def spcm_dwSetParam_i64(hDrv, lReg, Val):
278    try:
279        llVal = int64(Val.value)
280    except AttributeError:
281        llVal = int64(Val)
282    return spcm_dwSetParam_i64_ (hDrv, lReg, llVal)
def spcm_dwSetParam_d64(hDrv, lReg, Val):
284def spcm_dwSetParam_d64(hDrv, lReg, Val):
285    try:
286        dVal = double(Val.value)
287    except AttributeError:
288        dVal = double(Val)
289    return spcm_dwSetParam_d64_ (hDrv, lReg, dVal)
def spcm_dwDefTransfer_i64(hDrv, dwBufferType, dwDirection, notify, pvBuffer, offs, buf_len):
292def spcm_dwDefTransfer_i64(hDrv, dwBufferType, dwDirection, notify, pvBuffer, offs, buf_len):
293    try:
294        dwNotify = uint64(notify.value)
295    except AttributeError:
296        dwNotify = uint64(notify)
297    try:
298        qwOffs = uint64(offs.value)
299    except AttributeError:
300        qwOffs = uint64(offs)
301    try:
302        qwBufLen = uint64(buf_len.value)
303    except AttributeError:
304        qwBufLen = uint64(buf_len)
305    return spcm_dwDefTransfer_i64_ (hDrv, dwBufferType, dwDirection, dwNotify, pvBuffer, qwOffs, qwBufLen)
drv_handle = <class 'spcm_core.pyspcm.LP_c_ulong'>