Python API

Koheron SDK V1 documentation. The V1 branch is recommended for new instrument development and remains under active development ahead of the major release. Boards currently ship with V0; build and install a V1 OS image to follow this workflow. V1 images do not support legacy V0 instruments.

The Python client calls public C++ driver methods over TCP using the generated drivers.json metadata.

Connect to a board

from koheron import connect

host = "192.168.1.100"
client = connect(host, name="fft")

connect() starts the named instrument if needed, opens a TCP connection and loads driver metadata. Additional arguments are passed to run_instrument().

Write a Python driver wrapper

@command() sends the command; the method body receives its result.

from koheron import command

class FFT:
    def __init__(self, client):
        self.client = client

    @command()
    def get_fft_size(self):
        return self.client.recv_uint32()

    @command()
    def set_input_channel(self, channel):
        pass

    @command()
    def read_psd(self):
        return self.client.recv_array((4096,), dtype='float32')

Class and method names must match drivers.json, or be specified with @command(classname=..., funcname=...).

Application example

from koheron import connect
from fft import FFT

host = "192.168.1.100"
client = connect(host, name="fft")
fft = FFT(client)

print("FFT size:", fft.get_fft_size())
fft.set_input_channel(0)
psd = fft.read_psd()
print(psd[:10])

Upload and run an instrument

Run with the SDK's .venv/bin/python. Unlike make run, these calls return after the HTTP requests complete.

from koheron import upload_instrument, run_instrument

host = "192.168.1.100"
upload_instrument(host, "tmp/examples/alpha250/fft/fft.zip", run=True)

run=True starts the uploaded instrument. Restart a stored instrument with run_instrument(host, "fft", restart=True).

Query instrument status

from koheron import instrument_status

status = instrument_status("192.168.1.100")
print(status)

instruments lists stored names; live_instrument identifies the selected instrument.

Receive typed results

Receive helpers match the C++ return type:

  • uint32_t: recv_uint32()
  • int32_t: recv_int32()
  • float: recv_float()
  • double: recv_double()
  • bool: recv_bool()
  • std::string: recv_string()
  • JSON string: recv_json()
  • std::array<T, N>: recv_array(shape, dtype)
  • std::vector<T>: recv_vector(dtype)
  • std::tuple<...>: recv_tuple(fmt)

The client checks return-type metadata where available and raises an error if the receive helper does not match the exported C++ return type.

Send arrays and vectors

Arguments support scalars, strings, std::array and std::vector types declared in the metadata. Use NumPy arrays with matching dtype for array and vector arguments.

See also

hello@koheron.com