Author Topic: My First ChatGPT Experience  (Read 477 times)

0 Members and 1 Guest are viewing this topic.

Offline DrNefarioTopic starter

  • Contributor
  • Posts: 23
  • Country: us
My First ChatGPT Experience
« on: January 16, 2023, 05:27:18 pm »
Up until a few days ago, I was unaware of the ChatGPT existence.  I was having dinner with my son who told me about it.  He said I should try it to see what it can do.  This sounded intriguing, so I thought I would stump it with something hard.  I asked it to write a program in Python that would read an HP3478A voltmeter via the GPIB bus.  A few seconds later this is what it had:
Code: [Select]
import visa
import time
import csv

# Establish a connection to the voltmeter
rm = visa.ResourceManager()
voltmeter = rm.open_resource('GPIB0::7::INSTR')

# Configure the voltmeter to measure voltage
voltmeter.write("CONF:VOLT:DC")

# Set the measurement interval and the duration of the data collection
interval = 1 # seconds
duration = 300 # seconds

# Open a file to save the data
with open('voltage_data.csv', 'w', newline='') as csvfile:
    # Create a CSV writer
    csvwriter = csv.writer(csvfile)

    # Write the header row to the CSV file
    csvwriter.writerow(["Time", "Voltage"])

    # Start collecting data
    start_time = time.time()
    while time.time() - start_time < duration:
        # Read the voltage value
        voltage = float(voltmeter.query("MEAS:VOLT:DC?"))

        # Get the current time
        current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())

        # Write the data to the CSV file
        csvwriter.writerow([current_time, voltage])

        # Wait for the specified interval before taking the next measurement
        time.sleep(interval)
    print("Data collection complete.")

# Close the connection to the voltmeter
voltmeter.close()

The few things I had to do to run this was to:
1.   change visa to Pyvisa
2.   correct my GPIB address
3.   change the write and query statements to use proper HP3478 codes:
   a.   voltmeter.write ("CONF:VOLT:DC")  voltmeter.write ("F1T4R0Z1N3")
   b.   float(voltmeter.query("MEAS:VOLT:DC?"))  float(voltmeter.query("T3"))

The older HP instruments like this were designed prior to SCPI language but the ChatGPT used SCPI commands which didn’t work.  In fact, the meter thought that it was trying to write calibration values but announced that the CAL ENABLE switch was off.  Good thing it was.
All-in-all I was quite impressed.   I next asked it to include a GUI with start and stop buttons which it promptly did using the “tkinter” modules.  This one wasn’t 100% as the stop button was never read, but most of the code was there.  Still, pretty impressive.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf