Example_Powersupply_GPP4323

This is an example of using the library.

 1# %% ==========================================================================
 2# Import and Definitions
 3# =============================================================================
 4import time
 5import datetime
 6import pandas as pd
 7import numpy as np
 8import matplotlib.pyplot as plt
 9from tqdm import tqdm
10
11
12
13# Instrument Libraries Github: https://github.com/MartinMiroslavovMihaylov/Python_Instruments_Automation_Scripts
14# Install with:
15# pip install git+https://github.com/MartinMiroslavovMihaylov/Python_Instruments_Automation_Scripts.git
16
17# from Instruments_Libraries.GPP4323 import GPP4323
18from Instruments_Libraries.InstrumentSelect import PowerSupply_GPP4323
19
20# %% ==========================================================================
21# Select Instruments and Load Instrument Libraries
22# =============================================================================
23# myGPP4323 = GPP4323('COMXX') # replace with your COM Port
24myGPP4323 = PowerSupply_GPP4323()
25myGPP4323.reset()
26
27# %% ==========================================================================
28# Setup the Measurement
29# =============================================================================
30num_of_points = 10
31sleep_time = 0.5
32vcc_ch_num = 1 # channel number for VCC
33vcc = 4.7 # V
34vcc_current_limit = 0.140 # A
35
36# %% ==========================================================================
37# Configure the Instrument
38# =============================================================================
39myGPP4323.set_Voltage(vcc_ch_num, vcc)
40myGPP4323.set_CurrentLimit(vcc_ch_num, vcc_current_limit)
41
42# %% ==========================================================================
43# Measurement
44# =============================================================================
45myGPP4323.set_Out(channel=vcc_ch_num, state='ON') # turn on the channel or
46# myGPP4323.set_AllOut(state='ON') # turn on all channels
47
48records = [] # Empty list to store data and meta data
49for i in tqdm(range(num_of_points)):
50    rec = {} # single record
51    rec["VCC_Voltage"] = myGPP4323.ask_Voltage(vcc_ch_num)
52    rec["VCC_Current"] = myGPP4323.ask_Current(vcc_ch_num)
53    rec["Power"] = myGPP4323.ask_Power(vcc_ch_num)
54    rec["Timestamps"] = datetime.datetime.now()
55    records.append(rec)
56    time.sleep(sleep_time)
57
58# %% ==========================================================================
59# Create Dataframe
60# =============================================================================
61meas_df = pd.DataFrame.from_records(records)
62
63# %% ==========================================================================
64# Plot the Measurement
65# =============================================================================
66t0 = meas_df["Timestamps"].iloc[0]
67relative_time = (meas_df["Timestamps"] - t0).dt.total_seconds()
68
69plt.plot(relative_time, meas_df["VCC_Current"])
70plt.xlabel('Time (s)')
71plt.ylabel('Current (A)')
72plt.show()
73# %% ==========================================================================
74# Save Dataframe
75# =============================================================================
76# Save DataFrame to HDF5 (better than CSV)
77meas_df.to_hdf("measurements.h5", key="data", mode="w")
78# key="data" is like a "dataset name" inside the HDF5 file 
79# (you can store multiple DataFrames in one file with different keys).
80# mode="w" overwrites the file. Use mode="a" if you want to append new datasets.
81
82# Later: Load it back
83loaded_df = pd.read_hdf("measurements.h5", key="data")
84print(loaded_df.head())
85
86#or
87
88# Save DataFrame to CSV
89meas_df.to_csv("measurements.csv", index=False)
90
91# Load it back, auto-parsing the "Timestamps" column as datetime
92loaded_df = pd.read_csv("measurements.csv", parse_dates=["Timestamps"])
93print(loaded_df.head())
94
95# %% ==========================================================================
96# Close Instrument
97# =============================================================================
98myGPP4323.Close()