# Keysight 81160A

Control the Keysight 81160A function generator from Python with the `Keysight81160A` driver.

**Driver:** [pymeasure](https://pymeasure.readthedocs.io)  
**Type:** [Function and waveform generators](https://dev.nominal.io/instro/instruments/type/awg.md)  
**Manufacturer:** [Keysight](https://dev.nominal.io/instro/instruments/manufacturer/keysight.md)

Represent the Keysight 81160A and provide a high-level interface for interacting with the instrument.

## Publishing through instro

instro publishes the commands you send to a Keysight81160A and the values you read from it, through any [publisher](https://dev.nominal.io/instro/library/publishers.md). It takes two forms, both described in the [pymeasure guide](https://dev.nominal.io/instro/library/pymeasure.md). Install with `uv add "instro[pymeasure]"`.

### With a PymeasureInstrument

[`PymeasureInstrument`](https://dev.nominal.io/instro/library/pymeasure.md#write-new-instro-code) wraps the driver in an instro instrument, with tags and background polling. The instrument is created inside the `with` block, and `.device` is the pymeasure instrument.

```python
from pymeasure.instruments.keysight import Keysight81160A
from instro.pymeasure import PymeasureInstrument

with PymeasureInstrument(
    name="generator",
    instrument=Keysight81160A("GPIB::1"),
    dataset_rid="<dataset rid>",
) as generator:
    generator.device.reset()                            # Reset the generator to default settings

    generator.device.shape = "SIN"                      # Set default channel output shape to sine
    generator.device.channels[1].shape = "SIN"          # Set channel 1 output signal shape to sine
    generator.device.frequency = 1e3                    # Set default channel output frequency to 1 kHz
    generator.device.channels[1].frequency = 1e3        # Set channel 1 output frequency to 1 kHz
    generator.device.channels[2].amplitude = 5          # Set channel 2 output amplitude to 5 Vpp
    generator.device.channels[2].offset = 0.5           # Set channel 2 output offset to 0.5 V
    generator.device.channels[2].output = True          # Enable channel 2 output

    # Output a square wave at 1 kHz with 5 Vpp and 0.5 V offset
    generator.device.channels[2].apply_square(1e3, 5, 0)

    ch1 = generator.device.channels[1]                  # Short form for channel 1
    waveform = [-2.0, -1.5, 0.0, 1.5, 2.0]       # Define a user-defined waveform in Volts
    ch1.waveform_volatile = waveform             # Set user-defined waveform to volatile memory
    print(f"{ch1.free_memory_slots} slots free") # Get number of slots in non-volatile memory
    ch1.save_waveform(waveform, "test")          # Save the waveform to non-volatile memory
    ch1.shape = "USER"                           # Set channel 1 shape to user-defined
    ch1.trigger_mode = "MAN"                     # Set trigger mode to manual
    ch1.trigger_count = 2                        # Set number of cycles to be generated to 2
    ch1.output = True                            # Enable channel 1 output
    generator.device.trigger()                          # Trigger the generator to output the waveform
    ch1.delete_waveform("test")                  # Delete the waveform from non-volatile memory
```

### With existing pymeasure code (`attach()`)

[`attach()`](https://dev.nominal.io/instro/library/pymeasure.md#publish-from-existing-code) adds publishing to pymeasure code you already have. The imports, the `attach()` call after the instrument is created, and `recorder.close()` are all that instro adds.

```python
from pymeasure.instruments.keysight import Keysight81160A
from instro.lib import NominalCorePublisher
from instro.pymeasure import attach

generator = Keysight81160A("GPIB::1")        # Replace with your device address
recorder = attach(generator, "generator", [NominalCorePublisher(dataset_rid="<dataset rid>")])
generator.reset()                            # Reset the generator to default settings

generator.shape = "SIN"                      # Set default channel output shape to sine
generator.channels[1].shape = "SIN"          # Set channel 1 output signal shape to sine
generator.frequency = 1e3                    # Set default channel output frequency to 1 kHz
generator.channels[1].frequency = 1e3        # Set channel 1 output frequency to 1 kHz
generator.channels[2].amplitude = 5          # Set channel 2 output amplitude to 5 Vpp
generator.channels[2].offset = 0.5           # Set channel 2 output offset to 0.5 V
generator.channels[2].output = True          # Enable channel 2 output

# Output a square wave at 1 kHz with 5 Vpp and 0.5 V offset
generator.channels[2].apply_square(1e3, 5, 0)

ch1 = generator.channels[1]                  # Short form for channel 1
waveform = [-2.0, -1.5, 0.0, 1.5, 2.0]       # Define a user-defined waveform in Volts
ch1.waveform_volatile = waveform             # Set user-defined waveform to volatile memory
print(f"{ch1.free_memory_slots} slots free") # Get number of slots in non-volatile memory
ch1.save_waveform(waveform, "test")          # Save the waveform to non-volatile memory
ch1.shape = "USER"                           # Set channel 1 shape to user-defined
ch1.trigger_mode = "MAN"                     # Set trigger mode to manual
ch1.trigger_count = 2                        # Set number of cycles to be generated to 2
ch1.output = True                            # Enable channel 1 output
generator.trigger()                          # Trigger the generator to output the waveform
ch1.delete_waveform("test")                  # Delete the waveform from non-volatile memory

recorder.close()  # flush and close the publishers
```

The examples are adapted from the [pymeasure documentation for `Keysight81160A`](https://pymeasure.readthedocs.io/en/latest/api/instruments/keysight/keysight81160A.html#pymeasure.instruments.keysight.keysight81160A.Keysight81160A), which lists every property and method.
