-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add example to manual - add Python SCPI/PyVISA example code (WIP)
- Loading branch information
Showing
3 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#/usr/bin/python3 | ||
|
||
from datetime import datetime, timedelta | ||
import pdb | ||
import time | ||
|
||
import pyvisa | ||
|
||
import matplotlib.pyplot as plt | ||
import pandas as pd | ||
import seaborn as sns | ||
|
||
|
||
# select the instrument | ||
def get_device_index_number(instances): | ||
available_instruments = [] | ||
print('Available instruments (Select number and press Enter):') | ||
|
||
for num, inst in enumerate(instances): | ||
print(f'{num}: {inst}') | ||
available_instruments.append(str(num)) | ||
print(available_instruments) | ||
|
||
device = input('Make your selection: ') | ||
print(f'Selected instrument: "{device}"') | ||
if device not in available_instruments: | ||
print('Invalid instrument number') | ||
return -1 | ||
else: | ||
return device | ||
|
||
# decide on the length of capture | ||
def get_logging_time(): | ||
logging_time = input('Logging time in seconds: ') | ||
return int(logging_time) | ||
|
||
def connect_to_device(device_num): | ||
rm = pyvisa.ResourceManager('@py') | ||
instances = rm.list_resources() | ||
|
||
while (device_num == -1): | ||
device_num = get_device_index_number(instances) | ||
|
||
return rm.open_resource(instances[int(device_num)]) | ||
|
||
# setup the instrument | ||
def setup_instrument(inst, output_channel, device_input_voltage): | ||
inst.baud_rate = 115200 | ||
inst.read_termination = '\r\n' | ||
inst.write_termination = '\r\n' | ||
print(inst.query('*idn?', 0.2)) | ||
inst.write(f'source{output_channel}:volt {device_input_voltage}') | ||
inst.write(f'source{output_channel}:curr 3') # no limit on current | ||
|
||
def capture(inst, output_channel, logging_time, data): | ||
#data = [] | ||
line = [] | ||
start_time = datetime.now() | ||
print(start_time) | ||
end_time = start_time + timedelta(seconds=logging_time) | ||
print(end_time) | ||
inst.write(f'system:communicate:serial:feed LOG') | ||
inst.write(f'output{output_channel} ON') | ||
time.sleep(1) | ||
|
||
while(datetime.now() < end_time): | ||
line = inst.read().split(',') | ||
print(line) | ||
data.append(line) | ||
return(data) | ||
|
||
def finish_capture(inst, output_channel): | ||
#inst.write(f'output{output_channel} OFF') # uncomment to cut power to the measured device when the logging is done | ||
inst.write(f'system:communicate:serial:feed NONE') | ||
|
||
def plot_graph(data): | ||
df = pd.DataFrame(data, columns =[ | ||
'ms', | ||
'input_volt', 'input_ampere', 'input_watt', 'input_on_off', | ||
'ch0_volt', 'ch0_ampere', 'ch0_watt', 'ch0_on_off', 'ch0_interrupts', | ||
'ch1_volt', 'ch1_ampere', 'ch1_watt', 'ch1_on_off', 'ch1_interrupts', | ||
'CheckSum8_2s_Complement', 'CheckSum8_Xor']) | ||
|
||
ndf = df.drop([ | ||
'input_volt', 'input_ampere', 'input_watt', 'input_on_off', | ||
'ch0_volt', 'ch0_ampere', 'ch0_on_off', 'ch0_interrupts', | ||
'ch1_volt', 'ch1_ampere', 'ch1_watt', 'ch1_on_off', 'ch1_interrupts', | ||
'CheckSum8_2s_Complement', 'CheckSum8_Xor' | ||
], axis=1) | ||
print(ndf) | ||
sns.lineplot(x=ndf['ms'], y=ndf['ch0_watt'], data=ndf) | ||
plt.show() | ||
|
||
def run(): | ||
device_input_voltage = 5.1 # input voltage (V) | ||
output_channel = 1 # output channel | ||
device_num = -1 | ||
logging_time = 10 # in seconds | ||
data = [] | ||
|
||
inst = connect_to_device(device_num) | ||
setup_instrument(inst, output_channel, device_input_voltage) | ||
time.sleep(5) | ||
try: | ||
capture(inst, output_channel, logging_time, data) | ||
except: | ||
print('Sorry, no data could be captured this time') | ||
finish_capture(inst, output_channel) | ||
if data: | ||
plot_graph(data) | ||
|
||
if __name__ == '__main__': | ||
run() |