HPGL2 is the standardized version of the Hewlett-Packard Graphics Language. It consists of two letter commands that may be followed by a comma-delimited set of arguments. If you think about this as instructions to a pen on a plotter, you'll quickly get the idea. Examples of commonly used commands are:

Command Description
PD Pen Down so it can write.
PU Pen Up.
PA 67, 22 Pen Absolute. This tells the pen to go to x,y coordinates (67,22). If the pen is down, it will draw a line from its present position to this position.
LB Hello World The pen will print the letters after the 'LB' up to the non-printing end-of-text character (decimal code 3).

These commands are often delimited by a semicolon or, in some instances, the next two-letter command. The exception is the label command (LB). The end-of-text character terminates it.

To help you convert HPGL2 to a graphic in a PictureBox an example class, clsHPGL2ToPicture, is included in the example download.

The HPGL2 example demonstrates the commands and code snippets to retrieve and print the screen image. The example includes several different instruments to demonstrate how to return the binary data from the instrument. Use the object browser for a description of each method and property for the HPGL2 converter. For simplicity, there is no error checking. Be sure to use the correct GPIB address to prevent a long timeout or hang-up of the IO interface.

HPGL2 Conversion Class Example

The first example uses the HPGL2 conversion class for an IEEE488.2 compliant instrument like the Keysight 8753E. The special case of older instruments is covered later.

Let's discuss an Keysight 8753E Spectrum Analyzer. The Keysight 8753E supports HPGL2 as an output. The code snippets below use the Read method of VISA COM to load an array of bytes from the instrument. Since you don't know how many bytes to expect, set the 'count' of the Read method to return the an amount much larger than expected. The Read method will fill the array until there is no more data and then terminate.

    Dim mgr As KeysightRMLib.SRMCls
    Dim ioAddress As String
    Dim data() As Byte
    ioAddress = "GPIB::16"
    Set mgr = New KeysightRMLib.SRMCls
    Set Device = New VisaComLib.FormattedIO488
    Set Device.IO = mgr.Open(ioAddress)
    Device.IO.Timeout = 15000
    Device.WriteString "OUTPPLOT"
    data() = Device.IO.Read(150000)

Once the Read method returns, you must convert the byte data in the array data() into a bit map. This is a 4-step process using the HPGL2ToPicture class.

  1. Instantiate the HPGL2 class, pass it a picture box with the AutoRedraw property set to True.
  2. Select default values of pens, colors, background, font etc. that will be applied to a picture box.
  3. Use the Plot method to create a picture. The Plot method will convert the byte data to HPGL2 string commands. Place each command in an element of the string array. Step through the command array and execute each HPGL2 command on a PictureBox.
  4. Provide a way to refresh the PictureBox if it is resized.

These four steps are illustrated below. The variable picScreen is a PictureBox placed on a form, while data is the binary data from the instrument as described above.

1. Set up the HPGL2 converter.

     Dim plotter As clsHPGL2ToPicture
     Set plotter = New clsHPGL2ToPicture
     picScreen.AutoRedraw = True
     Set plotter.PictureBox = picScreen

2. Set defaults.

     With plotter
       ' set default values
       .Backcolor = vbWhite
       .IncludeTimeStamp = True
       .Zoom = 95 ' in per cent
       .isMonochrome = False
       .PlotterFontName = "Arial"
       .isDefaultPenColors = True
       .LineThickness = 1 ' width in pixels
     End With

3. Convert data to HPGL2 commands and put in PicutreBox.

     With plotter
       .HPGL2Data = data
       .Plot
     End With

4. Refresh

Once the picture box contains the screen picture, when the picture box changes size you need a way to refresh it to improve the resolution to match the increased size. To update the PictureBox simply invoke the Refresh method.

     plotter.Refresh

Printing

You can print using the same code as when you use a PictureBox. The Print object has the same graphical commands as the PictureBox. Pass the Printer object instead of a PictureBox to the HPGL2ToPicture class. Once the data has been plotted to the PictureBox, this code will send the picture to the printer. After printing, set the plotter back to the PictureBox.

     Set plotter.PictureBox = Printer
 
     ' print graphics to printer object
     plotter.PrintHPGL2
     Printer.EndDoc
 
     Set plotter.PictureBox = Me.picScreen

You'll find more details on setting up the printer in the HPGL2 example.

Limitations

Instrument Data

Instruments that do not follow the IEEE488.2 protocol may not return when getting the binary data until the timeout of the IO interface is reached. (If the instrument does not respond to '*IDN?' it most likely is not IEEE488.2 compliant.) These instruments expect to take control of the I/O interface to send data to a device such as a plotter and do not support being controlled through the I/O interface from a PC.
The timeout will often be longer than you want to wait. Often, you don't know how long the data takes because it depends on the Screen content. A full screen may take twice as long as a simple screen on the same instrument. So you have to set the timeout for the longest case. A way around this is to read the first byte with a long time out (~5sec) to give the instrument time to calculate and respond. Then, read all the following bytes one at a time with a short time out. When no more data comes back the I/O will quit with the short timeout. This works unless the instrument has long gaps while sending the binary data.

The Hewlett-Packard 4145B is an older instrument that responds to this alternative. Getting the binary data looks like this.

     Dim b As Byte
     Device.IO.TerminationCharacter = 10 
     Device.WriteString "PL0,0,10900,7650"
     delay 1000
     b = Device.IO.Read(1)
     data(0) = b
     Device.IO.Timeout = 1000
     Do
       b = Device.IO.Read(1)
       i = i + 1
       data(i) = b)
     Loop Until Err.Number <> 0

On some instruments you can avoid this problem altogether by setting the EOI or END to true.

For much older instruments (>15 yrs) getting the binary data may be more difficult. Some older instruments don't even have a command to get the data, but require that you run the code and then hurry and press a 'Plot' or 'Print' button on the instrument before the code times out.

HPGL2 Converter

The clsHPGL2ToPicture class is not a complete implementation of the HPGL2 commands. It was implemented to convert instruments commonly available. Check the Immediate window in the Visual Basic 6.0 development environment. The HPGL2 class will write any used but not implemented commands to that window. Instruments may include commands that are not included in HPGL2 handbook.

Fonts in the HPGL2 converter are implemented with the PictureBox font object. The font size may not fit the original designers intent when the PictureBox is small. The font size is selected by matching the available font size of the selected font with the requirements of the HPGL2 data. Try using different font names and select one that best fits the instrument and the size of the PictureBox.

Changing the Size of a PictureBox

You can change the size of the PictureBox with the resize command of the Parent form. If you include a refresh at that time, the implementation will be slow for large data sizes since it may have to be refreshed several times as the user sizes the window. When enlarging the picture, you may prefer to rewrite the data to the PictureBox to get the benefit of more resolution with the larger PictureBox. The Refresh method of the HPGL2 converter will rewrite the same data of the PictureBox. Alternately you can use the PaintPicture method of the PictureBox. Enlarging the image with PaintPicture will give blocky appearance.