Skip to content

Instantly share code, notes, and snippets.

@rindeal
Last active July 21, 2024 19:55
Show Gist options
  • Save rindeal/852365c3b241dfee813cfd02b4e3f705 to your computer and use it in GitHub Desktop.
Save rindeal/852365c3b241dfee813cfd02b4e3f705 to your computer and use it in GitHub Desktop.
Parse UBX messages from u-center's Messages View hex output
#!/usr/bin/env python3
# SPDX-FileCopyrightText: ANNO DOMINI 2024 Jan Chren ~rindeal <dev.rindeal gmail.com>
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
# Homepage: https://gist.github.com/rindeal/852365c3b241dfee813cfd02b4e3f705/
import io
import sys
import re
from typing import Iterator
import argparse
from pyubx2 import UBXReader
def parse_hex_input(input_stream: Iterator[str]) -> bytes:
hex_pattern = re.compile(r'^\s*\S+ ((?:[0-9A-Fa-f]{2}\s*)+) .*$')
bytearr = bytearray()
for i, line in enumerate(input_stream):
matches = hex_pattern.match(line)
if not matches:
print(f"WARN: Line #{i} skipped", file=sys.stderr)
continue
bytearr.extend(bytes.fromhex(matches[1].replace(' ', '')))
return bytes(bytearr)
def process_ubx_messages(raw_data: bytes) -> None:
data_stream = io.BytesIO(raw_data)
ubr = UBXReader(data_stream)
for (raw_data, parsed_data) in ubr:
if parsed_data:
print(parsed_data)
else:
print(f"Unable to parse UBX message: {raw_data.hex()}")
def main() -> None:
parser = argparse.ArgumentParser(
description="Parse UBX messages from u-center's Messages View hex output",
formatter_class=argparse.RawDescriptionHelpFormatter,
prog="ucenter_hex_parser",
usage="%(prog)s < hex_output.txt",
epilog="""
This script parses the hex output from u-center's Messages View when the "Show Hex Toggle"
button is enabled. It processes the hex dump format and parses the UBX messages.
To use:
1. In u-center (not u-center 2), go to the Messages View.
2. Enable the "Show Hex Toggle" button.
3. Copy the hex output from the text box at the bottom of the window.
4. Save it to a file or pipe it directly to this script.
Example input (from u-center Messages View with Show Hex Toggle):
0000 B5 62 06 1B 6C 00 46 15 A8 01 00 00 00 00 64 00 00 00 75 2D 62 6C 6F 78 ...
0018 20 41 47 20 2D 20 77 77 77 2E 75 2D 62 6C 6F 78 2E 63 6F 6D 00 00 00 00 ...
0030 00 00 75 2D 62 6C 6F 78 20 47 4E 53 53 20 72 65 63 65 69 76 65 72 00 00 ...
0048 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...
0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 DF 82 ...
Example usage:
%(prog)s < ucenter_hex_output.txt
%(prog)s <<< '0000 B5 62 06 1B 6C 00 ...'
# Or pipe directly from clipboard:
wl-paste | %(prog)s
""")
parser.parse_args()
if sys.stdin.isatty():
parser.print_help()
sys.exit(1)
raw_data = parse_hex_input(sys.stdin)
process_ubx_messages(raw_data)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment