Skip to content

Instantly share code, notes, and snippets.

@ejohnso49
Last active April 19, 2023 13:26
Show Gist options
  • Save ejohnso49/02ecbd3b629315ac63bb8b00f207465a to your computer and use it in GitHub Desktop.
Save ejohnso49/02ecbd3b629315ac63bb8b00f207465a to your computer and use it in GitHub Desktop.
Continuously export Memfault chunks from serial port
import pathlib
import subprocess
import click
import serial
def run_cli_post_chunks(output, project_key, device_serial):
args = [
"memfault",
"--project-key",
project_key,
"post-chunk",
"--device-serial",
device_serial,
"--encoding",
"sdk_data_export",
str(output),
]
subprocess.run(args=args)
@click.command()
@click.argument("device")
@click.argument("output", type=click.Path(exists=False, path_type=pathlib.Path))
@click.option("--project-key", help="Project key to export data to")
@click.option("--device-serial", help="Device serial")
@click.option("--baudrate", default=115200, help="Baudrate of serial port")
@click.option("--timeout", default=60, help="Read timeout before sending batch of chunks")
def read_chunks(device, output, project_key, device_serial, baudrate, timeout):
if output.exists():
click.echo(f"Overwriting file {output}")
port = serial.Serial(device, baudrate=baudrate, timeout=timeout)
while True:
with open(output, "wb") as output_file:
lines = port.readlines()
output_file.writelines([line + b"\n" for line in lines])
output_file.flush()
run_cli_post_chunks(output, project_key, device_serial)
if __name__ == "__main__":
read_chunks(auto_envvar_prefix="MEMFAULT")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment