Last active
June 2, 2018 17:09
-
-
Save edy555/d48a647472dc6a994bdf to your computer and use it in GitHub Desktop.
Script to transfer file of hardcopy from R&S CMU200 and format conversion. Requires python-ivi package and wmf2png optionally.
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
#!/usr/bin/env python | |
import ivi | |
import sys | |
import os | |
import os.path | |
from optparse import OptionParser | |
p = OptionParser() | |
p.add_option('-v', '--verbose', dest="verbose", action='store_true', | |
help="print verbose.") | |
p.add_option('-d', '--device', dest="device", metavar="DEV", | |
help="use DEV as serial port") | |
p.add_option('-s', '--speed', dest="speed", default=115200, | |
help="serial speed") | |
p.add_option('-c', dest="convert", action='store_true', | |
help="convert wmf to png") | |
p.add_option('-p', dest="preview", action='store_true', | |
help="preview converted png") | |
p.add_option('-o', dest="output", metavar="FILENAME", | |
help="destination filename") | |
opts, args = p.parse_args() | |
dev = opts.device or '/dev/cu.RS-CMU200-DevB' | |
#dev = opts.device or '/dev/cu.usbserial-FTHGADHV' | |
speed = opts.speed | |
verbose = opts.verbose | |
file = 'PRINT00.WMF' | |
if len(args) > 0: | |
file = args[0] | |
output = opts.output or file | |
def progress_func(label, end_val, bar_length=40, slug='#', space=' '): | |
def write_bar(label, bar, percent): | |
sys.stdout.write("\r{label}: [{bar}] {percent}%".format( | |
label=label, bar=bar, percent=percent | |
)) | |
sys.stdout.flush() | |
def progress(i): | |
percent = float(i) / end_val | |
slugs = slug * int(round(percent * bar_length)) | |
spaces = space * (bar_length - len(slugs)) | |
write_bar(label, slugs + spaces, int(round(percent * 100))) | |
return progress | |
def hcopy_immediate_0(file): | |
(fnam, ext) = os.path.splitext(file) | |
original_fnam = "print??" | |
if not force_restore_fnam: | |
original_fnam = instr._ask("HCOP:DEST:FNAM?") | |
original_fnam = original_fnam[1:-1] # omit quote | |
print original_fnam | |
instr._write("HCOP:DEST IWMF") | |
instr._write("HCOP:DEST:FNAM \"%s\""%fnam) | |
instr._write("*GTL") | |
instr._write("HCOP:IMM") | |
instr._write("HCOP:DEST:FNAM \"%s\""%original_fnam) | |
def hcopy_immediate(file): | |
instr._write("0;HCOP:IMM") | |
def replace_ext(file, ext): | |
(name, oldext) = os.path.splitext(file) | |
return name + ext | |
def fetch_data(file, output): | |
if verbose: | |
print "Transfering data..." | |
instr._write("0;MMEM:CDIR '\USERDATA\PRINT\'") | |
raw = instr._ask_raw("MMEM:DATA? '%s'"%file) | |
if verbose: | |
print len(raw) | |
if verbose: | |
print raw[:10] | |
#dat = raw[8:-2] | |
n = int(raw[1]) | |
size = int(raw[2:(2+n)]) | |
if verbose: | |
print size | |
with open(output, "wb") as f: | |
progress = progress_func(file, size) | |
dat = raw[(2+n):] | |
total = len(dat) | |
f.write(dat) | |
while total < size: | |
s = instr._read_raw() | |
f.write(s) | |
total += len(s) | |
progress(total) | |
#if verbose: | |
#print total | |
if verbose: | |
print "Connecting instrument via %s..."%dev | |
instr = ivi.Driver() | |
instr.initialize('ASRL::%s,%d::INSTR'%(dev,speed)) | |
if verbose: | |
print instr._ask('*IDN?') | |
try: | |
fetch_data(file, output) | |
if opts.convert: | |
os.system("wmf2png %s"%output) | |
if opts.preview: | |
os.system("open %s"%(replace_ext(output, ".png"))) | |
finally: | |
instr._write('*GTL') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment