Jump to content
  • 0

Python logging like the waveforms voltmeter


michaelvv

Question

2 answers to this question

Recommended Posts

Hi @michaelvv

It can be done like in t he following example: AnalogIn_Logger.py

>python AnalogIn_Logger.py
DWF Version: b'3.11.8'
Opening first device
Generating AM sine wave...
Press Ctrl+C to stop
DC:0.002V DCRMS:0.048V ACRMS:0.048V
DC:0.502V DCRMS:1.140V ACRMS:1.024V
DC:0.501V DCRMS:1.040V ACRMS:0.912V
DC:0.502V DCRMS:0.916V ACRMS:0.767V
DC:0.501V DCRMS:0.801V ACRMS:0.625V

Link to comment
Share on other sites

Hi Attila.

Your example works perfectly. I have done some modifications to get channel 1,2 in within the loop.

It works fine but I have one question. In the line in your code

dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(10))

Shouldn't I set it for the channel 2, If I wish to read both channels ??

like this dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(1), c_double(10)).

It seems to work anyway.

He's your modified code.

"""
   DWF Python Example
   Author:  Digilent, Inc.
   Revision:  2018-07-19

   Requires:                       
       Python 2.7, 3
   Description:
   Generates AM modulated signal on AWG1 channel
   Scope performs scan shift acquisition and logs DC and AC/DC-RMS values
"""

from ctypes import *
import math
import time
import matplotlib.pyplot as plt
import sys

if sys.platform.startswith("win"):
    dwf = cdll.dwf
elif sys.platform.startswith("darwin"):
    dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
    dwf = cdll.LoadLibrary("libdwf.so")

hdwf = c_int()

sts = c_byte()
secLog = 0.5 # logging rate in seconds
nSamples = 8000
rgdSamples = (c_double*nSamples)()
cValid = c_int(0)

version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))

print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))

if hdwf.value == 0:
    szerr = create_string_buffer(512)
    dwf.FDwfGetLastErrorMsg(szerr)
    print(str(szerr.value))
    print("failed to open device")
    quit()


#set up acquisition for channel 1

dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_bool(True))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(10))

dwf.FDwfAnalogInAcquisitionModeSet(hdwf, c_int(1)) #acqmodeScanShift
dwf.FDwfAnalogInFrequencySet(hdwf, c_double(nSamples/secLog))
dwf.FDwfAnalogInBufferSizeSet(hdwf, c_int(nSamples))



#wait at least 2 seconds for the offset to stabilize
time.sleep(2)

#begin acquisition
dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1))

# used for skipping the first sample
first = True

print("Press Ctrl+C to stop")
try:
    while True:
        dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))
        dwf.FDwfAnalogInStatusSamplesValid(hdwf, byref(cValid))

        # loop through both channels 0,1
        for channel in range(2):

            # channel in loop
            dwf.FDwfAnalogInStatusData(hdwf, c_int(channel), byref(rgdSamples), cValid) # get channel 1 or 2 data

            dc = 0
            for i in range(nSamples):
                dc += rgdSamples[i]
            dc /= nSamples
            dcrms = 0
            acrms = 0
            for i in range(nSamples):
                dcrms += rgdSamples[i] ** 2
                acrms += (rgdSamples[i]-dc) ** 2
            dcrms /= nSamples
            dcrms = math.sqrt(dcrms)
            acrms /= nSamples
            acrms = math.sqrt(acrms)

            if not first:

                print(f"Channel {channel+1} = DC:{dc:.3f}V DCRMS:{dcrms:.3f}V ACRMS:{acrms:.3f}V")


        time.sleep(secLog)

        first = False

except KeyboardInterrupt:
    pass

dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(False))
dwf.FDwfDeviceCloseAll()

Thanks for the code example..

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...