我在python中编写了一个与Avantes光谱仪一起使用的库,这是该库:
#as52.py
import ctypes
import time
lib = ctypes.WinDLL('AS5216.dll');
USER_ID_LEN = 64;
NR_WAVELEN_POL_COEF = 5;
NR_NONLIN_POL_COEF = 8;
NR_DEFECTIVE_PIXELS = 30;
MAX_NR_PIXELS = 4096;
NR_TEMP_POL_COEF = 5;
MAX_TEMP_SENSORS = 3;
ROOT_NAME_LEN = 6;
AVS_SERIAL_LEN = 10;
MAX_PIXEL_VALUE = 0xFFFC;
MAX_VIDEO_CHANNELS = 2;
MAX_LASER_WIDTH = 0xFFFF;
HW_TRIGGER_MODE = 1;
SW_TRIGGER_MODE = 0;
EDGE_TRIGGER_SOURCE = 0;
LEVEL_TRIGGER_SOURCE = 1;
MAX_TRIGGER_MODE = 1;
MAX_TRIGGER_SOURCE = 1;
MAX_TRIGGER_SOURCE_TYPE = 1;
MAX_INTEGRATION_TIME = 600000; # 600 seconds
SAT_DISABLE_DET = 0;
SAT_ENABLE_DET = 1;
SAT_PEAK_INVERSION = 2;
NR_DAC_POL_COEF = 2;
class AvsIdentityType(ctypes.Structure):
_pack_ = 1
_fields_ = [("SerialNumber", ctypes.c_char * AVS_SERIAL_LEN),
("UserFriendlyName", ctypes.c_char * USER_ID_LEN),
("Status", ctypes.c_ubyte)]
class ControlSettingsType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_StrobeControl", ctypes.c_ushort),
("m_LaserDelay", ctypes.c_uint),
("m_LaserWidth", ctypes.c_uint),
("m_LaserWaveLength", ctypes.c_float),
("m_StoreToRam", ctypes.c_ushort)]
class DarkCorrectionType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_Enable", ctypes.c_ubyte),
("m_ForgetPercentage", ctypes.c_ubyte)]
class DetectorType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_SensorType", ctypes.c_ubyte),
("m_NrPixels", ctypes.c_ushort),
("m_aFit", ctypes.c_float * NR_WAVELEN_POL_COEF),
("m_NLEnable", ctypes.c_bool),
("m_aNLCorrect", ctypes.c_double * NR_NONLIN_POL_COEF),
("m_aLowNLCounts", ctypes.c_double),
("m_aHighNLCounts", ctypes.c_double),
("m_Gain", ctypes.c_float * MAX_VIDEO_CHANNELS),
("m_Reserved", ctypes.c_float),
("m_Offset", ctypes.c_float * MAX_VIDEO_CHANNELS),
("m_ExtOffset", ctypes.c_float),
("m_DefectivePixels", ctypes.c_ushort * NR_DEFECTIVE_PIXELS)]
class SmoothingType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_SmoothPix", ctypes.c_ushort),
("m_SmoothModel", ctypes.c_ubyte)]
class SpectrumCalibrationType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_Smoothing", SmoothingType),
("m_CalInttime", ctypes.c_float),
("m_aCalibConvers", ctypes.c_float * MAX_NR_PIXELS)]
class IrradianceType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_IntensityCalib", SpectrumCalibrationType),
("m_CalibrationType", ctypes.c_ubyte),
("m_FiberDiameter", ctypes.c_uint)]
class TriggerType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_Mode", ctypes.c_ubyte),
("m_Source", ctypes.c_ubyte),
("m_SourceType", ctypes.c_ubyte)]
class MeasConfigType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_StartPixel", ctypes.c_ushort),
("m_StopPixel", ctypes.c_ushort),
("m_IntegrationTime", ctypes.c_float),
("m_IntegrationDelay", ctypes.c_uint),
("m_NrAverages", ctypes.c_uint),
("m_CorDynDark", DarkCorrectionType),
("m_Smoothing", SmoothingType),
("m_SaturationDetection", ctypes.c_ubyte),
("m_Trigger", TriggerType),
("m_Control", ControlSettingsType)]
class TimeStampType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_Date", ctypes.c_ushort),
("m_Time", ctypes.c_ushort)]
class SDCardType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_Enable", ctypes.c_bool),
("m_SpectrumType", ctypes.c_ubyte),
("m_aFileRootName", ctypes.c_byte * ROOT_NAME_LEN),
("m_TimeStamp", TimeStampType)]
class SpectrumCorrectionType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_aSpectrumCorrect", ctypes.c_float * MAX_NR_PIXELS)]
class StandAloneType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_Enable", ctypes.c_bool),
("m_Meas", MeasConfigType),
("m_Nmsr", ctypes.c_short),
("m_SDCard", SDCardType)]
class TempSensorType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_aFit", ctypes.c_float * NR_TEMP_POL_COEF)]
class TecControlType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_Enable", ctypes.c_bool),
("m_Setpoint", ctypes.c_float),
("m_aFit", ctypes.c_float * NR_DAC_POL_COEF)]
class ProcessControlType(ctypes.Structure):
_pack_ = 1
_fields_ = [("AnalogLow", ctypes.c_float * 2),
("AnalogHigh", ctypes.c_float * 2),
("DigitalLow", ctypes.c_float * 10),
("DigitalHigh", ctypes.c_float * 10)]
SETTINGS_RESERVED_LEN = ((62*1024) - ctypes.sizeof(ctypes.c_uint) -
(ctypes.sizeof(ctypes.c_ushort) + # m_Len
ctypes.sizeof(ctypes.c_ushort) + # m_ConfigVersion
USER_ID_LEN +
ctypes.sizeof(DetectorType) +
ctypes.sizeof(IrradianceType) +
ctypes.sizeof(SpectrumCalibrationType) +
ctypes.sizeof(SpectrumCorrectionType) +
ctypes.sizeof(StandAloneType) +
(ctypes.sizeof(TempSensorType)*MAX_TEMP_SENSORS) +
ctypes.sizeof(TecControlType) +
ctypes.sizeof(ProcessControlType)))
class DeviceConfigType(ctypes.Structure):
_pack_ = 1
_fields_ = [("m_Len", ctypes.c_ushort),
("m_ConfigVersion", ctypes.c_ushort),
("m_aUserFriendlyId", ctypes.c_byte * USER_ID_LEN),
("m_Detector", DetectorType),
("m_Irradiance", IrradianceType),
("m_Reflectance", SpectrumCalibrationType),
("m_SpectrumCorrect", SpectrumCorrectionType),
("m_StandAlone", StandAloneType),
("m_aTemperature", TempSensorType * MAX_TEMP_SENSORS),
("m_TecControl", TecControlType),
("m_ProcessControl", ProcessControlType),
("m_aReserved", ctypes.c_ubyte * SETTINGS_RESERVED_LEN)]
AVS_Init = lib['AVS_Init']
AVS_Init.restype = ctypes.c_int
AVS_GetNrOfDevices = lib['AVS_GetNrOfDevices']
AVS_GetNrOfDevices.restype = ctypes.c_int
AVS_GetList = lib['AVS_GetList']
AVS_GetList.restype = ctypes.c_int
AVS_GetList.argtypes = [ctypes.c_uint,ctypes.POINTER(ctypes.c_uint),ctypes.POINTER(AvsIdentityType)]
AVS_Activate = lib['AVS_Activate']
AVS_Activate.restype = ctypes.c_long
AVS_Activate.argtypes = [ctypes.POINTER(AvsIdentityType)]
AVS_GetNumPixels = lib['AVS_GetNumPixels']
AVS_GetNumPixels.restype = ctypes.c_int
AVS_GetNumPixels.argtypes = [ctypes.c_long, ctypes.POINTER(ctypes.c_ushort)]
AVS_UseHighResAdc = lib['AVS_UseHighResAdc']
AVS_UseHighResAdc.restype = ctypes.c_int
AVS_UseHighResAdc.argtypes = [ctypes.c_long, ctypes.c_bool]
AVS_GetLambda = lib['AVS_GetLambda']
AVS_GetLambda.restype = ctypes.c_int
AVS_GetLambda.argtypes = [ctypes.c_long, ctypes.POINTER(ctypes.c_double)]
AVS_PollScan = lib['AVS_PollScan']
AVS_PollScan.restype = ctypes.c_int
AVS_PollScan.argtype = ctypes.c_long
#l_PrepareMeasData = MeasConfigType()
#l_PrepareMeasData.m_StartPixel = 0
#l_PrepareMeasData.m_StopPixel = numpix.value - 1
#l_PrepareMeasData.m_IntegrationTime = 500
#l_PrepareMeasData.m_IntegrationDelay = 0;
#l_PrepareMeasData.m_NrAverages = 1;
#l_PrepareMeasData.m_CorDynDark.m_Enable = 0;
#l_PrepareMeasData.m_CorDynDark.m_ForgetPercentage = 100; #0 %
#l_PrepareMeasData.m_Smoothing.m_SmoothPix = 2;
#l_PrepareMeasData.m_Smoothing.m_SmoothModel = 0;
#l_PrepareMeasData.m_SaturationDetection = 1;
#l_PrepareMeasData.m_Trigger.m_Mode = 0;
#l_PrepareMeasData.m_Trigger.m_Source = 0;
#l_PrepareMeasData.m_Trigger.m_SourceType = 0;
#l_PrepareMeasData.m_Control.m_StrobeControl = 0;
#l_PrepareMeasData.m_Control.m_LaserDelay = 0;
#l_PrepareMeasData.m_Control.m_LaserWidth = 0;
#l_PrepareMeasData.m_Control.m_LaserWaveLength = 0;
#l_PrepareMeasData.m_Control.m_StoreToRam = 0;
#
AVS_PrepareMeasure = lib['AVS_PrepareMeasure']
AVS_PrepareMeasure.restype = ctypes.c_int
AVS_PrepareMeasure.argtypes = [ctypes.c_long,ctypes.POINTER(MeasConfigType)]
AVS_Measure = lib['AVS_Measure']
AVS_Measure.restype = ctypes.c_int
AVS_Measure.argtypes = [ctypes.c_long, ctypes.c_long, ctypes.c_short]
AVS_GetScopeData = lib['AVS_GetScopeData']
AVS_GetScopeData.restype = ctypes.c_int
AVS_GetScopeData.argtypes = [ctypes.c_long,ctypes.POINTER(ctypes.c_uint),ctypes.POINTER(ctypes.c_double)]
AVS_GetSaturatedPixels = lib['AVS_GetSaturatedPixels']
AVS_GetSaturatedPixels.restype = ctypes.c_int
AVS_GetSaturatedPixels.argtypes = [ctypes.c_long,ctypes.POINTER(ctypes.c_ubyte)]
AVS_StopMeasure = lib['AVS_StopMeasure']
AVS_StopMeasure.restype = ctypes.c_int
AVS_StopMeasure.argtypes = [ctypes.c_long]
AVS_Deactivate = lib['AVS_Deactivate']
AVS_Deactivate.restype = ctypes.c_bool
AVS_Deactivate.argtypes = [ctypes.c_long]
现在,如果我运行类似命令:
from as52 import *
import numpy as np
np.log10(np.arange(1,100))
一切都很好,但现在如果我尝试:
np.log10(np.arange(0,100))
Python内核死亡 也类似:
np.log10(0)
导致崩溃(“似乎内核意外死亡。使用'Restart kernel'继续使用此控制台”)。
我认为这与python中的inf
有关,但我不知道如何解决。
如果您想尝试一下,这里有一个链接
AS5216.dll。
我在Windows 10上使用spyder 2.3.5(32bit)的python(x,y)2.7.10。
EDIT1: 最小的崩溃再现器:
import ctypes
lib = ctypes.WinDLL('AS5216.dll');
import numpy as np
print np.log10(0)