Post Go back to editing

[Python/ADALM2000] ain.getSamples crashes the program with "Windows fatal exception: access violation" within try/except when ctx.setTimeout is specified

Hello,

I am using an ADLAM2000 with libm2k 0.6.0 and python bindings. I tried python 3.9, 3.10, libm2k 0.5.0 and 0.6.0 with similar results.

I am trying to initiate the acquisition by the triggering event. The problem is that if I set the trigger timeout to any value for example: ctx.setTimeout(3000) , my whole program crashes if no triggering happens within 3 seconds with an error:

Windows fatal exception: access violation


Current thread 0x00005594 (most recent call first):
File "C:\ProgramData\Anaconda3\envs\SpyderTestForge\lib\site-packages\libm2k.py", line 4736 in getSamples
File "C:\Users\User\Documents\PythonProjects\EL_v01\adalm2000.py", line 79 in get_data
File "c:\users\user\documents\pythonprojects\el_v01\elmain_v01.py", line 30 in generate_data
File "C:\ProgramData\Anaconda3\envs\SpyderTestForge\lib\threading.py", line 946 in run
File "C:\ProgramData\Anaconda3\envs\SpyderTestForge\lib\threading.py", line 1009 in _bootstrap_inner
File "C:\ProgramData\Anaconda3\envs\SpyderTestForge\lib\threading.py", line 966 in _bootstrap

Main thread:
Thread 0x00003ca4 (most recent call first):
File "c:\users\user\documents\pythonprojects\el_v01\elmain_v01.py", line 178 in <module>
File "C:\ProgramData\Anaconda3\envs\SpyderTestForge\lib\site-packages\spyder_kernels\py3compat.py", line 356 in compat_exec
File "C:\ProgramData\Anaconda3\envs\SpyderTestForge\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 469 in exec_code
File "C:\ProgramData\Anaconda3\envs\SpyderTestForge\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 611 in _exec_file
File "C:\ProgramData\Anaconda3\envs\SpyderTestForge\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 524 in runfile
File "C:\Users\User\AppData\Local\Temp\ipykernel_9124\2213792394.py", line 1 in <cell line: 1>

samples are acquired in a separate thread:

try:
     data = self.ain.getSamples(n_of_samples)
except:
     print("getSamples Error")

Please let me know if you need any additional information.

  • Did you install libm2k via conda install?

    Do you get the same error if you are trying to run this in the main thread ?

    -Adrian

  • import libm2k
    import numpy as np
    import time
    
    sample_rate = 100000000
    DIGITAL_CH = 0
    
    ctx = libm2k.m2kOpen()
    if ctx is None:
                print("Connection Error: No ADALM2000 device available/connected to your PC.")
                exit(1)
    
    
    ADCSampleRate = sample_rate
    ain = ctx.getAnalogIn()
    # Prevent bad initial config for ADC and DAC
    ain.reset()
    ctx.calibrateADC()
    ctx.setTimeout(3000)
    ain.enableChannel(0, True)
    ain.enableChannel(1, True)
    ain.setSampleRate(sample_rate)
    ain.setRange(0, libm2k.PLUS_MINUS_2_5V)  # set range to 2.5V - 1, 25V - 0
    ain.setRange(1, libm2k.PLUS_MINUS_2_5V)  # set range to 2.5V - 1, 25V - 0
    print(ain.getAvailableSampleRates())
    print(ain.getAvailableRanges())
    
    
    dig = ctx.getDigital()
    dig.reset()
    dig.setDirection(DIGITAL_CH, libm2k.DIO_OUTPUT)
    dig.enableChannel(DIGITAL_CH, True)
    dig.setValueRaw(DIGITAL_CH, libm2k.LOW)
    
    
    trig = dig.getTrigger()
    # self.trig.setAnalogMode(0, libm2k.EXTERNAL)  # Trigger condition specified only by external trigger (TI)
    trig.setAnalogMode(0, libm2k.ANALOG)
    trig.setAnalogSource(0)
    trig.setAnalogExternalCondition(0, libm2k.RISING_EDGE_ANALOG)
    trig.setAnalogLevel(0, 1)  # Set trigger level at 0.5
    trig.setAnalogDelay(0)  # Trigger is centered
    
    
    ain.startAcquisition(800000)
    time.sleep(1)
    
    
    try:
        data = ain.getSamples(64)
    except:
        print("getSamples Error")
                
    npdata = np.array(data)
    print(data)
    
    libm2k.contextClose(ctx)

    Hi Adrian,

    Thank you for helping. I put everything in one file you can run on an ADALM2000 with nothing connected. The program crashes with "Windows fatal exception: access violation" after 3 seconds of waiting for the trigger. If you connect channel 1 to the signal generator it runs fine and prints 64 values.

    Edit: I tried both conda 0.5.0 version and manual 0.6.0 pip installations on two separate windows 10 computers

  • Hi,

    When you tried the pip installation, did you clean up the conda package?
    The conda package for Windows is built without Exception handling - this means that when an exception should be thrown, only an error message will be printed and the program will likely crash.

    However, the pip package + the official libm2k installer have the Exception handling enabled. But having both libm2k installed in the system and in the conda environment, might get the libraries mixed up.

    Could you try to uninstall the conda package completely? Then use this installer to install libm2k in your system ( https://github.com/analogdevicesinc/libm2k/releases/download/v0.6.0/libm2k-0.6.0-Windows-setup.exe ) and then install libm2k using pip, with the following command pip install -i https://test.pypi.org/simple/ libm2k .

    -Alexandra

  • Hi Alexandra,

    I tried the clean install of the 0.6.0 on another machine with a similar result. Did you try running the code? You don't need to connect anything to the ADALM2000. I will try another computer again if you can't reproduce the problem