netmon-multinode/MultiNode/Configurator.py
2022-04-24 03:37:42 +02:00

81 lines
2.3 KiB
Python

import serial, struct
from glob import glob
port = serial.Serial(port=glob("/dev/serial/by-id/usb-Arduino_LLC_Arduino_Zero_*-if00")[0], baudrate=115200)
port.write(b"c")
config_info = {val.split(b":")[0] : int(val.split(b":")[1]) for val in port.read_until(b"END\r\n").split(b"\r\n")[:-2]}
mn_config_size = config_info[b"MNConfiguration"]
mn_cfgmem_size = config_info[b"CFGMEM"]
n_devices = config_info[b"N_DEVICES"]
mn_device_size = config_info[b"Device"]
mn_analog_input_size = config_info[b"AnalogInput"]
print(config_info)
port.write(b"r")
config = port.read(mn_config_size)
print(config)
port.write(b"R")
cfgmem = port.read(mn_cfgmem_size)
print(cfgmem)
modem_frequency = struct.unpack("<f", config[0:4])[0]
modem_power = config[4]
client_address = int.from_bytes(config[5:7], byteorder="little")
server_address = int.from_bytes(config[7:9], byteorder="little")
pointers = list(config[9 : 9 + n_devices])
modem_frequency = 868.0
modem_power = 0
client_address = 0x1234
server_address = 0x0001
def pack_device():
data = struct.pack("<bbb", 0, 0xFF, 0xFF)
assert len(data) == mn_device_size
return data
def pack_analog_input(sensor_type, pin, min_threshold, max_threshold, multiplier, offset, is_current):
data = struct.pack("<bbbffff?", 1, sensor_type, pin, min_threshold, max_threshold, multiplier, offset, is_current)
assert len(data) == mn_analog_input_size
return data
cfgmem = list(cfgmem)
cfgmem_pointer = 0x0
pointer_counter = 0
def add_to_config(entry):
global cfgmem_pointer, pointer_counter
cfgmem[cfgmem_pointer : cfgmem_pointer + len(entry)] = entry
pointers[pointer_counter] = cfgmem_pointer
cfgmem_pointer += len(entry)
pointer_counter += 1
assert cfgmem_pointer < 256
add_to_config(pack_analog_input(5, 15, -1000, 1000, 1, 0, False))
add_to_config(pack_analog_input(6, 16, 3.8, 20.5, 1, 0, True))
config = list(config)
config[0:4] = struct.pack("<f", modem_frequency)
config[4] = modem_power
config[5:7] = int.to_bytes(client_address, 2, byteorder="little")
config[7:9] = int.to_bytes(server_address, 2, byteorder="little")
config[9 : 9 + n_devices] = pointers
port.write(b"W")
port.write(bytearray(cfgmem))
port.write(b"w")
port.write(bytearray(config))
server_key = 0x2e29b257521dc792
client_key = 0x0#0x7ed64cce5b5d8e85
port.write(b"k")
port.write(struct.pack("<QQ", client_key, server_key))
#port.write(b"s")