Python Examples

Experimental V1 firmware documentation. Koheron products currently ship with V0.x firmware by default. V1 firmware executables are not publicly available yet. Please contact us if you are interested in early access or would like to evaluate V1 on your device.

Python scripts can control a V1 firmware controller through a serial port. Install pyserial first:

pip install pyserial

Text-command helper

In normal single-controller mode, the controller echoes input and prints the >> prompt. The helper below sends one command, reads until the next prompt, removes the echo and prompt, and returns the remaining response lines.

import time
from serial import Serial

PORT = "/dev/ttyUSB0"  # Use COMx on Windows
BAUD = 115200


def send_command(ser, command):
    ser.write((command + "\r\n").encode("ascii"))
    ser.flush()

    raw = ser.read_until(b">>")
    text = raw.decode("ascii", errors="replace").replace("\r", "\n")

    lines = []
    for line in text.split("\n"):
        line = line.strip()
        if not line:
            continue
        if line == ">>":
            continue
        if line == command:
            continue
        lines.append(line)

    return lines


def query_one(ser, command):
    lines = send_command(ser, command)
    return lines[-1] if lines else ""


with Serial(PORT, BAUD, timeout=1) as ser:
    time.sleep(0.2)
    ser.reset_input_buffer()
    ser.reset_output_buffer()

    # Synchronize with the prompt. Pressing Enter on an empty line redraws it.
    ser.write(b"\r\n")
    ser.flush()
    ser.read_until(b">>")

    # Read commands
    print("version =", query_one(ser, "version"))
    print("model =", query_one(ser, "model"))
    print("serial =", query_one(ser, "serial"))
    print("usrmode =", query_one(ser, "usrmode"))
    print("err =", query_one(ser, "err"))
    print("rtset =", query_one(ser, "rtset"))

    # Write command, then read back
    print("set rtset =", query_one(ser, "rtset 12000"))
    print("rtset =", query_one(ser, "rtset"))

    # Save only after verifying that the value is safe for the connected system
    print("save =", query_one(ser, "save"))

Read one value

rtset = query_one(ser, "rtset")
print(rtset)

Write one value

reply = query_one(ser, "rtset 12000")
print(reply)

Most read/write commands return the resulting value after the write.

Read and clear errors

errors = int(query_one(ser, "err"), 16)
if errors:
    print("error mask:", hex(errors))
    print("cleared:", query_one(ser, "errclr"))

Addressed mode example

When muxon is enabled, commands must be addressed. In that mode the normal prompt is suppressed, so use a different helper that reads one line or a known byte count depending on the command.

def query_mux_line(ser, address, command):
    ser.write((address + " " + command + "\r\n").encode("ascii"))
    ser.flush()
    return ser.readline().decode("ascii", errors="replace").strip()

print(query_mux_line(ser, "@1", "version"))
print(query_mux_line(ser, "@SN12345", "version"))

Broadcast commands execute silently, so do not wait for a normal reply after @* commands.

Binary commands

The helper above is for ASCII commands only. Do not use it for $, logerr, memr or other raw binary transfers. Binary commands need command-specific byte-count handling.

[email protected]