Skip to content

Commit

Permalink
Merge pull request #2 from atalax/master
Browse files Browse the repository at this point in the history
Add measurements + fix python2 crash
  • Loading branch information
pklaus committed Nov 14, 2015
2 parents 3b0c79d + 8d488df commit bfeb585
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
15 changes: 15 additions & 0 deletions ds1054z/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,21 @@ def set_channel_scale(self, channel, volts, use_closest_match=False):
volts = min(possible_channel_scale_values, key=lambda x:abs(x-volts))
self.write(":{0}:SCALe {1}".format(channel, volts))

def get_channel_measurement(self, channel, item, type="CURRent"):
"""
Measures value on a channel
:param channel: The channel name (like CHAN1, ...). Alternatively specify the channel by its number (as integer).
:type channel: int or str
:param str item: Item to measure, can be vmax, vmin, vpp, vtop, vbase, vamp, vavg, vrms, overshoot, preshoot, marea, mparea, period, frequency, rtime, ftime, pwidth, nwidth, pduty, nduty, rdelay, fdelay, rphase, fphase, tvmax, tvmin, pslewrate, nslewrate, vupper, vmid, vlower, variance, pvrms
:param str type: Type of measurement, can be CURRent, MAXimum, MINimum, AVERages, DEViation
"""
channel = self._interpret_channel(channel)
ret = float(self.query(":MEASure:STATistic:item? {0},{1},{2}".format(type, item, channel)))
if ret == 9.9e37: # This is a value which means that the measurement cannot be taken for some reason (channel disconnected/no edge in the trace etc.)
return None
return ret

def format_hex(byte_str):
if sys.version_info >= (3, 0):
return ' '.join( [ "{:02X}".format(x) for x in byte_str ] )
Expand Down
20 changes: 18 additions & 2 deletions ds1054z/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import sys
import os
import itertools
import errno

from ds1054z import DS1054Z

Expand Down Expand Up @@ -157,6 +158,15 @@ def main():
action_desc = 'Start an interactive shell to control your scope.'
tforce_parser = subparsers.add_parser('shell', parents=[device_parser],
description=action_desc, help=action_desc)
# ds1054z measure
action_desc = 'Measure a value on a channel'
measure_parser = subparsers.add_parser('measure', parents=[device_parser],
description=action_desc, help=action_desc)
measure_parser.add_argument('--channel', '-c', choices=(1, 2, 3, 4), type=int, required=True,
help='Channel from which to take the measurement')
measure_parser.add_argument('--type', '-t', choices=('CURRent', 'MAXimum', 'MINimum', 'AVERages', 'DEViation'), default='CURRent')
measure_parser.add_argument('item', choices=('vmax', 'vmin', 'vpp', 'vtop', 'vbase', 'vamp', 'vavg', 'vrms', 'overshoot', 'preshoot', 'marea', 'mparea', 'period', 'frequency', 'rtime', 'ftime', 'pwidth', 'nwidth', 'pduty', 'nduty', 'rdelay', 'fdelay', 'rphase', 'fphase', 'tvmax', 'tvmin', 'pslewrate', 'nslewrate', 'vupper', 'vmid', 'vlower', 'variance', 'pvrms'),
help='Value to measure')
args = parser.parse_args()

if args.version:
Expand Down Expand Up @@ -343,13 +353,19 @@ def csv_open(filename):
histfile = os.path.join(os.path.expanduser("~"), ".ds1054z_history")
try:
readline.read_history_file(histfile)
except FileNotFoundError:
pass
except IOError as e:
if e.errno != errno.ENOENT:
raise e
atexit.register(readline.write_history_file, histfile)
except ImportError:
pass
run_shell(ds)

if args.action == 'measure':
v = ds.get_channel_measurement(args.channel, args.item, type=args.type)
if v is not None:
print(v)

def run_shell(ds):
""" ds : DS1054Z instance """
print(SHELL_HOWTO)
Expand Down

0 comments on commit bfeb585

Please sign in to comment.