Author Topic: [Python] Flexible data acquisition on the Keysight 34465A  (Read 1703 times)

0 Members and 1 Guest are viewing this topic.

Offline Pack34Topic starter

  • Frequent Contributor
  • **
  • Posts: 753
[Python] Flexible data acquisition on the Keysight 34465A
« on: June 07, 2023, 08:27:18 pm »
What I'm trying to accomplish, is to set the DMM to using the external trigger and collecting 1 sample at each edge of the trigger. The issue is that the amount of triggers will be unknown. I'd like to be able to set the TRIGGER:COUNT to infinity as per the programming manual and then do a READ? when I'm ready to grab the data. The issue is that this doesn't work. The number of triggers specified seems to need to be satisfied in order to query the data from the meter. Otherwise it just times out waiting for a response.

When doing a READ instead of a FETCH it triggers error 420 Query UNTERMINATED.

The programming manual gives the option of INIFINTIY for the TRIGGER_COUNT value so I would assume I should be able to do something like this. Unless there's a specific mode I need to put it in? Do I need to send an alternate of INIT to close the data collection process?

Code: [Select]
import pyvisa as visa
import time
import csv
import datetime

# Setup our instrument
rm = visa.ResourceManager()
v34465A = rm.open_resource('USB0::0x2A8D::0x0101::MY57514376::0::INSTR')    # Open instrument over USB
v34465A.write(':CONFigure:VOLTage:DC %G,%s' % (1.0, 'MAXimum'))             # Set range and resolution
v34465A.write(':SAMPle:COUNt %d' % (1))                                   # Specify number of samples to 100
#v34465A.write(':SAMPle:SOURce %s' % ('TIMer'))                              # Sample using a timer
#v34465A.write(':SAMPle:TIMer %G' % (0.1))                                   # Timer interval of 100ms
v34465A.write(':SAMPle:SOURce %s' % ('IMMediate'))                              # Sample as fast as possible
v34465A.write(':TRIGger:SOURce %s;SLOP POS' % ('EXTernal'))                         # Immediately trigger on init
v34465A.write(':TRIGger:COUNt %d' % (10))                         #
v34465A.write(':FORMat:DATA %s' % ('ASCii'))                                # Data format must be set to fetch
v34465A.write(':INITiate:IMMediate')                                        # Start sampling

# Delay needed to sample. Fetching data will timeout if data not ready
print("Sampling...")
input("Press Enter to continue...")
print("Done")

# Retrieve the data from the instrument
temp_values = v34465A.query_ascii_values(':READ?')
# temp_values = v34465A.query_ascii_values(':FETCh?')

# Create data file
fileNameString = datetime.datetime.now().strftime("%y%m%d%H%M") + "_data.csv"
f1 = open(fileNameString, "w", newline='', encoding='utf-8')
c = csv.writer(f1)
header = ['Sample', 'Voltage']
c.writerow(header)

sample_counter = 0
for x in temp_values:       
    data = [ sample_counter, x ]
    c.writerow(data)
    sample_counter = sample_counter+1

f1.close()
v34465A.close()
rm.close()
 

Offline Pack34Topic starter

  • Frequent Contributor
  • **
  • Posts: 753
Re: [Python] Flexible data acquisition on the Keysight 34465A
« Reply #1 on: June 07, 2023, 09:43:35 pm »
After spending most of the day on this, I think I figured it out. You have to return the device to idle before you can retrieve the data. The READ command is supposed to be a FETCH and delete but I haven't been able to get that to work. The workaround I'm using is to query the amount of data points in memory and then remove that data in a separate transaction. Feels cludgey but it works.

Code: [Select]
import pyvisa as visa
import time
import csv
import datetime
 
