#!/usr/bin/env python3 GPIB_ADDR = 18 # change to suit your instrument import pyvisa, hpgl, sys, io, os from contextlib import contextmanager @contextmanager def _no_stderr(): orig_stderr=os.dup(2); dev_null=os.open(os.devnull, os.O_WRONLY) os.dup2(dev_null, 2); os.close(dev_null); yield os.dup2(orig_stderr, 2); os.close(orig_stderr) rm=pyvisa.ResourceManager() instr=rm.open_resource('GPIB0::'+str(GPIB_ADDR)+'::INSTR') bus=rm.open_resource("GPIB0::INTFC") # Get the HPGL data with _no_stderr(): instr.write("PLOT") bus.send_command(bytes([0x3f, 0x20, 0x5f, 0x40+GPIB_ADDR])) hpgl_data=bus.visalib.buffer_read(bus.session,32000) # Fix the HPGL fonts hpgl_data=hpgl_data[0].replace(b';SR1.67,1.78;', b';SR0.1,0.1;') # Convert to SVG and fix linewidth svg_data=hpgl.hpgl2svg(io.StringIO(hpgl_data.decode("utf-8"))) svg_data=svg_data.replace('stroke-width="14.000"', 'stroke-width="1.500"') print("Content-type: image/svg+xml\n\n"+svg_data)