67 lines
1.5 KiB
Python
67 lines
1.5 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
import os
|
||
|
import sys
|
||
|
from io import RawIOBase
|
||
|
from typing import List
|
||
|
|
||
|
sys.path.insert(1, os.path.dirname(os.path.dirname(sys.argv[0])))
|
||
|
|
||
|
from draw_memory_map import memory_table # noqa: E402
|
||
|
|
||
|
from srnemqtt.config import get_config, get_interface # noqa: E402
|
||
|
from srnemqtt.protocol import readMemory # noqa: E402
|
||
|
|
||
|
|
||
|
def get_device_name(iface: RawIOBase) -> str | None:
|
||
|
data = readMemory(iface, 0x0C, 8)
|
||
|
if data is None:
|
||
|
return None
|
||
|
|
||
|
return data.decode("utf-8").strip()
|
||
|
|
||
|
|
||
|
def get_device_version(iface: RawIOBase) -> str | None:
|
||
|
data = readMemory(iface, 0x14, 4)
|
||
|
if data is None:
|
||
|
return None
|
||
|
|
||
|
major = (data[0] << 8) + data[1]
|
||
|
minor = data[2]
|
||
|
patch = data[3]
|
||
|
|
||
|
return f"{major}.{minor}.{patch}"
|
||
|
|
||
|
|
||
|
def get_device_serial(iface: RawIOBase) -> str | None:
|
||
|
data = readMemory(iface, 0x18, 3)
|
||
|
if data is None:
|
||
|
return None
|
||
|
|
||
|
p1 = data[0]
|
||
|
p2 = data[1]
|
||
|
p3 = (data[2] << 8) + data[3]
|
||
|
return f"{p1}-{p2}-{p3}"
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
conf = get_config()
|
||
|
iface = get_interface(conf)
|
||
|
|
||
|
print(get_device_name(iface))
|
||
|
print(get_device_version(iface))
|
||
|
print(get_device_serial(iface))
|
||
|
|
||
|
data: List[int] = []
|
||
|
for i in range(0, 0xFFFF, 16):
|
||
|
newdata = readMemory(iface, i, 16)
|
||
|
if newdata:
|
||
|
data.extend(newdata)
|
||
|
# !!! FIXME: Naively assumes all queries return the exact words requested
|
||
|
print(
|
||
|
memory_table(
|
||
|
data,
|
||
|
wordsize=2,
|
||
|
skip_nullrows=True,
|
||
|
)
|
||
|
)
|