# Setup our instrument
rm = visa.ResourceManager()
v34465A = rm.open_resource('USB0::0x2A8D::0x0101::MY57514376::0::INSTR')  # Open instrument over USB
v34465A.write(':CONFigure:VOLTage:DC %G,%s' % (1.0, 'MAXimum'))           # Set range and resolution
v34465A.write(':SAMPle:COUNt %d' % (1))                                   # Specify number of samples to 100
v34465A.write(':SAMPle:SOURce %s' % ('IMMediate'))                        # Sample as fast as possible
v34465A.write(':TRIGger:SOURce %s;SLOP POS' % ('EXTernal'))               # External triggering
v34465A.write(':TRIGger:COUNt %s' % ('INFinity'))                         # Trigger as many times as memory allows
v34465A.write(':FORMat:DATA %s' % ('ASCii'))                              # Data format must be set to fetch
v34465A.write(':INITiate:IMMediate')                                      # Start sampling
 
# Wait for keypress for testing purposes
print("Sampling...")
input("Press Enter to continue...")
print("Done")
 
# ABORT returns the DMM to idle state to retrieve data
v34465A.write(':ABORt')
 
# Retrieve the data from the instrument by reading how much data there is, and removing that amount of data
temp_values = v34465A.query_ascii_values(':DATA:POINts?')
points = int(temp_values[0])
temp_values = v34465A.query_ascii_values(':DATA:REMove? %d' % (points))
 
# Create data file
fileNameString = datetime.datetime.now().strftime("%y%m%d%H%M") + "_data.csv"
f1 = open(fileNameString, "w", newline='', encoding='utf-8')
c = csv.writer(f1)
header = ['Sample', 'Voltage']
c.writerow(header)
 
sample_counter = 0
for x in temp_values:       
    data = [ sample_counter, x ]
    c.writerow(data)
    sample_counter = sample_counter+1
 
f1.close()
v34465A.close()
rm.close()
 

Offline TizianoHV

  • Regular Contributor
  • *
  • Posts: 118
  • Country: it
    • My Website
Re: [Python] Flexible data acquisition on the Keysight 34465A
« Reply #2 on: June 08, 2023, 08:55:30 am »
I think you shouldn't sent 'READ?' or 'FETCH?' with 'TRIG:COUNT INF' because they'll wait for infinite readings!
Since readings are saved to the memory i would work only with memory. As soon as a reading is stored you read and clear it.



Code: [Select]
import pyvisa as visa
import time
import csv
import datetime
 
# Setup our instrument
rm = visa.ResourceManager()
v34465A = rm.open_resource('USB0::0x2A8D::0x0101::MY57514376::0::INSTR')  # Open instrument over USB

v34465A.write('*RST')   #Reset instrument

v34465A.write('CONF:VOLT:DC %G,%s' % (1.0, 'MAXimum'))           # Set range and resolution

v34465A.write('SAMP:COUN %d' % (1))                #1 sample per trigger
v34465A.write('TRIG:SOUR EXT;SLOP POS')          # External triggering
v34465A.write('TRIG:COUN INF')                    #Infinite triggers


#v34465A.write(':FORM:DATA %s' % ('ASCii'))     #It is necessary? #Data format must be set to fetch

# Create data file
fileNameString = datetime.datetime.now().strftime("%y%m%d%H%M") + "_data.csv"
f1 = open(fileNameString, "w", newline='', encoding='utf-8')
c = csv.writer(f1)
header = ['Sample', 'Voltage']
c.writerow(header)

tempData = []

loop = "ok"
while loop != "s":
    print("Meas loop started. CTRL+c to stop")

    v34465A.write(':INIT')              # Exit from idle state, ready for trigger
    rdngCnt = 0
    try:
        while True:
            if int(v34465A.query("DATA:POINTS?").rstrip("\n").rstrip("\r")) > 0:
                tempData.append(v34465A.query("DATA:REMOVE? 1").rstrip("\n").rstrip("\r"))

                #Save to file ...

                rdngCnt += 1

            time.sleep(0.05)      #some margin...
    except KeyboardInterrupt:
        print("SCAN STOPPED")
    except AttributeError:
        print("Something")

    v34465A.write(':ABORt')       #Return to idle state
    loop = input("'s' to stop: ")
« Last Edit: June 08, 2023, 11:44:03 am by TizianoHV »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf