Program devices in Python

nRF Util

tags
nRF Util

The following Python code snippet uses the JSON output to program all SEGGER J-Link devices with a file firmware.hex and print the results:

import json
import subprocess

firmware_path = "firmware.hex"

# Program all connected J-Link devices and collect the output
output = subprocess.run(
    [
        "nrfutil",
        "--json",
        "device",
        "program",
        "--traits",
        "jlink",
        "--firmware",
        firmware_path,
    ],
    capture_output=True,
)
stdout_as_lines = output.stdout.decode("utf-8").splitlines()

for line in stdout_as_lines:
    parsed_line = json.loads(line)

    # Find the lines indicating that a task ended
    if parsed_line["type"] == "task_end":
        # Print the results of the operation
        print(
            "{}: {}".format(
                parsed_line["data"]["task"]["description"],
                parsed_line["data"]["result"],
            )
        )