The brand-new PN400 Phase Noise Test System is available for purchase! →

Error when getting sweep results with API

Forums › SA Series Discussions › Error when getting sweep results with API

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #111102
    akarthikeyan
    Participant

    Hi,

    I am trying to do a sweep using sa_api.dll and python & ctypes and I am getting an error and python is getting killed when I call ‘saGetSweep’ function. Following is my program and error.

    +++++++
    Snippet
    +++++++
    import os
    import sys
    import ctypes as ct

    class SA124B():
    def __init__(self):
    self.dllpath = os.path.join(os.getcwd(), ‘x64/sa_api.dll’)
    self.sa = 0
    self.deviceHandle = 0

    # Initialize
    self.sa = ct.CDLL(self.dllpath)
    print “Successfuly Initialized”

    # Open Device
    def openDevice(self):
    self.deviceHandle = ct.c_int(0)
    deviceHandlePnt = ct.pointer(self.deviceHandle)
    res = self.sa.saOpenDevice(deviceHandlePnt)
    if res == 0:
    print ‘Opened Device Successfully’
    else:
    print “Opening Device Failed”
    sys.exit(0)

    # Get Serial Number
    def getSerialNumber(self):
    serialNo = ct.c_uint(0)
    serialNoPnt = ct.pointer(serialNo)
    self.sa.saGetSerialNumber(self.deviceHandle, serialNoPnt)
    print ‘Serial Number: %s’ % str(serialNo.value)

    # Get Firmware Vesion
    def getFirmwareVersion(self):
    firmwareRev = ct.c_uint(0)
    firmwareRevPnt = ct.pointer(firmwareRev)
    self.sa.saGetFirmwareString(self.deviceHandle, firmwareRevPnt)
    print ‘Firmware Revision: %s’ % str(firmwareRev.value)

    # Get Device Type
    def getDeviceType(self):
    devType = ct.c_uint(0)
    devTypePnt = ct.pointer(devType)
    self.sa.saGetDeviceType(self.deviceHandle, devTypePnt)
    print ‘Device Type: %s’ % str(devType.value)

    # Get API Version
    def getAPIVersion(self):
    self.sa.saGetAPIVersion.restype = ct.c_char_p # Tell ctypes this function returns a pointer to a string
    apiRevStr = self.sa.saGetAPIVersion(self.deviceHandle)
    ret = ct.c_char_p(apiRevStr).value # Dereference pointer, extract string
    print ‘API Version: %s’ % str(ret)

    # Configure Center Span
    def cfgCeterSpan(self):
    center = ct.c_double(915.0e6)
    span = ct.c_double(1.0e6)
    res = self.sa.saConfigCenterSpan(self.deviceHandle, center, span)
    if res == 0:
    print ‘Configured Center Span’
    else:
    print ‘Configuring Center Span Failed: %s’ % str(res)
    self.closeDevice()
    sys.exit(0)

    # Configure Acquisition
    def cfgAcquisition(self):
    detector = ct.c_uint(0)
    scale = ct.c_uint(0)
    res = self.sa.saConfigAcquisition(self.deviceHandle, detector, scale)
    if res == 0:
    print ‘Configured Acquisition’
    else:
    print ‘Configuring Acquisition Failed: %s’ % str(res)
    self.closeDevice()
    sys.exit(0)

    # Configure Level
    def cfgLevel(self):
    ref = ct.c_double(-10.0)
    atten = ct.c_double(-1)
    res = self.sa.saConfigLevel(self.deviceHandle, ref, atten)
    if res == 0:
    print ‘Configured Level’
    else:
    print ‘Configuring Level Failed: %s’ % str(res)
    self.closeDevice()
    sys.exit(0)

    # Configure Sweep Coupling
    def cfgSweepCoupling(self):
    rbw = ct.c_double(1.0e3)
    vbw = ct.c_double(1.0e3)
    rejection = ct.c_bool(True)
    res = self.sa.saConfigSweepCoupling(self.deviceHandle, rbw, vbw, rejection)
    if res == 0:
    print ‘Configured Sweep Coupling’
    else:
    print ‘Configuring Sweep Coupling Failed: %s’ % str(res)
    self.closeDevice()
    sys.exit(0)

    # Initiate
    def startSweep(self):
    mode = ct.c_int(0)
    flag = ct.c_uint(0)
    res = self.sa.saInitiate(self.deviceHandle, mode, flag)
    if res == 0:
    print ‘Started Sweep’
    else:
    print ‘Start Sweep Failed: %s’ % str(res)
    self.closeDevice()
    sys.exit(0)

    # Get Sweep and Frame Characteristics
    def getSweepInfo(self):
    swlen = ct.c_int()
    swlenPnt = ct.pointer(swlen)
    sfreq = ct.c_double()
    sfreqPnt = ct.pointer(sfreq)
    binsz = ct.c_double()
    binszPnt = ct.pointer(binsz)
    res = self.sa.saQuerySweepInfo(self.deviceHandle, swlenPnt, sfreqPnt, binszPnt)
    if res == 0:
    print ‘Got Sweep Info’
    print ‘Sweep Length: %s’ % str(swlen.value)
    print ‘Start Frequency: %s’ % str(sfreq.value)
    print ‘Bin Size: %s’ % str(binsz.value)

    min = ct.c_double(swlen.value)
    minPnt = ct.pointer(min)
    max = ct.c_double(swlen.value)
    maxPnt = ct.pointer(max)
    # res = self.sa.saGetSweep_64f(self.deviceHandle, minPnt, maxPnt)
    start = ct.c_int32(500)
    startPnt = ct.pointer(start)
    stop = ct.c_int32(1000)
    stopPnt = ct.pointer(stop)
    res = self.sa.saGetPartialSweep_64f(self.deviceHandle, minPnt, maxPnt, startPnt, stopPnt)
    print ‘result: %s’ % str(res)
    if res == 0:
    print ‘Got Sweep Results’
    #print minPnt
    #print min.value
    #print ‘Minimum Point: %s’ % str(min.value)
    #print ‘Maximum Point: %s’ % str(max.value)
    else:
    print ‘Get Sweep Failed: %s’ % str(res)
    self.closeDevice()
    sys.exit(0)
    else:
    print ‘Querying Sweep Info Failed: %s’ % str(res)
    self.closeDevice()
    sys.exit(0)

    def Abort(self):
    res = self.sa.saAbort(self.deviceHandle)
    if res == 0:
    print ‘Aborted’
    else:
    print ‘Abort Failed: %s’ % str(res)
    self.closeDevice()
    sys.exit(0)

    def closeDevice(self):
    res = self.sa.saCloseDevice(self.deviceHandle)
    if res == 0:
    print ‘Closed Device Successfully’
    else:
    print ‘Closing Device Failed: %s’ % str(res)
    sys.exit(0)

    if __name__ == “__main__”:
    sa = SA124B()
    sa.openDevice()
    #sa.getSerialNumber()
    #sa.getFirmwareVersion()
    #sa.getDeviceType()
    #sa.getAPIVersion()
    sa.cfgCeterSpan()
    sa.cfgAcquisition()
    sa.cfgLevel()
    sa.cfgSweepCoupling()
    sa.startSweep()
    sa.getSweepInfo()
    sa.getSweep()
    sa.Abort()
    sa.closeDevice()

    +++++++++

    +++++++++
    ERROR
    +++++++++
    Please find attached Error1.png and Error2.png.
    +++++++++

    Attachments:
    You must be logged in to view attached files.
    #111261
    AndrewAndrew
    Moderator

    Hello akarthikeyan,

    I apologize, I don’t have much experience with ctypes or Python but I can try to help.

    It doesn’t look like you are creating a proper array to pass to our API. This line right here

    min = ct.c_double(swlen.value)

    It looks like you are simply create a single double, whereas you need an array of doubles. (This is an educated guess on my part)

    Also, if you are calling GetSweep, you do not need to call GetPartialSweep. Unless you have specific reason to, you will only need GetSweep. GetPartialSweep is a method for retrieving a very long sweep as it is updating. You should start with simply GetSweep for now until you need this functionality.

    There might be others who can be of further assistance.

    Regards,
    A.J.

    #111280
    akarthikeyan
    Participant

    Thanks for the mail Andrew.

    It is the issue I guess because after passing the correct data type it works!!!

    I have one more question though!
    Can you tell me what is the min and max array values are in case of finding the peak table?

    #111291
    AndrewAndrew
    Moderator

    The min/max array values are used to store the results of the +peak and -peak detectors when MIN_MAX detector is selected. If AVERAGE detector is selected both arrays will contain the same values. Most people only need to use the max array. If you require the use of -peak detector, use the min array.

    Regards,
    A.J.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.