From 54235bb66ddc39436361ae747692f33da660da93 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 28 May 2024 04:33:13 -0700 Subject: [PATCH 1/5] Add parsing script --- parse.py | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 parse.py diff --git a/parse.py b/parse.py new file mode 100644 index 0000000..8510585 --- /dev/null +++ b/parse.py @@ -0,0 +1,142 @@ +# -*- coding: utf-8 -*- + +# Python script to parse Bambu Lab RFID tag data +# Created for https://github.com/Bambu-Research-Group/RFID-Tag-Guide +# Written by Vinyl Da.i'gyu-Kazotetsu (www.queengoob.org), 2024 + +import sys +import struct +from datetime import datetime + +COMPARISON_BLOCKS = [1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14] +IMPORTANT_BLOCKS = [0] + COMPARISON_BLOCKS + +BYTES_PER_BLOCK = 16 +BLOCKS_PER_TAG = 64 +TOTAL_BYTES = BLOCKS_PER_TAG * BYTES_PER_BLOCK + +# Byte conversions +def bytes_to_string(data): + return data.decode('ascii').replace('\x00', ' ').strip() + +def bytes_to_hex(data, chunkify = False): + output = data.hex().upper() + return " ".join((string[0+i:2+i] for i in range(0, len(string), 2))) if chunkify else output + +def bytes_to_int(data): + return int.from_bytes(data, 'little') + +def bytes_to_float(data): + return struct.unpack(' 0: + tag.compare(data[i-1]) + print() + +if __name__ == "__main__": + data = load_data(sys.argv[1:]) + print_data(data, False) From 02c7103d2d0cdcea3e0512ca37edb9e3252f939f Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 28 May 2024 05:05:16 -0700 Subject: [PATCH 2/5] Add Unit class --- parse.py | 58 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/parse.py b/parse.py index 8510585..b67ac7b 100644 --- a/parse.py +++ b/parse.py @@ -48,6 +48,37 @@ class TagLengthMismatchError(TypeError): def __init__(self, actual_length): super().__init__(f"The data does not appear to be a valid MIFARE 1K RFID tag (received {actual_length} bytes / {int(actual_length / BYTES_PER_BLOCK)} blocks, expected {TOTAL_BYTES} bytes / {BLOCKS_PER_TAG} blocks).") +class Unit(): + def __init__(self, value, unit): + self.value = value + self.unit = unit + + def __str__(self): + return str(self.value) + ("º" if self.unit in ["C", "F"] else "") + self.unit + + def __get_comparison_values(self, other): + if type(other) in [int, float]: + return [self.value, other] + if type(other) != Unit: + raise TypeError(f"Type {type(other)} cannot be compared with type Unit") + if self.unit != other.unit: + raise TypeError(f"Unit type {other.unit} is not identical to {self.unit}") + return [self.value, other.value] + + def __eq__(self, other): + values = self.__get_comparison_values(self, other) + return values[0] == values[1] + + def __lt__(self, other): + values = self.__get_comparison_values(self, other) + return values[0] < values[1] + + def __gt__(self, other): + values = self.__get_comparison_values(self, other) + return values[0] > values[1] + +print(type(Unit(1, "g"))) + class Tag(): def __init__(self, filename, data): # Check to make sure the data is 1KB @@ -64,20 +95,20 @@ class Tag(): "filament_type": bytes_to_string(self.blocks[2]), "detailed_filament_type": bytes_to_string(self.blocks[4]), "color": "#" + bytes_to_hex(self.blocks[5][0:4]), - "weight": bytes_to_int(self.blocks[5][4:6]), # in g (grams) - "length": bytes_to_int(self.blocks[14][4:6]), # in m (meters) - "diameter": bytes_to_float(self.blocks[5][8:12]), # in mm (meters) - "spool_width": bytes_to_int(self.blocks[10][4:6]) / 100, # in mm (meters) + "weight": Unit(bytes_to_int(self.blocks[5][4:6]), "g"), + "length": Unit(bytes_to_int(self.blocks[14][4:6]), "m"), + "diameter": Unit(bytes_to_float(self.blocks[5][8:12]), "mm"), + "spool_width": Unit(bytes_to_int(self.blocks[10][4:6]) / 100, "mm"), "material_id": bytes_to_string(self.blocks[1][8:16]), "variant_id": bytes_to_string(self.blocks[1][0:8]), - "nozzle_diameter": bytes_to_float(self.blocks[8][12:16]), # in mm (meters) + "nozzle_diameter": Unit(round(bytes_to_float(self.blocks[8][12:16]), 1), "mm"), "temperatures": { - "min_hotend": bytes_to_int(self.blocks[6][10:12]), # in C (Celsius) - "max_hotend": bytes_to_int(self.blocks[6][8:10]), # in C (Celsius) - "bed_temp": bytes_to_int(self.blocks[6][6:8]), # in C (Celsius) + "min_hotend": Unit(bytes_to_int(self.blocks[6][10:12]), "C"), + "max_hotend": Unit(bytes_to_int(self.blocks[6][8:10]), "C"), + "bed_temp": Unit(bytes_to_int(self.blocks[6][6:8]), "C"), "bed_temp_type": bytes_to_int(self.blocks[6][4:6]), - "drying_time": bytes_to_int(self.blocks[6][2:4]), # in hours - "drying_temp": bytes_to_int(self.blocks[6][0:2]), # in C (Celsius) + "drying_time": Unit(bytes_to_int(self.blocks[6][2:4]), "h"), + "drying_temp": Unit(bytes_to_int(self.blocks[6][0:2]), "C"), }, "x_cam_info": self.blocks[8][0:12], "tray_uid": self.blocks[9], @@ -91,7 +122,12 @@ class Tag(): result = "" for key in self.data: - result += f"- {key}: {bytes_to_hex(self.data[key]) if type(self.data[key]) == bytes else self.data[key]}\n" + if type(self.data[key]) == dict: + result += f"- {key}:\n" + for tkey in self.data[key]: + result += f" - {tkey}: {self.data[key][tkey]}\n" + else: + result += f"- {key}: {bytes_to_hex(self.data[key]) if type(self.data[key]) == bytes else self.data[key]}\n" return result[:-1] From eb6296699707bd597736e5a63d7df802d87c6c3f Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 28 May 2024 06:13:53 -0700 Subject: [PATCH 3/5] Remove leftover debugging --- parse.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/parse.py b/parse.py index b67ac7b..3ba860a 100644 --- a/parse.py +++ b/parse.py @@ -77,8 +77,6 @@ class Unit(): values = self.__get_comparison_values(self, other) return values[0] > values[1] -print(type(Unit(1, "g"))) - class Tag(): def __init__(self, filename, data): # Check to make sure the data is 1KB From ca83aec1e997685ab059d8f57d989bc99c2f5f17 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 28 May 2024 06:15:19 -0700 Subject: [PATCH 4/5] Change a few dictionary keys --- parse.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/parse.py b/parse.py index 3ba860a..cd583b0 100644 --- a/parse.py +++ b/parse.py @@ -92,10 +92,10 @@ class Tag(): "uid": bytes_to_hex(self.blocks[0][0:4]), "filament_type": bytes_to_string(self.blocks[2]), "detailed_filament_type": bytes_to_string(self.blocks[4]), - "color": "#" + bytes_to_hex(self.blocks[5][0:4]), - "weight": Unit(bytes_to_int(self.blocks[5][4:6]), "g"), - "length": Unit(bytes_to_int(self.blocks[14][4:6]), "m"), - "diameter": Unit(bytes_to_float(self.blocks[5][8:12]), "mm"), + "filament_color": "#" + bytes_to_hex(self.blocks[5][0:4]), + "spool_weight": Unit(bytes_to_int(self.blocks[5][4:6]), "g"), + "filament_length": Unit(bytes_to_int(self.blocks[14][4:6]), "m"), + "filament_diameter": Unit(bytes_to_float(self.blocks[5][8:12]), "mm"), "spool_width": Unit(bytes_to_int(self.blocks[10][4:6]) / 100, "mm"), "material_id": bytes_to_string(self.blocks[1][8:16]), "variant_id": bytes_to_string(self.blocks[1][0:8]), From be70dd0f0de3e7bf33e9e51e5ffc47ea41d60f10 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 28 May 2024 06:41:05 -0700 Subject: [PATCH 5/5] Check if any blank blocks are present and warn the user if so --- parse.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/parse.py b/parse.py index cd583b0..9ffbc9f 100644 --- a/parse.py +++ b/parse.py @@ -87,6 +87,11 @@ class Tag(): self.filename = filename self.blocks = list(data[0+i:BYTES_PER_BLOCK+i] for i in range(0, len(data), BYTES_PER_BLOCK)) + # Check for blank blocks + for bi in IMPORTANT_BLOCKS: + if self.blocks[bi] == b'\x00' * BYTES_PER_BLOCK: + print(f"Warning! Block {bi} is blank!") + # Parse the data self.data = { "uid": bytes_to_hex(self.blocks[0][0:4]),